Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I named a number of files with spaces in them, and I want to replace the space with "_". However, every time I write a command in the shell with the file name (eg "Spring 2011"), the shell doesn't recognize the file or directory. What can I do about this? Is there any way to use the unicode character for a space?

share|improve this question

migrated from stackoverflow.com Jun 12 '11 at 3:35

This question came from our site for professional and enthusiast programmers.

4 Answers 4

up vote 23 down vote accepted

Escape the space, e.g. Spring\ 2011, or use quotes, e.g. 'Spring 2011'. In the future, it's typically a bad idea to use file names with spaces in them on any *NIX.

If you've got rename, you can use this:

rename ' ' '_' [filenames...]
share|improve this answer
Do you know is there anyway to do this for all the files at once? –  Phil Braun Jun 8 '11 at 19:55
@phil for the rename command, specify a pattern for filenames that matches all the files you want to rename (e.g. if there's a common prefix/suffix). –  Rafe Kettler Jun 8 '11 at 20:04
Why is it a bad idea? It's the responsibility of the programmer to handle filenames properly. –  glenn jackman Jun 9 '11 at 14:28
@rafe, really it's error prone not to handle filenames properly up front. The extra effort comes when having to debug a script when you simply forgot to quote variables containing a filename. –  glenn jackman Jun 9 '11 at 15:52
This answer does not seem to work (on Ubuntu 12.04 LTS, at least). The answer from unutbu works fine though. –  DNA Sep 3 '12 at 14:44

If your machine has the rename command, then this will change all spaces to underscores in all files/dirs in the current working directory:

rename 's/ /_/g' *
share|improve this answer
Confirmed to work in Ubuntu 12.04 –  anthonygore Oct 10 '14 at 6:50

If you don't have rename or prefer to use just the shell:

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

Broken down:

  • *\ * selects all files with a space in their name as input for the the for loop.
  • The quotes around "$f" are important because we know there's a space in the filename and otherwise it would appear as 2+ arguments to mv.
  • ${f//str/new_str} is a bash-specific string substitution feature. All instances of str are replaced with new_str.
share|improve this answer
+1 nice answer. –  glenn jackman Jun 22 '11 at 17:09
this works but for me it only replaces the first space. if i use for f in *\ *; do mv "$f" "${f// /_}"; done it works –  billynoah Jul 7 '14 at 21:52
@billynoah Good catch, updated. –  blahdiblah Jul 7 '14 at 22:05

mv "Spring 2011.file" Spring_2011.file should tell the command-line to take the quoted string as a single input.

share|improve this answer
This is the only answer that I could get to work. –  blakeoft Jan 20 at 22:00

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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