How to use Special Character in Linux

To Operate Filenames Having Spaces and Special Characters in Linux

Basically in Linux, file name is created with number, character or alpha numeric but in this tutorial we are going to deal with file name with special characters.

Normally, we create files in the following format.

[root@linuxhelp Desktop]# touch abc.txt
[root@linuxhelp Desktop]# touch 123.txt
[root@linuxhelp Desktop]# touch as12.txt

File with special character dash (-)

Now lets try to create a file with special character dash (-). If you set the file name as follows it results the following error.

[root@linuxhelp Desktop]# touch -abc.txt
touch: invalid option -- ' b' 

Use -- option and enter the file name starts with (-) dash to overcome the above error.

[root@linuxhelp Desktop]# touch -- -abc.txt

List to view the created file.

[root@linuxhelp Desktop]# ls -l
total 59240
-rw-r--r-- 1 root root        0 Jun 17 02:37 123.txt
-rw-r--r-- 1 root root        0 Jun 17 02:39 -abc.txt

The following error occurs when we open the file with editor.

[root@linuxhelp Desktop]# vim -abc.txt 
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Jul 24 2015 02:23:23)
Unknown option argument: " -abc.txt" 

To open this file again you should use -- option before that file name .

[root@linuxhelp Desktop]# vim -- -abc.txt

The following error occurs when we move the special character file from one location to another location.

[root@linuxhelp Desktop]# mv -abc.txt /var/www/html/
mv: invalid option -- ' a' 

So use --option before the file name to move the file from one location to another location.

[root@linuxhelp Desktop]# mv -- -abc.txt /var/www/html/

The following error occurs when we remove the special character file.

[root@linuxhelp html]# rm -abc.txt 
rm: invalid option -- ' a' 

For removing file use -- option before the file.

[root@linuxhelp html]# rm -- -abc.txt

File name starts with special character hash (#)

Create the file name starts with special character # and the file name should be in quotes.

[root@linuxhelp ~]# touch ' #abc.txt' 
                (or) 
[root@linuxhelp ~]# touch  ./#abc.txt

Check whether the file is created or not.

[root@linuxhelp ~]# ls
#abc.txt         Desktop    Downloads    install.log.syslog  Pictures  Templates  Videos
anaconda-ks.cfg  Documents  install.log  Music               Public    testing

Open the file using vim or any other editor.

[root@linuxhelp ~]# vim ' #abc.txt' 

To remove the file .

[root@linuxhelp ~]# rm -rf ' #abc.txt' 

To move the file

[root@linuxhelp ~]# mv  ' #abc.txt'   /var/www/

File name starts with semi-colon ( )

Create the file name starts with ( ) semi colon by using the following command.

[root@linuxhelp ~]# touch '  abcd.txt' 

Open the file by vim or any other editor.

[root@linuxhelp ~]# vim  '  abcd.txt' 

Remove the file .

[root@linuxhelp ~]# rm &ndash rf  '  abcd.txt' 

To Create multiple file which means one normal file and special character based file name at same time

[root@linuxhelp ~]# touch abc.txt ' #abc.txt' 

File with different special character without any restrictions

To Create the file with different special character without any restrictions, run the following command.

Creating file name with Product symbol (*)

[root@linuxhelp ~]# touch *abc.txt

Creating file name with dollor symbol ($)

[root@linuxhelp ~]# touch  $abc.txt

Creating file name with percentage symbol (%)

[root@linuxhelp ~]# touch  %abc.txt

Creating file name with at symbol (@)

[root@linuxhelp ~]# touch @abc.txt

Creating file name with factorial symbol (!)

[root@linuxhelp ~]# touch ' !efg.txt' 
[root@linuxhelp ~]# ls
@abc.txt  abc.txt          Documents  install.log         Pictures   testing
%abc.txt  anaconda-ks.cfg  Downloads  install.log.syslog  Public     Videos
+abc.txt  Desktop          !efg.txt   Music               Templates

Creating file name start with Parenthesis

[root@linuxhelp ~]# touch ' (file.txt)' 
[root@linuxhelp ~]# ls
@abc.txt  abc.txt          Documents  (file.txt)          Music     Templates
%abc.txt  anaconda-ks.cfg  Downloads  install.log         Pictures  testing
+abc.txt  Desktop          !efg.txt   install.log.syslog  Public    Videos

Creating file name start with Chevrons

[root@linuxhelp ~]# touch ' ' 

Creating file name start with Square Brackets

[root@linuxhelp ~]# touch ' [ijk.txt]' 

Creating file name with dot marks.

[root@linuxhelp ~]# touch a.b.c.txt
[root@linuxhelp ~]# ls
  +abc.txt   anaconda-ks.cfg  Downloads   [ijk.txt]           Music     Templates
@abc.txt   a.b.c.txt  Desktop          !efg.txt    install.log         Pictures  testing
%abc.txt   abc.txt    Documents        (file.txt)  install.log.syslog  Public    Videos

Creating file name with white spaces

[root@linuxhelp ~]# touch this is my file.txt
[root@linuxhelp ~]# ls
  a.b.c.txt        Documents   [ijk.txt]           Pictures   this is my file.txt
@abc.txt   abc.txt          Downloads   install.log         Public     Videos
%abc.txt   anaconda-ks.cfg  !efg.txt    install.log.syslog  Templates
+abc.txt   Desktop          (file.txt)  Music               testing

FAQ
Q
How to open and view a file with [-] in Linux?
A
Please use -- option before that file name i.e vim -- -filename. to open filename with special characters.
Q
How to create a file with special character dash (-)?
A
In order to create a file with special character dash (-) use "touch -- -filename".
Q
Please provide special character to open a file
A
In order to open a file "';" is used along with the file name say for example "vim ';abcd.txt'"
Q
How to creating multiple files in one shot?
A
For creating multiple files with special characters use, "touch abc.txt '#abc.txt'".
Q
Why is that filenames with spaces, shows with slashes?
A
This is because Linux won't support file/folder names with spaces.