Remove or substitute space within a file name

The purpose of this tutorial is to show various methods on how to remove or substitute spaces within a file name. Some tools on a Linux system may not work properly with files that contain spaces, which would give some users motivation to replace the white space with underscores or just delete them completely, 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 remove or substitute the spaces in file names in Linux.

In this tutorial you will learn:

  • Various commands used to remove or substitute spaces in file names
Remove or substitute space within a file name
Remove or substitute space within a file name
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Bash shell, ls, grep, mv, echo, tr, rename, mmv, detox
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

Remove or substitute space within a file name




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 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 ' ' '_')
    
  2. To remove the white spaces completely, we can use the same command above but use tr and the -d option to remove spaces:
    $ myfile="some file.txt" ; mv "$myfile" $(echo $myfile | tr -d ' ')
    
  3. Here’s another command that can remove the spaces from all files within a directory:
    $ ls | grep " " | while read -r f; do mv -i "$f" `echo $f | tr -d ' '`; done
    
  4. The same command as above, but this time it will replace the spaces with underscores:
    $ ls | grep " " | while read -r f; do mv "$f" `echo $f | tr ' ' '_'`; done
    
  5. The detox command will clean up all kinds of strange characters in file names. One of its default functions is to replace spaces with underscores.
    $ detox my\ file.txt
    OR
    $ detox *
    

    To see detox installation and more usage instructions, see our tutorial on Clean up filenames with detox command line utility.

  6. 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' ./*.*
    
  7. 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.