Batch remove spaces in file names via Ubuntu command line

When working with Linux, you may encounter some utilities and applications that work only with file names that do not contain spaces. We do not always save files in this format “without spaces” and you may have to look for a workaround that removes spaces in file names in general. Thus, your file names will not contain spaces, and you can easily work with them in all your applications.

In this article, we will explain two ways to remove spaces from file names, very simply through the Ubuntu command line.

We ran the commands and procedures mentioned in this article on the Ubuntu 18.04 LTS system.

Method 1: using the mv command in the for loop

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

Open the Ubuntu command line, Terminal, either by searching in the Application Launcher, or by using the keyboard shortcut Ctrl + Alt + T.

This is what the files in my examples folder look like; I have listed the contents with the ls command:

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

This is the command that 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

Batch remove spaces in file names via Ubuntu command line

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

Batch remove spaces in file names via Ubuntu command line

Method 2. Using bash script to rename files

In this method, we will use the bash script, which uses the mv command to rename file and folder names so that all spaces are removed.

Open a Terminal application and navigate 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 to open an empty script file called remove_spaces.sh

$ sudo nano remove_spaces.sh

Add the following script to this empty file:

#!/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 entering the entire script into your bash file, you can copy it from here and paste it into the terminal using Ctrl + Shift + V or using the “Paste” option in the context menu.

Here’s what your file will look like:

Script to remove spaces in file names in Linux shell

Now exit the file using the keyboard shortcut Ctrl + X and save the file in the “Save modified buffer?” Window by typing Y, and then press Enter.

To make this file an executable script, run the following command in the 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 in my “sample” folder in the “Downloads” folder, I see all the spaces in the names of my files. You can see the result when I list the contents of the file again with the ls command:

Using a script

Thus, these were two ways to rename files to remove all spaces in their names. Now, any application that you use will not be able to recognize file names that contain spaces.

Batch remove spaces in file names via Ubuntu command line

Sidebar