I have a load of files(mp3, wav, txt, doc) that have been created in MS Windows and they have spaces in their names. eg The file of whoever.doc

I would like to rename them all at once, replacing the space with an underscore or dot.

  • Underscores or dots? – Jeff Schaller Nov 16 '17 at 18:46
  • I would use rename or tr (no backtick on this keyboard!) – Joe Healey Nov 16 '17 at 18:50
  • 1
    You do know it's possible to use filenames containing spaces in the UNIX/Linux world, don't you...? – roaima Nov 16 '17 at 21:00
  • If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you! – Jeff Schaller Nov 19 '17 at 14:49

The shell can do this pretty easily:

for file in *.doc *.mp3 *.wav *.txt
  mv -- "$file" "${file// /_}"

Change the *.doc ... glob to match whatever files you're interested in renaming.

To rename all of the files in the current directory that currently have spaces in their filenames:

for file in *' '*
  mv -- "$file" "${file// /_}"

You might also consider adding a "clobber" check:

for file in *' '*
  if [ -e "${file// /_}" ]
    echo Warning, skipping "$file" as the renamed version already exists

  mv -- "$file" "${file// /_}"
  • is there a way to include all file types and folders as well? at the moment i am adding/removing the file extensions, but i cannot get all of them and i cannot have the folders. Also some of them have a slash. is it possible to make it as underscore? – john Nov 29 '17 at 18:08
  • @john, see my recent edit; for recursively renaming files & folders, you'll want to look for (or ask) a question that focuses on a 'find' based solution, as you don't want to rename folders before catching the files inside of them. Unix files cannot contain (forward) slashes, so either it's a backslash, or something that looks like a slash but isn't actually, or you're wanting to move files in subdirectories up a level and renaming them to include the old directory name with an underscore. All good material for a separate question. – Jeff Schaller Nov 29 '17 at 19:40

You can use sed, e.g.:

mv -i "${original_file}" "$(echo "${original_file}" | sed 's/ /_/g')"

Here's an example.

Create a file with spaces in its name:

touch "/tmp/test file with spaces"

Rename the file:

mv -i \
"/tmp/test file with spaces" \
"$(echo "/tmp/test file with spaces" | sed 's/ /_/g')"

Here is the new name of the file:


You could do the same thing with tr instead, i.e.:

mv -i "${original_file}" "$(echo "${original_file}" | tr ' ' _)"

Or using Bash substring replacement:

mv -i "${original_file}" "${original_file// /_}"

Or using the rename command:

rename "s/ /_/g" "${original_file}"
rename s/\ /_/ *

I can’t remember right now whether the white space needs escaping or not with rename.

(Or swap * for whatever extension.)

  • 1
    Only when you don't quote the substitution pattern. If recommend rename 's/ /_/g' * myself. Or even rename 'y/ /_/' * – roaima Apr 3 at 17:54

Aside from what's been mentioned, the program detox might be of some interest. It's designed specifically for doing this type of thing, and handles lots of stuff other than just spaces. Provided you don't have any diacritical marks, parentheses, or other odd stuff in the filenames, the following should do exactly what you want if run at the top of the directory tree:

detox -r .

Note that that will also fix the names of directories, so do not run that on the Users directory from a Windows system, otherwise you're liable to break things (Windows requires specific names for certain folders in the user directories, and this will change those folders' names).

  • 1
    @roaima I've added an example to the answer as requested (as well as a warning about usage). – Austin Hemmelgarn Nov 17 '17 at 16:23

Replace all spaces with underscores in mp3, txt, wav, and doc filenames using qmv and vim:

qmv -f do --editor="vim -c '%s/ /_/g' -c 'wq'" \
    *.mp3 *.txt *.wav *.doc

qmv is a part of renameutils.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.