Linux - Replacing spaces in file names

I have several files in a folder and I want to replace each space character in all file names with underscores. How can I achieve this?

+55
linux command replace filenames spaces
Nov 27 '09 at 5:08
source share
10 answers

This should do it:

for file in *; do mv "$file" `echo $file | tr ' ' '_'` ; done 
+97
Nov 27 '09 at 5:42
source share

I prefer using the "rename" command, which accepts Perl-style regular expressions:

 rename "s/ /_/g" * 

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

 rename -n "s/ /_/g" * 
+39
Nov 27 '09 at 5:45
source share

Use sh ...

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

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

+7
Nov 27 '09 at 5:39
source share

What if you want to apply the replacement task recursively ? How do you do this?

Ok, I just found the answer myself. There is not the most elegant solution (trying to rename files that do not meet the condition), but it works. (By the way, in my case I needed to rename files with "% 20", and not with 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 
+4
Apr 02 '10 at 20:33
source share

If you are using bash:

 for file in *; do mv "$file" ${file// /_}; done 
+3
Nov 27 '09 at 5:24
source share

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

 for files in *.txt; do mv "$files" `echo $files | tr ' ' '_'`; done 
+1
Nov 27 '09 at 5:21
source share

Enter your variables:

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

Remove the echo to actually rename.

+1
Nov 27 '09 at 5:42
source share

I believe your answer is in Replace spaces in filenames with underscores .

0
Nov 27 '09 at 5:18
source share

The easiest way to replace a string (the 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.

0
Dec 09 '16 at 13:03
source share

I am trying to run this as a script https://hastebin.com/xemitireri.bash ,

 #!/bin/bash # https://stackoverflow.com/questions/1806868/linux-replacing-spaces-in-the-file-names find . -type d | while read N do ( cd "$N" if test "$?" = "0" then for file in *; do mv "$file" ${file// /_}; done fi ) done 

I want to run this recursively in one folder, in which there are several folders, each of which has several files. Both folder names and file names have spaces in them (for example, to run in the "Test" folder, which contains a "Good album", which contains "Disk 1", "Disk 2", etc. Each folder of the disk has "Song 1.txt", "Song 2.txt", "Song 3.txt, etc.). This DID renames the folders, but not the files inside the folders. I get the error bash: cd: ./ Disk 02: no such a file or directory bash: cd: ./ Disk 01: there is no such file or directory bash: cd: ./ Disk 03: there is no such file or directory

-one
Jul 13 '17 at 9:51 on
source share



All Articles