Replace filenames with a space with underscores
From
Black Panther@77:1/102 to
All on Mon Aug 26 15:53:22 2019
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 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
sana@hp-probook:~/Downloads$ ls
'File Name1.docx' 'File Name2.docx' 'File Name3.pdf' 'File Name4.docx'
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:
sana@hp-probook:~/Downloads$ for file in *; do mv "$file" `echo $file | tr ' ' '_'` ; done
sana@hp-probook:~/Downloads$ ls
File_Name1.docx File_Name2.docx File_Name3.pdf File_Name4.docx sana@hp-probook:~/Downloads$
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
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.
---
|03B|09lack |03P|09anther|03(|09RCS|03)|07
--- Mystic BBS v1.12 A43 2019/03/02 (Linux/64)
* Origin: Castle Rock BBS - bbs.castlerockbbs.com - (77:1/102)