Linux - Replacing spaces in the file names

LinuxReplaceCommandFilenamesSpaces

Linux Problem Overview


I have a number of files in a folder, and I want to replace every space character in all file names with underscores. How can I achieve this?

Linux Solutions


Solution 1 - Linux

This should do it:

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

Solution 2 - Linux

I prefer to use the command 'rename', which takes Perl-style regexes:

rename "s/ /_/g" *

You can do a dry run with the -n flag:

rename -n "s/ /_/g" *

Solution 3 - Linux

Use sh...

for i in *' '*; do   mv "$i" `echo $i | sed -e 's/ /_/g'`; done

If you want to try this out before pulling the trigger just change mv to echo mv.

Solution 4 - Linux

If you use bash:

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

Solution 5 - Linux

What if you want to apply the replace task recursively? How would you do that?

Well, I just found the answer myself. Not the most elegant solution, (also tries to rename files that do not comply with the condition) but it works. (BTW, in my case I needed to rename the files with '%20', not with an underscore)

#!/bin/bash
find . -type d | while read N
do
     (
           cd "$N"
           if test "$?" = "0"
           then
               for file in *; do mv "$file" ${file// /%20}; done
           fi
     )
done

Solution 6 - Linux

Here is another solution:

ls | awk '{printf("\"%s\"\n", $0)}' | sed 'p; s/\ /_/g' | xargs -n2 mv
  1. uses awk to add quotes around the name of the file
  2. uses sed to replace space with underscores; prints the original name with quotes(from awk); then the substituted name
  3. xargs takes 2 lines at a time and passes it to mv

Solution 7 - Linux

Try something like this, assuming all of your files were .txt's:

for files in *.txt; do mv$files” `echo $files | tr ‘ ‘ ‘_’`; done

Solution 8 - Linux

Quote your variables:

for file in *; do echo mv "'$file'" "${file// /_}"; done

Remove the "echo" to do the actual rename.

Solution 9 - Linux

To rename all the files with a .py extension use,

find . -iname "*.py" -type f | xargs -I% rename "s/ /_/g" "%"

Sample output,

$ find . -iname "*.py" -type f                                                     
./Sample File.py
./Sample/Sample File.py
$ find . -iname "*.py" -type f | xargs -I% rename "s/ /_/g" "%"
$ find . -iname "*.py" -type f                                                     
./Sample/Sample_File.py
./Sample_File.py

Solution 10 - Linux

This will replace ' ' with '_' in every folder and file name recursivelly in Linux with Python >= 3.5. Change path_to_your_folder with your path.

Only list files and folders:

python -c "import glob;[print(x) for x in glob.glob('path_to_your_folder/**', recursive=True)]"

Replace ' ' with '_' in every folder and file name

python -c "import os;import glob;[os.rename(x,x.replace(' ','_')) for x in glob.glob('path_to_your_folder/**', recursive=True)]"

With Python < 3.5, you can install glob2

pip install glob2
python -c "import os;import glob2;[os.rename(x,x.replace(' ','_')) for x in glob2.glob('path_to_your_folder/**')]"

Solution 11 - Linux

The easiest way to replace a string (space character in your case) with another string in Linux is using sed. You can do it as follows

sed -i 's/\s/_/g' *

Hope this helps.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMithun SreedharanView Question on Stackoverflow
Solution 1 - LinuxneeshView Answer on Stackoverflow
Solution 2 - LinuxDF.View Answer on Stackoverflow
Solution 3 - LinuxDigitalRossView Answer on Stackoverflow
Solution 4 - LinuxMurali VPView Answer on Stackoverflow
Solution 5 - LinuxjavipasView Answer on Stackoverflow
Solution 6 - LinuxyoogottamkView Answer on Stackoverflow
Solution 7 - LinuxAmir AfghaniView Answer on Stackoverflow
Solution 8 - Linuxghostdog74View Answer on Stackoverflow
Solution 9 - Linuxakilesh rajView Answer on Stackoverflow
Solution 10 - LinuxKatuView Answer on Stackoverflow
Solution 11 - LinuxDesta Haileselassie HagosView Answer on Stackoverflow