Clean up filenames with detox command line utility

If you have spent much time on the command line to work with files in Linux, then you probably know about the pains of dealing with file names that contain spaces or any other strange characters. It can be tedious to escape certain file names or to work with a bunch of files that have inconsistent encoding in their file names. The detox command is a solution to this problem, as it converts all file names to a consistent format that make them easier to work with.

In this tutorial, you will see how to install the detox command line utility on all major Linux distros. Then, we will show you how to get started using the detox command through usage examples. This is a great tool to use if you import files from other operating systems or download lots of files online. It will sanitize your file names so they follow a uniform naming format and are easy to work with on Linux and in the command line.

In this tutorial you will learn:

  • How to install detox on major Linux distros
  • How to use the detox command through usage examples
Clean up filenames with detox command line utility
Clean up filenames with detox command line utility
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software detox
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to install detox



Twitter icon Follow LinuxConfig.org on Twitter for the latest tips and tricks about Linux!


You can use the appropriate command below to install detox with your system’s package manager.

To install detox on Ubuntu, Debian, and Linux Mint:

$ sudo apt install detox

To install detox on Fedora, CentOS, AlmaLinux, and Red Hat:

$ sudo dnf install detox

To install detox on Arch Linux and Manjaro:

$ sudo pacman -S detox

detox usage examples

Now that detox has been installed, let’s see how to use the command in order to clean up file names on Linux.

NOTE
By default, the detox command will remove spaces by replacing them with underscores, convert file names from utf8 encoding, remove escaped CGI characters, clean up Latin-1 (ISO 8859-1) characters, clean up names encoded in 8-bit ASCII characters, remove special characters like ampersands and others, etc.
  1. The most basic way to use the detox command is to specify the files that you want to clean up. Keep in mind that detox will also rename directories.
    $ detox file.txt
    

    Or multiple files…

    $ detox file*.txt
    OR
    $ detox file1.txt file2.txt file3.txt
    OR
    $ detox *
    
  2. Before running the detox command on a bunch of files, it would be wise to use the -n (dry run) option first. This allows you to get a preview of the changes that detox plans to make. If you like the preview version, then you can remove the -n option and run the command again.
    $ detox -n file\ name.txt 
    file name.txt -> file_name.txt
    



  3. Another handy option is the -v (verbose) flag. This shows you what changes detox is making to your file names. Without this option, the operation will not produce any output unless an error occurs.
    $ detox -v *
    
  4. To use detox recursively, add the -r option. This will clean up file and directory names for all subdirectories as well as all the files they contain. Be careful if using this on a big file tree, as editing important system file names could cause damage to your installation.
    $ detox -r *
    
  5. In case you need a quick refresher, all of detox’s most common options can be viewed at any time with the -h (help) option.
    $ detox -h
    
  6. detox works by using sequences. These are basically renaming rules that it can use. To see which ones are available, use the -L option.
    $ detox -L
    
  7. To use a specific detox sequence rather than the default, specify it with the -s option.
    $ detox -s iso8859_1 myfiles/
    

Closing Thoughts




In this tutorial, we saw how to use the detox command to clean up file names on a Linux system. This utility makes a Linux user’s life much easier, as they do not have to worry about incompatible file names, escaping characters, etc.



Comments and Discussions
Linux Forum