in

How to Handle Filenames Contains a Special Symbols, Characters and Spaces?

How to handle filenames with special characters

Hello friends, This article is about how to handle the folders, files contain a special symbol, characters, and spaces in its name. Yeah everyone knows the way to create a file or folder and naming it. We usually follow the 3 types of naming pattern and they are:

  • Alphabetical names
  • Alphanumerical names
  • Numerical names

These are the basic formats we will follow while creating and name a new file or naming the existing file. But here, the scenario is to create names using special characters, symbols and spaces. Using special character is not possible in Windows but here in Linux, everything is possible in here.

Alphabetical Names:

  • word.txt
  • dummy.txt
  • file.txt

AlphaNumerical Names:

  • word123.txt
  • lov2432.txt
  • card9878.txt

Numerical Names:

  • 123.txt
  • 456.txt
  • 789.txt

Literally, it is not possible to use the #, & ; in the filenames. Not only these three, But It also is not possible to use any type of special character in the filenames. But this is possible in Linux that is because the Linux will see each and every folder as the simple file and so the naming is possible in Linux.

Jump to:

Handling files having #tags in their names:

This Hashtag # has a different working principle in the “bash“. Here the # tags are referred to the “comment” and so it will be neglected.

I think the example of this command will give you more information about this # tags:

$ touch tecrobust.txt

How to use? Copy the above code and paste in terminal

Here the “touch” command is mainly used to create the directory in Linux. By implementing this command with # tag the result will be like the below image.

Touch error
Touch error
touch: missing file operand

Try ‘touch –help’ for more information.

The touch command will throw the error to you and this is because that the “bash” will eliminate the # tag and so the touch command has been executed without file operand or considered as the empty command. Ultimately it throws the error.

To fix this issue, you need to make the bash not to interpret the # tag as the comment. To do this you need to use the following commands.

$ touch ./#abc.txt
$ touch ‘#abc.txt’

How to use? Copy the command and paste in terminal !!

In the first command, The “/” character help the “#” tag and so the command will execute normally.

In the second command, The ‘ ‘ will declare the bash to not to interpret the following file and so the command will execute normally.

After implementing the above command, if you want to verify that the above command is worked or not. Then follow the below command to verify:

$ ls -l

How to use? Copy the command and paste in terminal !!

by executing the above command the output will be:

total 0
-rw-r–r– 1 avi avi 18 Oct 25 02:00 #tecrobust.txt

How to use? Copy the command and paste in terminal !!

Create a filename with # tags:

The below command will help you to create the file have a filename with # tag.

$ touch ./tecrobust##.txt

$ touch ./#tecrobust#.txt

How to use? Copy the command and paste in terminal !!

The above command will create the file having the filename contains # tag in it. How to verify the above command is worked or not and then you need to execute the below command.

$ ls -l                               -> It will verify the command.

Check this too  The Easy Hacks In Linux Using Command Line Codes, Command Line Hacks

How to use? Copy the command and paste in terminal !!

The output of the above command will be like the below note.

total 0

-rw-r–r– 1 avi avi 18 Oct 25 02:00 tecrobust##.txt

-rw-r–r– 1 avi avi 18 Oct 25 02:00 #tecrobust#.txt

When you tried to create more than one file. In which, one file contains # tag and another contains no # tag try to execute the filenames using the “touch” command and the result will be like this.

$ touch tec.txt #robust.txt
$ ls -l -> Verification command
The output of the command is
-rw-r–r– 1 avi avi 18 Oct 25 02:00 tec.txt

How to use? Copy the command and paste in terminal !!

The result will show that the “tec.txt” has been executed successfully but not the “#robust.txt”. Yes, bash interprets the # tag as the comment and so this command executed only the “tec.txt”. To solve this problem, you need to add the “/” character along with the #tag in the command.

$ touch tec.txt _/#robust.txt

or
$ touch tec.txt ‘#robust.txt’

The output of the command is
-rw-r–r– 1 avi avi 18 Oct 25 02:00 tec.txt

-rw-r–r– 1 avi avi 18 Oct 25 02:00 #robust.txt

How to use? Copy the command and paste in terminal !!

You can Copy, Move, Edit & Remove the files having filenames contains # tag:

To Copy the file using filenames contain #tag in it and then follow the below command.

$ cp ./#tec.txt ./#robust.txt
or
$ cp ‘#tec.txt’ ‘#robust.txt’

How to use? Copy the command and paste in terminal !!

Move the file using the filenames contain #tag in it and then follow the below command.

$ mv ./#tec.txt ./#robust.txt
or
$ mv ‘#tec.txt’ ‘#robust.txt’

How to use? Copy the command and paste in terminal !!

To Edit the files having #tag in the filename in it and then you need to follow the below command.

$ nano ./#tec.txt ./#robust.txt
or
$ nano ‘#tec.txt’ ‘#robust.txt’

How to use? Copy the command and paste in terminal !!

To delete or remove the file using the filename contains #tag in it and then follow the below command.

$ rm ./#tec.txt ./#robust.txt
or
$ rm ‘#tec.txt’ ‘#robust.txt’

How to use? Copy the command and paste in terminal !!

Handling files having (-) Dash in their names:

Try to create the file using “touch” command and the filename contains (-) dash character in it and the output will be the below image.

$ touch -tec.txt
or
$ touch -robust.txt

How to use? Copy the command and paste in terminal !!

Handling - Special Character
Handling (-) Special Character

It will throw the error!!. This is due to that shell interprets anything after a dash (-) and so there is no command to execute. Ultimately it throws the error. To eliminate this error, that we need to instruct the bash to not to interpret the (-) character. It can be done by using the following ways:

$ touch — -tecrobust.txt
or
$ touch ./-tecrobust.txt

How to use? Copy the command and paste in terminal !!

To see the output, you need to verify the above command by using the following command:

$ls -l                                 -> Verifies the command
The Output will be:
total 0
-rw-r–r– 1 avi avi 18 Oct 25 02:00 -tecrobust.txt

How to use? Copy the command and paste in terminal !!

You can Copy, Move, Edit & Remove the files having filenames contains (-) Dash:

To Copy the file using filenames contain #tag in it and then follow the below command.

$ cp — -tecrobust.txt
or
$ cp ./-tecrobust.txt

How to use? Copy the command and paste in terminal !!

Check this too  How to Generate, Encrypt, Decrypt Random Passwords in Linux

Move the file using the filenames contain #tag in it and then follow the below command.

$ mv — -tecrobust.txt
or
$ mv ./-tecrobust.txt

How to use? Copy the command and paste in terminal !!

To Edit the files having #tag in the filename in it and then you need to follow the below command.

$ nano — -tecrobust.txt
or
$ nano — -tecrobust.txt

How to use? Copy the command and paste in terminal !!

To delete or remove the file using the filename contains #tag in it and then follow the below command.

$ rm — -tecrobust.txt
or
$ rm — -tecrobust.txt

How to use? Copy the command and paste in terminal !!

To remove more than one files that contain – in their filenames and then follow the below command.

$ rm ./-*

How to use? Copy the command and paste in terminal !!

The above command will remove all the files contains (-) in the filename. This method can be used, when you have more than one file contain – in its filename and you need to delete those files.

Handling files having (;) Semicolon in their names:

In bash, the semicolon will act as the command separator. If you not aware of this information, then you may end up with an error. This semicolon is mainly used to separate the commands when more than one command executed in a line. How to use the semicolon in the filenames without throwing the error? Follow the below tutorial to implement the method.

Try to create a .txt file having (;) in the filename by using the touch command.

$ touch ;tecrobust.txt
The Output will be:
touch: missing file operand Try ‘touch –help’ for more information. bash: abc.txt: command not found

How to use? Copy the command and paste in terminal !!

Handle ; special character
Handle ; special character

Yeah, The above command using touch will throw the error. This is due to the bash will interpret the “touch” command and it moves to the “;” and it interprets the “tecrbust.txt” as the command and it fails. Yes, using “;” will interpret the command as the separator, Bash will expect another command or condition after the “;” and so it failed to execute. To eliminate this, you need to follow the below command.

$ touch ./’;tecrobust.txt’
or
$ touch ‘;tecrobust.txt’

How to use? Copy the command and paste in terminal !!

You can Copy, Move, Edit & Remove the files having filenames contains (;) Dash:

To Copy the file using filenames contain #tag in it and then follow the below command.

$ cp ‘;tecrobust.txt’
or
$ cp ./’;tecrobust.txt’

How to use? Copy the command and paste in terminal !!

Move the file using the filenames contain #tag in it and then follow the below command.

$ mv ‘;tecrobust.txt’
or
$ mv ./’;tecrobust.txt’

How to use? Copy the command and paste in terminal !!

To Edit the files having #tag in the filename in it and then you need to follow the below command.

$ nano ‘;tecrobust.txt’
or
$ nano ./’;tecrobust.txt’

How to use? Copy the command and paste in terminal !!

To delete or remove the file using the filename contains #tag in it and then follow the below command.

$ rm ‘;tecrobust.txt’
or
$ rm ./’;tecrobust.txt’

How to use? Copy the command and paste in terminal !!

To remove more than one files that contain – in their filenames and then follow the below command.

$ rm ./;*

How to use? Copy the command and paste in terminal !!

The above command will remove all the files contains (-) in the filename. This method can be used, when you have more than one file contain – in its filename and you need to delete those files.

Check this too  50+ Linux Command Sheet List for Newbies!

Handling Other Special Character in Linux:

The below methods will help you to deal with the special characters in Linux.

Spaces in the File name:

Creating the filename with spaces in the Linux is possible and so you need to follow the below commands. Before that, implementing the filenames with spaces will cause you some readability problems and we won’t suggest this method. To implement this method:

$ touch Tecrobust\ is\ the\ best.txt
The output will be like this
Tecrobust is the best.txt

How to use? Copy the command and paste in terminal !!

Using ~ character in the filename:

“Emacs” is one of the Linux editors, which is mainly used to edit the file and backup files are created while editing the file. This editor will create a backup file of the file being edited with its filename and ~ at the end of the filename. You can find the location of the backup file using the ~ character.

$ touch ~tec.txt
or
$touch robust~.txt

How to use? Copy the command and paste in terminal !!

Using ” ‘ Character in the filename:

It is possible to use the Single and Double quotes in the filename. To do this you just need to follow the below steps.

$ touch “tec’.txt”
and
$ touch ‘robust”.txt’

How to use? Copy the command and paste in terminal !!

if you need to have a single quote in the filename, enclose the file name with double quotes and if you need to have the double quote in the file name, enclose it with a single quote.

Using : Colon in the filename:

It is possible to use the colon in the filename and you don’t need to do any new things to add the colon in the filename. Just add the colon in the filename. Simple right !!!

$ touch :tecrobust.txt
or

$ touch tecrobust:.txt

How to use? Copy the command and paste in terminal !!

Using , Comma in the filename:

It is similar to the colon, just add the comma in the filename and simply it will execute to create a filename with the comma in it.

$ touch ,tecrobust.txt

How to use? Copy the command and paste in terminal !!

Using Basic Special Characters in Filenames:

ConditionCommand
+ Plus operator$ touch +tecrobust.txt
% Percentage operator$ touch %tecrobust.txt
$ Dollar operator$ touch ‘$tecrobust.txt’
* Asterisk operator$ touch *tecrobust.txt
! Exclamatory operator$ touch ‘!tecrobust.txt’
@ At the rate of the operator$ touch ‘@tecrobust.txt’
^ Carot operator$ touch ^tecrobust.txt
& Ampersand operator$ touch ‘&tecrobust.txt’
( ) Parenthesis operator$ touch ‘(tecrobust.txt)’
{ } Braces operator$ touch {tecrobust.txt}
< > Chevrons operator$ touch ”<tecrobust.txt>’
[ ] Square Brackets operator$ touch [tecrobust.txt]
_ Underscore operator$ touch _tecrobust.txt
= Equals operator$ touch =tecrobust.txt
\ Backword Slash operator$ touch ‘ .txt’
/ Forward Slash operatorNot possible otherwise your system needs to support unicode
? Question Mark operator$ touch ?tecrobust.txt
? Question Mark operator$ touch .tecrobust.txt

For some conditions, you need to add the single quote in the command as we shared in the above table. Here we come to the end of the article. We almost tried to cover all the topics related special character filename. If you have any queries then feel free to comment us.

What do you think?

points
Upvote Downvote
  • Content Length
  • Content Explanation
  • Command Explanation
  • Command Examples usages
4.8

Summary

In this article, we shared some most important tips to use the special characters, symbols in the filenames of the file. To know more information, we suggest you see our homepage Tecrobust

Leave a Reply

Your email address will not be published. Required fields are marked *

Written by Rachel

I'm Rachel, From Arizona. Am playing the "Content Management" Role in Tec Robust. I started working in TR from Jan 2019. Big Marvel Fan.

Contact me:
tecrobust [@] gmail.com
Sub: Message to Rachel

How to Generate, Encrypt, Decrypt Random Passwords in Linux

Bleachbit 2.0

How To Install Bleachbit For Ubuntu Linux 14.04, 16.04 & 18.04