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 18.04 LTS system.

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

$ ls Downloads

File list

I will be using 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 be making use of the Ubuntu mv command in a for loop in order to rename all files/folders in a given directory so that all spaces in their names are replaced with underscore characters.

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 contents of the directory 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 in order 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 replace_spaces.sh

$ sudo nano replace_spaces.sh

In that empty file, add the following script:

#!/bin/bash

for f in *
do
  new="${f// /_}"
  if [ "$new" != "$f" ]
  then
    if [ -e "$new" ]
    then
      echo not renaming \""$f"\" because \""$new"\" already exists
    else
      echo moving "$f" to "$new"
    mv "$f" "$new"
  fi
fi
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:

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.

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

$ sudo chmod +x replace_spaces.sh

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.

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