How to replace spaces in file names using bash script on Linux?

Consider a case in which one directory on my local machine looks something like this −

immukul@192 dir1 % ls -ltr
total 0
-rw-r--r-- 1 immukul staff 0 Jul 3 20:44 sample code.txt
-rw-r--r-- 1 immukul staff 0 Jul 3 20:44 sample code with love.txt

In the above directory named dir1, there are two .txt files present and both these files have spaces in between the words in their name, and we need to replace these spaces using a bash script on Linux.

In order to achieve what we want, we must first be aware of the fact that we can traverse the files via a Linux command and print their names, then we can later modify that command to replace the spaces in the files name to _ and get the desired output.

Method 1: Using For Loop with Parameter Expansion

To print the spaces in the files names we just need to type the following command in the terminal −

for f in *.txt; do echo ${f}; done;

Output

immukul@192 dir1 % for f in *.txt; do echo ${f}; done;

sample code with love.txt
sample code.txt

Now, we just need to modify the part after the first semicolon to allow us to replace the spaces with the underscores.

The command for that is −

for file in *.txt; do mv "$file" "${file// /_}"; done

In the above command, we are iterating over all the files that are matching the .txt extension then we are taking one file at a time and then moving it to a new file. The ${file// /_} syntax is parameter expansion where // replaces all spaces with underscores.

Output

immukul@192 dir1 % ls -tlr
total 0
-rw-r--r-- 1 immukul staff 0 Jul 3 20:44 sample_code.txt
-rw-r--r-- 1 immukul staff 0 Jul 3 20:44 sample_code_with_love.txt

Method 2: Creating a Bash Script

A better way is to put the command into a bash script and then run that bash script. To create a bash script, create a file named sample.sh and then give it execute permissions −

touch sample.sh
chmod +x sample.sh

Now put the following content inside the sample.sh file −

#!/bin/bash

# Replace spaces with underscores in .txt files
for file in *.txt; do
    if [[ -f "$file" ]]; then
        new_name="${file// /_}"
        if [[ "$file" != "$new_name" ]]; then
            mv "$file" "$new_name"
            echo "Renamed: '$file' -> '$new_name'"
        fi
    fi
done

Execute the file using the following command −

./sample.sh

Output

Renamed: 'sample code.txt' -> 'sample_code.txt'
Renamed: 'sample code with love.txt' -> 'sample_code_with_love.txt'

Method 3: Using the rename Command

Linux also provides a built-in rename command that can handle this task more efficiently −

rename 's/ /_/g' *.txt

This command uses regular expressions where s/ /_/g means substitute all spaces with underscores globally.

Key Points

  • Always use double quotes around file variables to handle spaces properly: "$file"

  • The ${file// /_} syntax replaces all occurrences of spaces with underscores

  • Check if file exists using [[ -f "$file" ]] before processing

  • Use chmod +x instead of chmod 777 for better security

  • The rename command provides a more concise solution for bulk renaming

Conclusion

Replacing spaces in file names can be accomplished using bash parameter expansion, custom scripts, or the built-in rename command. The parameter expansion method ${file// /_} is the most portable approach, while rename offers the most concise solution for bulk operations.

Updated on: 2026-03-17T09:01:38+05:30

669 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements