How do I rename files with spaces using the Linux shell?

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?


Solution 1:

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...]

Solution 2:

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' *