Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 Jun 12 '11 at 3:35

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

up vote 26 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
@blahdiblah Thank you so much! You just saved me so much time! – Guerrilla Feb 20 at 17:03

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 '15 at 22:00

You must log in to answer this question.

protected by bwDraco Oct 1 '15 at 20:37

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged .