Search code examples
linuxbashwhitespacefilenames

How to replace spaces in file names using a bash script


Can anyone recommend a safe solution to recursively replace spaces with underscores in file and directory names starting from a given root directory? For example:

$ tree
.
|-- a dir
|   `-- file with spaces.txt
`-- b dir
    |-- another file with spaces.txt
    `-- yet another file with spaces.pdf

becomes:

$ tree
.
|-- a_dir
|   `-- file_with_spaces.txt
`-- b_dir
    |-- another_file_with_spaces.txt
    `-- yet_another_file_with_spaces.pdf

Solution

  • Use rename (aka prename) which is a Perl script which may be on your system already. Do it in two steps:

    find . -name "* *" -type d | rename 's/ /_/g'    # do the directories first
    find . -name "* *" -type f | rename 's/ /_/g'
    

    Based on Jürgen's answer and able to handle multiple layers of files and directories in a single bound using the "Revision 1.5 1998/12/18 16:16:31 rmb1" version of /usr/bin/rename (a Perl script):

    find . -depth -name "* *" -execdir rename 's/ /_/g' "{}" \;