Rename/Replace white space in files in entire directory recursively

The purpose of this tutorial is to show various methods on how to rename or replace white space in multiple files. Some tools on a Linux system may not work properly with files that contain white spaces, which would give some users motivation to replace the white space with underscores, for example. Manipulating files without white space is much easier, as you do not have to worry about escaping the file name each time.

In this tutorial, you will see how to rename/replace the white space recursively through files in Linux. This will include renaming individual files, an entire directory, as well as a directoy’s contents and the contents of all of its subdirectories.

In this tutorial you will learn:

  • How to rename/replace white space in an individual file
  • How to rename/replace white space on all files in a directory
  • How to recursively rename/replace white space on all files
Rename/Replace white space in files in entire directory recursively
Rename/Replace white space in files in entire directory recursively
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software mv, tr, find, rename, mmv
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Rename/Replace white space in files in entire directory recursively examples




All of the following examples will take place on the Linux command line. Open a terminal and navigate to the directory which contains the files you want to rename, then try out some of the commands below to get started.

  1. To replace the white space in all files with underscores, the following command inside of a Bash for loop will do the job. Note that this command will not touch files within subdirectories.
    $ for f in *; do mv "$f" $(echo $f | tr ' ' '_'); done
    
  2. To remove the white spaces in an individual file and substitute underscores in their place, you can use the following command:
    $ myfile="some file.txt" ; mv "$myfile" $(echo $myfile | tr ' ' '_')
    

    Here we are assigning a variable $myfile with the file name that we wish to rename. This allows us to call on the variable two times in our subsequent command. Be sure to take out some file.txt in the example above and put your own file name in its place.

  3. What if we want to remove the white space completely, and not put anything in its place? In that case, we can leave out the underscore in the tr command in the above examples and use the -d option.
    $ for f in *; do mv "$f" $(echo $f | tr -d ' '); done
    
  4. To recursively replace all white spaces in file names with underscores, we can use the Linux find command. Note that this command will traverse all subdirectories, no matter how deep, and change the names for all files that contain any white space. This behavior can be altered with the -maxdepth option.
    $ find . -type f -name "* *" -exec bash -c 'mv "$0" "${0// /_}"' {} \;
    



  5. If you have the rename utility installed on your Linux system, there is a very easy syntax to remove the white space from all files and replace it with underscores:
    $ rename 's/\s/_/g' ./*.*
    
  6. Anoter utility which is not ordinarily installed by default on Linux, but can do the job very well, is mmv. The following command will remove all white spaces and replace them with underscores:
    $ mmv '* *' '#1_#2'
    
NOTE
You may also be interested in our tutorial on How to rename multiple files on Linux where we show more examples of batch file renaming, as well as how to install the rename and mmv commands.


Closing Thoughts

In this tutorial, we saw how to remove the white space from files on a Linux system. Files without white space are easier to work with, as they have have compatibility with more tools on Linux, and we do not have to remember to escape the file names any time we are working with the files. Whether you have a handful or a few thousand of files to rename, the commands in this tutorial will get the renaming task done in no time.



Comments and Discussions
Linux Forum