How to Remove White Space from the File Name in Linux

Not able to access or open file because of white space in file name? Then you can remove the white space from the file name by following three different method .

When you look at your system directory structure, you’ll find that many files are stored in several ways, such as “somefilename.txt”, “some_file_name.txt”, “some-file-name.txt”, or “some file name.txt”.

You’re probably wondering, “What’s wrong with this filename?” I don’t see a problem with it. Yes, you will not find any problems until you try to access the file from the terminal, or else some of the applications will not accept the file if it has a whitespace between filenames.

On the other hand, operating systems like Windows and macOS allow you to access files that contain whitespace between filenames, but this is not the case with Linux.

Here you need to be specific about the file name conventions. Otherwise, you have to follow some additional steps to access the file.

Therefore, we will show you how to remove white space from filenames, so you can access your stored files very easily. Before that, let’s look at what the problem looks like.

How does the problem look? 

So let me try to access one of the files that I have created for you, “sample file.txt”.

As you know, the best way to read files on the CLI is the cat command, so when I execute the below command, it opens different files than what I intended.

$ cat sample file.txt

And when I added a backward slash (\) after the first name, then it opened the correct file, which I have created for you. Thus, if you want to know more about how it happened, then you should check out this article: Enter a File or Directory with Space and Special Characters in its Name?

Behaviour of the above command:

Access file with white space

To get rescued from this problem, you should enclose the file name in double quotes (“”) or else use the proper naming convention. But what about those which we already have?

For that, you should rename the file name, which we will see in the following section. 

Remove Whitespace from File Name using find Command 

One of the best ways to remove white space from the filename is to use the find command, which will work in almost all cases, whether it’s a directory, a regular file, or else you just want to remove space from specific extensions among the other file extensions like .txt.jpg.html, etc. 

So let’s see how you can use the find command with some bash tricks to remove blank space.

Remove White space from File Name using find Command

When you just want to remove all the whitespace from the file name that exists in the current directory, then you should execute the below command, which will replace white space with (_) underscore.

$ find . -type f -name "* *" -exec bash -c 'mv "$0" "${0// /_}"' {} \;

For example, I have several files present in my current directory, and out of that, I want to rename all the files that have white space between the file names.

In that case, I would run the above command, which removes the white space from the file, and I would get the output as shown below:

Remove White space from Directory Name using find Command

The above-mentioned command will only work for files, not on directories because we have already specified the type of file (f). But now, with a slight tweak, you can add an underscore between the whitespace of the directory name.

Let me show you the same thing on my system by executing the below code, which will replace the white space with an underscore notation.

$ find . -type d -name "* *" -exec bash -c 'mv "$0" "${0// /_}"' {} \;
Remove white space from directory name
Remove white space from directory name

Remove White space Based on Extension Name Using find Command

Finally, we are able to remove whitespace from files and directories, but the problem is that it removes whitespace from all the available files, which I don’t want to happen. In that case, you should specify the type of extension on which the command should perform the work.

Let’s say you hold multiple “.txt” files along with others and you want to remove whitespace between filenames. Then you should invoke the below code, which will first find the .txt extension file which has the white space between filenames, and later it will replace the white space with underscore notation.

$ find . -type f -name "* *.txt" -exec bash -c 'mv "$0" "${0// /_}"' {} \;

Remove White Space from File Name using for loop and mv Command

Alternatively, you can also use for loop conditional statement to replace white space from the file and directory name, so let’s see how you can use for loop to file whitespace along the filename.

First I’ll show you how to add the underscore special character between whitespace, so open your terminal window and go to the location where you want to perform the following action, and paste the below command, which will add the underscore character between whitespace to fill cap.

$ for f in *\ *; do mv "$f" "${f// /_}"; done

The behaviour of the above command

remove white space using for loop condition
Remove white space using for loop condition

If you do not want to use underscore or any special character, then execute the below code

$ for f in *\ *; do mv "$f" "${f// /}"; done

Remove White Space from File Name using rename Command

You can also remove white space from the file name using the rename command, which is mainly used to rename groups of files very easily. If you want to know more about rename command, then you can check this article from here because discussing over here is out of scope.

Unfortunately, this utility is not preinstalled on major Linux distributions, but you can easily install it from system repositories by invoking the following list of commands as per your distribution.

$ sudo apt install rename         // Debian, Ubuntu, and Mint
$ sudo dnf install prename        // RHEL/CentOS/Fedora, and Rocky Linux/AlmaLinux
$ sudo pacman -S rename           // Arch Linux

Once the installation is finished, you can execute the below command on the current directory, which will globally substitute whitespace with underscore notation (_), which ends with the.txt extension.

For example, I do have multiple files that belong in my demo directory, where I’m performing all the steps. Among the other files, I just want to replace whitespace with underscore, which ends with the .txt extension, and other file extensions should be excluded.

$ rename 's/\s/_/g' ./*.txt
remove white space only to specific extension using rename command
Remove white space only to a specific extension using rename command

Alternatively, I can also add underscore notation to all the files by invoking the below command.

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

If you want to replace white space among all files and directory names, then you should consider the below command, which will substitute white space with an underscore for all the available files that have white space between files.

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

No matter if it is a directory or a file, it will complete the task as shown below image.

remove all the white space from filename in the current directory
Remove all the white space from the filename in the current directory

If you remember, we have already discussed how to use the find command to detect white space and replace it with an underscore, which is a bit complex to recall.

Instead of following that command, you can take note of this alternative command, which requires rename utilities to be installed, and it is simple as well.

You can pipe the find command with the rename command to remove white space from the directory filename or from the regular file.

$ find . -name "* *" -type f | rename 's/ /_/g'         // Remove whitespace from filename
$ find . -name "* *" -type d | rename 's/ /_/g'        // Remove whitespace from directory name 

Wrap up

That’s all for now! If you are having any problems while following, then do let us know in the comment section, and also please do share this article on your social media if this article helped you.

0 0 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
David Moore
David Moore
1 year ago

Is there a fix for filenames with Windows naming conventions to be accessed by their corresponding programs run under Linux, in Wine, for instance?