Posted: . At: 2:24 PM. This was 11 years ago. Post ID: 5409
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


How to replace spaces in file or directory names with hyphens or underscores with the Linux shell.


This command will take all files and directories in a folder and replace all of the spaces in their names with hyphens. This could be a very useful command when you want the files to have sensible filenames.

john@adeptus-mechanicus ~ $ ls -hula | perl -e 'rename $_, s/\s+/-/gr for (<*>)'

This example will replace all of the spaces in the filenames with underscores.

john@adeptus-mechanicus ~ $ ls -hula | perl -e 'rename $_, s/\s+/_/gr for (<*>)'

Here is another method; using bash itself. Using this command: for f in *; do mv -iv "$f" "${f// /-}"; done;.

john@adeptus-mechanicus ~/Documents/mine $ ls -hula
total 8.0K
drwxrwxr-x  2 john john 4.0K Feb 24 14:10 .
drwxr-xr-x 10 john john 4.0K Feb 24 14:09 ..
-rw-rw-r--  1 john john    0 Feb 24 14:10 this is a file with spaces2.txt
-rw-rw-r--  1 john john    0 Feb 24 14:10 this is a file with spaces3.txt
-rw-rw-r--  1 john john    0 Feb 24 14:10 this is a file with spaces4.txt
-rw-rw-r--  1 john john    0 Feb 24 14:10 this is a file with spaces.txt
 
john@adeptus-mechanicus ~/Documents/mine $ for f in *; do mv -iv "$f" "${f// /-}"; done;
`this is a file with spaces2.txt' -&gt; `this-is-a-file-with-spaces2.txt'
`this is a file with spaces3.txt' -&gt; `this-is-a-file-with-spaces3.txt'
`this is a file with spaces4.txt' -&gt; `this-is-a-file-with-spaces4.txt'
`this is a file with spaces.txt' -&gt; `this-is-a-file-with-spaces.txt'
 
john@adeptus-mechanicus ~/Documents/mine $ ls -hula
total 8.0K
drwxrwxr-x  2 john john 4.0K Feb 24 14:10 .
drwxr-xr-x 10 john john 4.0K Feb 24 14:09 ..
-rw-rw-r--  1 john john    0 Feb 24 14:10 this-is-a-file-with-spaces2.txt
-rw-rw-r--  1 john john    0 Feb 24 14:10 this-is-a-file-with-spaces3.txt
-rw-rw-r--  1 john john    0 Feb 24 14:10 this-is-a-file-with-spaces4.txt
-rw-rw-r--  1 john john    0 Feb 24 14:10 this-is-a-file-with-spaces.txt
 
john@adeptus-mechanicus ~/Documents/mine $

This is how to only list directories and then rename them. But this works perfectly.

jason@jason-Lenovo-H50-55:~/Documents$ ls -d */ | perl -e 'rename $_, s/\s+/_/gr for (<*>)'

This is the directory before.

jason@jason-Lenovo-H50-55:~/Documents$ ls
 ballgirl.webm   ballreaction.webm   message.txt   nc   nc.nasm   systemd   testing.c  'this is a dir '

And this is the directory afterwards.

jason@jason-Lenovo-H50-55:~/Documents$ ls
ballgirl.webm  ballreaction.webm  message.txt  nc  nc.nasm  systemd  testing.c  this_is_a_dir_

This is indeed a very useful tip for Linux use.


1 thought on “How to replace spaces in file or directory names with hyphens or underscores with the Linux shell.”

Leave a Reply to ryantate13 Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.