Remove single quote from a file name in Bash

A file name with a single quote in its name can cause a lot of problems when used with a Bash script or used on the command line. Single quotes are special characters and instruct the Bash shell to interpret the text between them literally. Most users would, ideally, prefer to work with files that do not have quotes in the file name.

Manipulating files without quotes is much easier, as you do not have to worry about escaping the file name each time. In this tutorial, you will see how to remove single quotes from file names using various methods in Linux.

In this tutorial you will learn:

  • Various commands used to remove or substitute single quotes in file names
Remove single quote from a file name in Bash
Remove single quote from a file name in Bash
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Bash shell, ls, grep, mv, echo, tr, rename, mmv, 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

Remove single quote from a file name in Bash




All of the following examples will take place on the Linux command line. Open a terminal and navigate to the directory which contains the files you want to rename, then try out some of the commands below to get started.

  1. To remove the single quotes in an individual file and substitute underscores in their place, you can use the following command:
    $ myfile="some'file.txt" ; mv "$myfile" $(echo $myfile | tr "'" '_')
    
  2. To remove the single quotes completely, we can use the same command above but use tr and the -d option to the quotes:
    $ myfile="some'file.txt" ; mv "$myfile" $(echo $myfile | tr -d "'")
    
  3. Here’s another command that can remove the single quotes from all files within a directory:
    $ ls | grep "'" | while read -r f; do mv -i "$f" `echo $f | tr -d "'"`; done
    
  4. The same command as above, but this time it will replace the single quotes with underscores:
    $ ls | grep "'" | while read -r f; do mv "$f" `echo $f | tr "'" '_'`; done
    
  5. The detox command will clean up all kinds of strange characters in file names. One of its default functions is to replace single quotes with underscores.
    $ detox some\'file.txt
    OR
    $ detox *
    

    To see detox installation and more usage instructions, see our tutorial on Clean up filenames with detox command line utility.

  6. If you have the rename utility installed on your Linux system, there is a very easy syntax to remove the single quotes from all files:
    $ rename "s/\'//g" *
    
  7. Anoter utility which is not ordinarily installed by default on Linux, but can do the job very well, is mmv. The following command will remove all single quotes and replace them with underscores:
    $ mmv "*'*" '#1_#2'
    


NOTE
You may also be interested in our tutorial on How to rename multiple files on Linux where we show more examples of batch file renaming, as well as how to install the rename and mmv commands.

Closing Thoughts

In this tutorial, we saw how to remove the single quotes from files on a Linux system. Files without single quotes are easier to work with, as they have have compatibility with more tools on Linux, and we do not have to remember to escape the file names any time we are working with the files. Whether you have a handful or a few thousand of files to rename, the commands in this tutorial will get the renaming task done in no time.