The Ultimate Guide to Handling Filenames with Special Characters in Linux

We come across files and folder names very regularly. In most of the cases file/folder name are related to the content of the file/folder and starts with number and characters.

Alpha-Numeric file name are pretty common and very widely used, but this is not the case when we have to deal with file/folder name that has special characters in them.

Note: We can have files of any type but for simplicity and easy implementation we will be dealing with text files (.txt), throughout the article.

Most Common File Names in Linux

Examples of the most common file names are:

abc.txt
avi.txt
debian.txt
...

Examples of numeric file names are:

121.txt
3221.txt
674659.txt
...

Examples of Alpha-Numeric file names are:

eg84235.txt
3kf43nl2.txt
2323ddw.txt
...

Examples of file names that have special character and is not very common:

#232.txt
#bkf.txt
#bjsd3469.txt
#121nkfd.txt
-2232.txt
-fbjdew.txt
-gi32kj.txt
--321.txt
--bk34.txt
...

One of the most obvious questions here is – who on earth create/deal with files/folder name having a Hash (#), a semi-colon (;), a dash (-) or any other special character.

I agree with you, that such file names are not common still your shell should not break/give up when you have to deal with any such file names. Also speaking technically every thing be it a folder, driver, or anything else is treated as a file in Linux.

Dashed Filename in Linux

In Linux, filenames that begin with a dash ("-") are often called “dashed filenames” or “hyphenated filenames“. These filenames can sometimes cause issues when working with them because the leading dash can be misinterpreted as an option or flagged by command-line utilities.

Create Dashed File in Linux

To work with dashed filenames in Linux, first, create a file that starts with a dash (-), say -abx.txt using the touch command.

$ touch -abc.txt

Sample Output:

touch: invalid option -- 'b'
Try 'touch --help' for more information.

The reason for the above error is that the shell interprets anything after a dash (-), as an option, and obviously, there is no such option, hence the error.

To resolve such an error, we have to tell the bash shell not to interpret anything after the special character (here dash), as an option.

There are two ways to resolve this error as:

$ touch -- -abc.txt		[Option #1]
$ touch ./-abc.txt		[Option #2]

You may verify the file thus created by both the above ways by running commands ls or ls -l for long listing.

$ ls -l

total 0
.rw-rw-r-- tecmint tecmint   0 B  Tue Jun 20 10:32:43 2023 -abc.txt
Create Dashed File in Linux
Create Dashed File in Linux

Edit Dashed File in Linux

To edit a file with a dashed filename, you can use various text editors available. Here’s an example using the nano text editor:

$ nano -- -abc.txt 
or 
$ nano ./-abc.txt 

You may replace nano with any other editor of your choice, for example, use vim editor as shown:

$ vim -- -abc.txt 
or 
$ vim ./-abc.txt 

Rename Dashed File in Linux

To rename a file with a dashed filename, you can use the mv command as shown.

For example, to rename a file named “-abc.txt” to “-a.txt“, you would use:

$ mv -- -abc.txt -a.txt
OR
mv ./-abc.txt ./-a.txt

Delete Dashed File in Linux

To delete a file with a dashed filename, you can use the rm command as shown.

$ rm -- -abc.txt
OR
$ rm ./-abc.txt 

If you have lots of files in a folder the name of which contains a dash, and you want to delete all of them at once, do as:

$ rm ./-*

The same rule as discussed above follows for any number of hyphens in the name of the file and their occurrence. Viz., -a-b-c.txt, ab-c.txt, abc-.txt, etc.

The same rule as discussed above follows for the name of the folder having any number of hyphen and their occurrence, except the fact that for deleting the folder you have to use ‘rm -rf‘ as:

$ rm -rf -- -abc
or
$ rm -rf ./-abc

Hashed Filename in Linux

In Linux, the "#" character is not restricted or reserved for any specific purpose in filenames. You can use the "#" character like any other alphanumeric character in a filename.

However, it’s worth mentioning that some special characters, including the "#", have special meanings in certain contexts or when used with specific commands or utilities.

For example, the "#" character is commonly used in shell scripting to indicate comments. If you’re writing a shell script and want to include the "#" character in a filename, it’s advisable to properly escape or quote the filename to avoid any unintended interpretation as a comment.

Create a Hash File in Linux

Here’s an example of creating a file with the "#" character in its filename:

$ touch #abc.txt

Sample Output:

touch: missing file operand
Try 'touch --help' for more information.

The reason for the above error is that bash is interpreting #abc.txt as a comment and hence ignoring it. So the touch command has been passed without any file operand and hence is the error.

To resolve such an error, you may ask bash not to interpret # as a comment.

$ touch ./#abc.txt
or
$ touch '#abc.txt'

and verify the file just created as:

$ ls -l

.rw-rw-r-- tecmint tecmint 0 B Tue Jun 20 11:11:00 2023 #abc.txt
Create Hashed File in Linux
Create Hashed File in Linux

Now create a file the name of which contains # anywhere except at the begging.

$ touch ./a#bc.txt
$ touch ./abc#.txt

or
$ touch 'a#bc.txt'
$ touch 'abc#.txt'

What happens when you create two files (say a and #bc) at once:

$ touch a.txt #bc.txt

Obviously from the above example it only created file ‘a‘ and file ‘#bc‘ has been ignored. To execute the above situation successfully we can do,

$ touch a.txt ./#bc.txt
or
$ touch a.txt '#bc.txt'

Finally, verify the file just created using the ls command.

$ ls -l
Verify Hash Files in Linux
Verify Hash Files in Linux

Rename or Copy Hash File in Linux

To rename a hash file, you can use the mv command as shown.

$ mv ./#bc.txt ./#cd.txt
or
$ mv '#bc.txt' '#cd.txt'

To copy a hash file, you can use the cp command as shown.

$ cp ./#cd.txt ./#de.txt
or
$ cp '#cd.txt' '#de.txt'

Edit or Delete Hash File in Linux

To edit a hash file, you can use the nano or vim editor as shown.

$ vi ./#cd.txt
or
$ vi '#cd.txt'
$ nano ./#cd.txt
or
$ nano '#cd.txt'

To delete a hash file, you can use the rm command as shown.

$ rm ./#bc.txt 
or
$ rm '#bc.txt'

To delete all the files that have hash (#) in the file name, you may use:

$ rm ./#*

Semicolon Filename in Linux

In case you are not aware, the semicolon acts as a command separator in BASH and perhaps another shell as well. Semicolon lets you execute several commands in one go and acts as a separator. Have you ever dealt with any file name having a semicolon in it? If not here you will.

Create a file having a semi-colon in it.

$ touch ;abc.txt

Sample Output:

touch: missing file operand
Try 'touch --help' for more information.
bash: abc.txt: command not found

The reason for the above error is that when you run the above command BASH interprets touch as a command but could not find any file operand before the semicolon and hence it reports an error.

It also reports another error that the ‘abc.txt‘ command was not found, only because after the semicolon BASH was expecting another command, and ‘abc.txt‘, is not a command.

To resolve such an error, tell BASH not to interpret semicolon as a command separator, as:

$ touch ./';abc.txt'
or
$ touch ';abc.txt'
Note: We have enclosed the file name with a single quote ''. It tells BASH that ; is a part of the file name and not a command separator.

The rest of the action (viz., copy, move, delete) on the file and folder having a semicolon in its name can be carried out straightforwardly by enclosing the name in a single quote.

Special Characters in Filenames in Linux

In Linux, filenames can contain most special characters, including spaces, dots, dashes, underscores, and even symbols such as @, !, $, and %.

Here is a list of special characters that you should use with caution or consider escaping when using them in filenames:

Plus Sign (+) in Filename

To create a plus sign (+) file in Linux, you can use the touch command along with the filename enclosed in single quotes.

$ touch '+ravi.txt' 

Dollar Sign ($) in Filename

To create a dollar sign ($) file in Linux, you can use the touch command along with the filename enclosed in single quotes.

$ touch '$ravi.txt' 

Percent (%) in Filename

To create a percent sign (%) file in Linux, you can use.

$ touch '%ravi.txt' 

Asterisk (*) in Filename

To create a asterisk sign (*) file in Linux, you can use.

$ touch '*ravi.txt' 
Note: When you have to delete a file that starts with *, never use the following commands to delete such files.
$ rm *
or
$ rm -rf *

Instead, use,

$ rm ./*.txt

Exclamation Mark (!) in Filename

Just Enclose the file name in the single quote and the rest of the things are the same.

$ touch '!ravi.txt'

At Sign (@) in Filename

Nothing extra, treat a filename having @ sign as a normal file.

$ touch '@ravi.txt'

Caret Sign (^) in Filename

No extra attention is required, use a file having ^ in the filename as a normal file.

$ touch '^ravi.txt'

Ampersand Sign (&) in Filename

The filename should be enclosed in single quotes and you are ready to go.

$ touch '&ravi.txt'

Parentheses () in Filename

If the file name has parenthesis, you need to enclose the filename with single quotes.

$ touch '(ravi.txt)'

Braces Sign ({}) in Filename

No Extra Care is needed. Just treat it as just another file.

$ touch {ravi.txt}

Chevrons (<>) in Filename

A file name having chevrons must be enclosed in single quotes.

$ touch '<ravi.txt>'

Square Brackets ([]) in Filename

Treat file names having square brackets as normal files and you need not take extra care of it.

$ touch [ravi.txt]

Under Score (_) in Filename

They are very common and don’t require anything extra. Just do what you would have done with a normal file.

$ touch _ravi.txt

Equal-to (=) in Filename

Having an equal-to sign does not change anything, you can use it as a normal file.

$ touch =ravi.txt

Backslash (\) in Filename

Backslash tells the shell to ignore the next character. You have to enclose the file name in a single quote, as we did in the case of the semicolon. The rest of the things are straightforward.

$ touch '\ravi.txt'

Forward (\) in Filename

You cannot create a file the name of which includes a forward slash (/), until your file system has the bug. There is no way to escape a forward slash.

So if you can create a file such as ‘/ravi.txt’ or ‘b/c.txt’ then either your File System has a bug or you have Unicode support, which lets you create a file with a forward slash. In this case, the forward slash is not a real forward slash but a Unicode character that looks like a forward slash.

Question Mark (?) in Filename

Again, an example where you don’t need to put in any special attempt. A file name having a Question mark can be treated in the most general way.

$ touch '?ravi.txt'

Dot Mark (.) in Filename

The files starting with a dot (.) are very special in Linux and are called dotfiles. They are hidden files generally configuration or system files. You have to use switch ‘-a‘ or ‘-A‘ with the ls command to view such files.

Creating, editing, renaming, and deleting such files is straightforward.

$ touch '.ravi.txt'

Note: In Linux, you may have as many dots (.) as you need in a file name. Unlike other system dots in the file, names don’t mean to separate names and extension. You can create a file having multiple dots as:

$ touch 1.2.3.4.5.6.7.8.9.10.txt

and check it as:

$ ls -l

total 0
-rw-r--r-- 1 avi avi 0 Jun  8 14:32 1.2.3.4.5.6.7.8.9.10.txt

Comma (,) in Filename

You can have a comma in a file name, as many as you want and you Don’t require anything extra. Just do it the normal way, as the simple file name.

$ touch ',ravi.txt'
or
$ touch ',ravi,.txt'

Colon (:) in Filename

You can have a colon in a file name, as many as you want and you Don’t require anything extra. Just do it the normal way, as the simple file name.

$ touch :12.txt
or
$ touch ':ravi:.txt'

Double Quotes (“”) in Filename

To have quotes in the file name, we have to use the rule of exchange. I.e, if you need to have a single quote in the file name, enclose the file name with double quotes and if you need to have a double quote in the file name, enclose it with a single quote.

$ touch "15'.txt"

and

$ touch 'ravi”.txt'

Tilde (~) in Filename

Some Editors in Linux like emacs create a backup file of the file being edited. The backup file has the name of the original file plus a tilde at the end of the file name. You can have a file the name of which includes a tilde, at any location simply as:

$ touch '~ravi.txt'
or
$touch 'anusha~.txt'

White Space in Filename

Create a file with the name which has space between characters/words, say “hi my name is ravi.txt”.

It is not a good idea to have file names with spaces and if you have to distinct readable name, you should use, an underscore or dash. However, if you have to create such a file, you have to use a backward slash that ignores the next character to it. To create the above file we have to do it this way..

$ touch hi\ my\ name\ is\ ravi.txt

hi my name is ravi.txt

I have tried covering all the scenarios you may come across. Most of the above implementations are explicitly for BASH Shell and may not work in other shells.

If you feel that I missed something (that is very common and human nature), you may include your suggestion in the comments below. Keep Connected, and Keep Commenting. Stay Tuned and connected! Like and share us and help us get spread!

Ravi Saive
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

12 thoughts on “The Ultimate Guide to Handling Filenames with Special Characters in Linux”

  1. “You can have a colon in a file name, as many as you want and you Don’t require anything extra.”

    There may be no problems creating a file name with a colon but there may be problems manipulating the file.

    Using MX Linux (ver. 19.4) I have no problems creating a file with a colon in its name. However, when I try to copy it to another device or another directory, I get an “Invalid file name” error. To make the file name ‘valid’, I have to take out the colon.

    Reply
  2. I have a file name ~$S123.docx in AIX when I run below command it prompts no file found

    ls -l ~$S123.docx 
    ls -l "~$S123.docx"
    ls -l "S123.docx"
    ls -l "$S123.docs"
    

    Please help

    Reply
  3. Hi Dear,

    I’m creating the shell script needs to copy files. This script has three parameters, the first parameter is the file.txt that contains list of files, 2nd parameter is location of the files and last parameter is destination path.

    I’m facing problem when I have to copy files whose filename has white space.

    Can you help me with that?

    Reply
  4. Regarding the terminal argument ” touch *12.txt ” may open up more than one file if you have a file named a12.txt , b12.txt etc in a text editor like Geany or the first file in alphabetical order in Nano .

    Reply
  5. We can eliminate this issue by passing the file name is single quote to solve the special characters present in the file.

    Eg: touch ‘₹@@%%@’

    Reply
  6. Hi there i have a lot of files with [!] in their namefiles , i need move just that files inside another sub-folder , i try with

    for file in $(ls | grep -e “.[!]]” | awk ‘NF { print “\””$0″\””}’); do mv -f “$PWD/$file” “$PWD/Folder001/” ; done

    but is imposible , because bash in “do mv -f “$PWD/$file”
    reads the “This is an example of large filename.ext” like

    This -> File1
    is -> File2
    an -> File 3
    example -> File 4
    of -> File 5
    large -> File 6
    filename.ext -> File 7

    How i can use “mv” for large filename with special characters like [!](EJU) to another folder ???

    Reply
    • You can use a single quote with any character to copy and move anywhere like this. The file name does not matter whether it is long or short, USE a single quote

      [root@ip-172-31-2-191 test]# touch '[!](EJU)#_()'
      [root@ip-172-31-2-191 test]# ls -l
      -rw-r--r-- 1 root root 0 Jun 10 15:51 [!](EJU)#_()
      [root@ip-172-31-2-191 test]# mv '[!](EJU)#_()' /root
      [root@ip-172-31-2-191 test]# ls -l /root
      total 0
      -rw-r--r-- 1 root root  0 Jun 10 15:51 [!](EJU)#_()
      
      Reply
  7. it’s easy to create such files, “” will resolve all the situation, but it’s hard to process them in your script or command.
    the most ugly file name I have seen it’s “xxx “, since when i list it in terminal, I cannot know how many spaces the name inlcuded. but if it’s in script, it will have no problem to get the name through API

    Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.