How to Replace Spaces in Filenames with Underscores on the Linux Shell

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

In this article, we will explain two ways for you to convert all spaces in filenames to underscores very simply through the command line.

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

My Downloads folder, which I will use as a sample for this article, contains spaces in all filenames.

$ ls Downloads

File list

I will use this folder to explain how I convert the filenames to a new format.

Method 1: Through a single mv command

In this method, we will use the Ubuntu mv command in a for loop to rename all files/folders in a given directory so that all spaces in their names are replaced with underscore characters and use echo to show progress output.

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

Here is the syntax of the command you will be using:

$ for file in *; do mv "$file" `echo $file | tr ' ' '_'` ; done

I ran the same command to replace spaces with underscores in my Downloads folder:

Replace whitespace with underscore command

When I listed the directory’s contents again, you can see that all file names now contain underscores instead of spaces.

Method 2: Using a script to rename files

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

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

$ 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

$ sudo nano

In that empty file, add the following script:


for f in *
  new="${f// /_}"
  if [ "$new" != "$f" ]
    if [ -e "$new" ]
      echo not renaming \""$f"\" because \""$new"\" already exists
      echo moving "$f" to "$new"
    mv "$f" "$new"

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:

How to Replace Spaces in Filenames with Underscores on the 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.

To make this file an executable script, run the following command in your Terminal:

$ sudo chmod +x

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

When I run the script in my sample Downloads folder, I see all the spaces in my file names converted to underscores as follows:

Run the shell script

So, these were the two ways through which you can rename the files so that all spaces in their names are converted to underscores. Now any application that you are using will not fail to recognize filenames that contain spaces.