Almost two decades ago I wrote an article explaining how to work with files that contain spaces and special characters. That article has been viewed over a 250k times. Anyone new to the Linux command line (especially those coming from Windows) will eventually come across files whose name make it hard to work with on the command line. In this article we are going to approach the problem from a different angle. Instead of trying to work around the spaces and special characters with escaping and quoting, we are going to remove the trouble characters. Of course there is a utility for that... Enter the detox command.

The detox command was specifically designed to deal with these common problems. Here we will show you how to install the detox utility and some of the basics. Let's get started.

How to Install the Detox Command

The detox command may already be installed on your system. Simply type the command to check if it is already installed.

Linux detox command line utility

The above shows us it is not installed on my Fedora system. In this case we can simply install it using dnf like so:

[savona@putor ]$ sudo dnf install detox -y
Last metadata expiration check: 2:31:27 ago on Thu 07 Nov 2024 01:54:10 PM EST.
Dependencies resolved.
===================================================================================================================================================================================
 Package                                 Architecture                             Version                                           Repository                                Size
===================================================================================================================================================================================
Installing:
 detox                                   x86_64                                   1.4.5-7.fc40                                      fedora                                    41 k

Transaction Summary
===================================================================================================================================================================================
Install  1 Package

Total download size: 41 k
Installed size: 142 k
Downloading Packages:
detox-1.4.5-7.fc40.x86_64.rpm                                                                                                                      361 kB/s |  41 kB     00:00    
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                              106 kB/s |  41 kB     00:00     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                                                           1/1 
  Installing       : detox-1.4.5-7.fc40.x86_64                                                                                                                                 1/1 
  Running scriptlet: detox-1.4.5-7.fc40.x86_64                                                                                                                                 1/1 

Installed:
  detox-1.4.5-7.fc40.x86_64                                                                                                                                                        

Complete!

In Ubuntu is should be equally as easy, although you may have to add a repo first:

sudo add-apt-repository universe
sudo apt update
sudo apt install detox

Use detox to Clean Up Filenames with Spaces and Special Characters

Using detox to clean up filenames with spaces and special characters is simple. You can simply call the utility and give it a filename as a argument.

NOTE: Using detox will rename the file instantly. I recommend using the -n option first to do a dry run to ensure you receive the expected results.

Here we have 2 files, one with spaces and one with spaces and a single quote. Both of these can create problems for beginners.

[savona@putor ]$ ls -lrt
total 0
-rw-r--r--. 1 savona savona 0 Nov  7 16:04 'This is a file with spaces.txt'
-rw-r--r--. 1 savona savona 0 Nov  7 16:05 "This file has a ' single quote"
[savona@putor ]$ 

We will pass one of the files to detox (using the dry run method) to see what happens.

[savona@putor ]$ detox -n "This file has a ' single quote"
This file has a ' single quote -> This_file_has_a_single_quote
[savona@putor ]$ 

The output is showing us how it would have renamed the file. In this case it removes the single quote and replaces all the spaces with underscores. This new filename is much easier to work with on the command line, especially when looping or passing it to some kind of script.

That's it! The detox utility makes it easy to clean up these filenames. Let's pass a standard wildcard to have detox rename all the files in the directory. We will also pass the -v option for verbose output.

[savona@putor ]$ detox -v *
Scanning: This file has a ' single quote
This file has a ' single quote -> This_file_has_a_single_quote
Scanning: This is a file with spaces.txt
This is a file with spaces.txt -> This_is_a_file_with_spaces.txt
[savona@putor ]$ ls -l
total 0
-rw-r--r--. 1 savona savona 0 Nov  7 16:05 This_file_has_a_single_quote
-rw-r--r--. 1 savona savona 0 Nov  7 16:04 This_is_a_file_with_spaces.txt
[savona@putor ]$ 

You can also have it run recursively into all sub-directories by using the -r option. Again, you might want to do a dry run with that option to ensure you are getting the expected output.

That it! For more information on the detox utility see the reference links below.

Resources and Links