1. Overview

Renaming files is a common task in Linux, but it can be challenging when the filenames contain special characters such as quotes. Quotes are often used to enclose strings or commands in the shell, so we must handle them properly to avoid syntax errors or unexpected results.

In this tutorial, we’ll learn how to rename files with a quote in the filename in Linux.

The commands used in this tutorial were tested in Bash shell version 5.1.16.

2. Using the mv Command

The mv command enables us to move or rename files and directories in Linux with the syntax:

mv [options] [source] [destination]

If the destination is a directory, the source file is moved to that directory. If the destination is another filename, the source file is renamed to that file. Moreover, options allow us to use different options with the mv command.

2.1. Using the mv Command With Escape Character

One way we can rename files with a quote in the filename is to use the escape character, which is \ (backslash). The escape character tells the shell to treat the next character as a literal character, not as a special character.

For example, if we have a file named back’pack.pdf, we can rename it to backpack.pdf:

$ mv back\'pack.pdf backpack.pdf

The backslash before the quote tells the shell to treat the quote as part of the filename, not as a string delimiter. Now, we’ve changed the file name from back’pack.pdf to backpack.pdf.

Moreover, we can also rename filenames with more than one quote in their name:

$ mv ba\'ck\'pa\'ck.pdf backpack.pdf

Here, we renamed the file form ba’ck’pa’ck.pdf to backpack.pdf.

Similarly, we can also rename the file backpack.pdf to back’pack.pdf:

$ mv backpack.pdf back\'pack.pdf

2.2. Using the mv Command With Single Quotes

Another way we can rename files with a quote in the filename is to use the single quote (). However, we use single quotes when there’s a double quote in the filename. The single quote tells the shell to treat everything inside the quotes as a literal string, ignoring any special characters.

For example, if we have a file named back”pack.pdf, we can rename it to backpack.pdf:

$ mv 'back"pack.pdf' backpack.pdf

The single quotes around the filename tell the shell to treat the whole filename as a literal string, not as a special character. The filename also changes to backpack.pdf.

Similarly, we can also rename the file backpack.pdf to back”pack.pdf:

$ mv backpack.pdf 'back"pack.pdf'

2.3. Using the mv Command With Double Quotes

We can also rename files with a single quote in between with double quotes around the filename:

$ mv "back'pack.pdf" backpack.pdf

The double quote around the filename tells the mv command to treat everything inside the double quotes as a literal string and rename the file, except for some special characters such as the dollar sign ($), the backtick (`), and the backlash (\).

Notably, these special characters are still interpreted by the shell, so we need to escape them with backlash:

$ mv "back\$pack" backpack

Here, we use the double quote to tell the shell to treat the filename as a literal string and \ to escape $ to enable us to rename the file.

3. Using the rename Command

Another method we can use to rename files in Linux is the rename command. The rename command is a powerful tool that we can use to rename multiple files at once using regular expressions. Regular expressions are patterns that can be used to match and manipulate strings.

However, for us to be able to use the rename command, we have to install the command:

$ sudo apt install rename

In the code snippet above, we use the sudo command to install the rename command:

  • sudo command allows us to run another command as a root or superuser with administrative privilege
  • apt enables us to interact with the Advanced Packaging Tool (APT) and handle dependencies and sources of the package
  • install option allows us to install any other dependencies the package needs
  • rename allows us to rename files

After we run the code, a prompt shows up for us to input the system password, and we can now use the command.

The rename command follows the syntax:

rename [options] [expression] [files]

The expression is a Perl expression that modifies the file name. The files are files that match the expression.

3.1. Rename a Single File

If we want to rename files with a quote in the filename, we can use the s operator to substitute the quote with another character. For example, if we have a file named back’pack.pdf, we can rename it to backpack.pdf:

$ rename "s/back'pack/backpack/" "back'pack.pdf"

In the code snippet above, s stands for substitute, and the / is the delimiter that separates the expression and replacement. back’pack is the pattern that matches the string back’pack in the filename, and backpack is the new string that replaces it. Therefore, the rename command renames the file back’pack.pdf to backpack.pdf by substituting back’pack with backpack.

3.2. Rename Multiple Files

In addition, we might want to rename more than one file with a quote in its filename. We can do this with one rename command.

For example, if we have files named back’pack.pdf, back’pack_notes.pdf, and back’pack_articles.pdf, we can rename them to backpack.pdf, backpack_notes.pdf, and backpack_articles.pdf:

$ rename "s/'//" *.pdf

In the code snippet above, the expression “s/’//” means to substitute every occurrence of the single quote with nothing. The files *.pdf mean to match every file that ends with .pdf. Also, the double quotes around the expression prevent the shell from interpreting the single quote as a special character. Here, we renamed three files with quotes in their filename together.

4. Conclusion

In this article, we learned how we can rename files with a quote in their filename. First, we looked at the mv command and used the mv command with escape characters, single quotes, and double quotes.

Then, we looked at the rename command and how we can use it to rename single files and multiple files with a quote in their filename.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.