How to Remove Spaces from Filenames in Linux

In other operating system environments, creating and using filenames with spaces is irrevocably permissible. However, when we enter the Linux operating system domain, the existence of such filenames becomes an inconvenience.

For instance, consider the existence of the following filenames inside a Linux operating system environment.

Filenames with Spaces in Linux
Filenames with Spaces in Linux

As per the command line view of these filenames, processing or moving them becomes an unwarranted inconvenience because of the white space in their naming convention.

Additionally, filename spaces in Linux are a disadvantage to users processing them via web-based applications as the string %20 tends to be included as part of the processed/final filename.

This article takes a look at valid approaches to help you get rid of spaces on filenames while working under a Linux operating system environment.

Prerequisite

Familiarize yourself with the usage of the Linux terminal or command-line interface. For practical reference purposes, we will be using the spaced filenames presented in the following screen capture.

Filenames with Spaces in Linux
Filenames with Spaces in Linux

1. Removing Spaces from Filename with Specific File Extension

The find command is combined with the mv command to effectively execute its functional objective to remove spaces on a filename with a specific file extension e.g .xml files.

$ find . -type f -name "* *.xml" -exec bash -c 'mv "$0" "${0// /_}"' {} \;
Remove Spaces from Filenames in Linux
Remove Spaces from Filenames in Linux

2. Replacing Filename Spaces Using rename Command

Alternatively, instead of using find with mv commands to trace and replace filename spaces, we could use a single rename command, which is a pearl extension and can be installed in the following Linux operating system distributions:

$ sudo apt install rename         [On Debian, Ubuntu and Mint]
$ sudo yum install rename         [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux]
$ sudo emerge -a sys-apps/rename  [On Gentoo Linux]
$ sudo pacman -S rename           [On Arch Linux]
$ sudo zypper install rename      [On OpenSUSE]    

Once installed, the rename command can be used in the following manner:

$ rename 's/\s/_/g' ./*.xml

The above command will replace the spaces in all .xml files with an underscore. To replace all the spaces in all the filenames regardless of the file extension, use:

 
$ rename 's/\s/_/g' ./*.* 
Delete Spaces from Filenames in Linux
Delete Spaces from Filenames in Linux

3. Replacing Filename Spaces Using For Loop and mv Command

This approach is effective in getting rid of filename spaces on different file name formats existing on a specific/targeted folder/directory. The for loop function queries for filename spaces inside a targeted directory/folder and afterward replaces those filename spaces with an underscore notation.

Consider the following implementation of this approach:

$ for f in *; do mv "$f" `echo $f | tr ' ' '_'`; done
Removing Filename Spaces in Linux
Removing Filename Spaces in Linux

Before the start of this article, filename spaces were a nuisance especially when you need to copy and move files from the Linux terminal or process them via a web-based program. This tutorial has provided a viable solution that works against such inconveniences.

2 thoughts on “How to Remove Spaces from Filenames in Linux”

  1. This will be useful also for removing underscores. I installed the Arduino IDE on my Ubuntu 20.04 system via Synaptic. It will not accept any file with an underscore in its name, apparently a Java thing. In fact, the IDE will not start if there are any files in the sketchbook with non-alpha characters.

    Reply

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.