Batch removal of spaces in filenames via the Ubuntu command line

While working with Linux, you might come across some utilities and apps that only work with file names that do not include any spaces. We do not always save files in this “no space” format and might have to look for a workaround that removes spaces in filenames altogether. This way your filenames will contain no spaces and you can easily work with them in all your applications.

In this article, we will explain two ways for you to remove spaces from your filenames, very simply through the Ubuntu command line.

We have run the commands and procedures mentioned in this article on a Ubuntu 22.04 LTS system.

Method 1: Through the mv command in ‘for’ loop

In this method, we will be making use of the Ubuntu mv command, in a for loop, in order to rename all files/folders in a directory such that all the spaces are removed.

Open your Ubuntu command line, the Terminal, either through the Application Launcher search or the Ctrl+Alt+T shortcut.

Here is how the files in my sample folder look like; I have listed the contents using the ls command:

List of files with spaces in filenames

So, all the file names contain not one, but two spaces each.

This is the command I will use, while in the directory whose files I want to rename:

$ for oldname in *; do newname=`echo $oldname | sed -e 's/ //g'`; mv "$oldname" "$newname";

done

When I listed the contents of the directory again, you can see that all the file names no longer contain any spaces.

Method 2: Using a bash script to rename files

In this method, we will make use of a bash script that uses the mv command in order to rename file and folder names in a way that all the spaces are removed.

Open the Terminal application and move to the bin folder as follows:

$ cd ~bin

Now, open a new script file in one of your favorite text editors. We will use the Nano editor in order to open an empty script file by the name of remove_spaces.sh

$ sudo nano remove_spaces.sh

In that empty file, add the following script:

#!/bin/bash
(
IFS=$'\n'
for y in $(ls $1)
do
mv $1/`echo $y | sed 's/ /\\ /g'` $1/`echo "$y" | sed 's/ /_/g'`
done
)

Tip: Instead of typing the whole script into you bash file, you can copy it from here and paste in the Terminal by using the Ctrl+Shift+V, or by using the Paste option from the right-click menu.

This is how your file will look like:

Script to remove spaces in filenames on Linux shell

Now, exit the file through the Ctrl+X shortcut and save the file on the “Save modified buffer?” prompt by typing Y and then hitting Enter.

In order to make this file an executable script, run the following command in your Terminal:

$ sudo chmod +x remove_spaces.sh

Now you are ready to use the script in any of your folders.

Sample files

When I run the script on my "sample" directory in the Downloads folder, I see all the spaces in my file names removed. You can see the result as I list the contents of the file again using the ls command:

Using the script

So, these were the two ways through which you can rename the files so that all spaces in their names are removed. Now any application that you are using will not fail to recognize filenames that contain spaces. Have a look here at how to delete files on Linux.