How to Replace Spaces in File Names Using a Bash Script

How to replace spaces in file names using a bash script

Use rename (aka prename) which is a Perl script which may be on your system already. Do it in two steps:

find . -name "* *" -type d | rename 's/ /_/g'    # do the directories first
find . -name "* *" -type f | rename 's/ /_/g'

Based on Jürgen's answer and able to handle multiple layers of files and directories in a single bound using the "Revision 1.5 1998/12/18 16:16:31 rmb1" version of /usr/bin/rename (a Perl script):

find /tmp/ -depth -name "* *" -execdir rename 's/ /_/g' "{}" \;

Linux - Replacing spaces in the file names

This should do it:

for file in *; do mv "$file" `echo $file | tr ' ' '_'` ; done

Replace spaces in all files in a directory with underscores

find . -type f -exec sed -i -e 's/ /_/g' {} \;

find grabs all items in the directory (and subdirectories) that are files, and passes those filenames as arguments to the sed command using the {} \; notation. The sed command it appears you already understand.

if you only want to search the current directory, and ignore subdirectories, you can use

find . -maxdepth 1 -type f -exec sed -i -e 's/ /_/g' {} \;

Script to remove spaces in all files and folders?

Try this

ls | grep " " | while read file_name
mv "$file_name" "$(echo $file_name | sed -E 's/ +//g')"

sed -E is so that you can use some simple regex, and / +/ so it can work in case of multiple consecutive spaces such as . And /g so it replaces every occurrences such as foo baa .txt .

Is there an efficient way to replace spaces with _ in filenames? (thousands of files)

for f in "$(find . -name '* *')"; do mv $f $(echo $f | sed 's/\ /_/'); done will do it. There should be a better way using find's -exec option as well.

If the function is put in a script, then find can be used directly:

cat <<EOF > space_to_underscore
#!/usr/bin/env bash
mv "$1" "$(sed 's/\ /_/' <(echo "$1"))"
chmod +x space_to_underscore

find . -name '* *' -exec ./space_to_underscore {} \;

This will be faster than using a for loop.

Eliminate spaces in filename and rename (cywgin)


Swap $jay and $jay2. The mv command uses the first argument as the source and the second argument as the destination:

mv sourceFile destinationFile

Don't forget to quote, since you have spaces:

mv "$jay" "$jay2"


If you have rename installed, you can replace your script with the following command:

rename 's/ //g' *

The s/ //g means substitute (s) space (/ /) with the empty string (//) globally (g).

The wildcard * specfies the files to be renamed, that is all files in the working directory.

sed replace spaces in directory names

sed is meant for search and replacement on files and not on directories in Linux/Unix. The -i flag in sed is used to make the text replacement on-the-fly on a file, the action simply does not make sense for a directory. You probably meant to change the name of the directory using sed on the filename and eventually use mv to rename the actual directory with the replaced string.

But you could just use mv in the first place with shell native features to replace white space with a - character.

for directory in **; do
if [[ -d $directory ]] && [[ -w $directory ]]; then
mv -- "$directory" "${directory// /-}"

Related Topics

Leave a reply