Linux - Manage Spaces And Special Characters In File Name

May 24, 2011 | By
| Reply More

In Linux, whitespace is a special character and one of the most frequently used characters. It separates the commands or arguments to the commands. For example, the copy command treats the first file assigned to it as the source file and the second file as the destination file. These two file arguments are separated by whitespace. So if the file name contains whitespace, it creates problem on the command line. This article discusses how you can create files with special characters and how to use them on the command line effectively.

Handling special characters

Let us try to create a file with spaces in its name with touch command.

$ touch file with spaces
$ ls -l
total 0
-rw-r--r-- 1 raghu raghu 0 2012-08-11 09:04 file
-rw-r--r-- 1 raghu raghu 0 2012-08-11 09:04 spaces
-rw-r--r-- 1 raghu raghu 0 2012-08-11 09:04 with

The “ls” command in the above output shows that we do not get a single file but three files, i.e. file, spaces and with. This is because touch command considers the name as three different files, and not as a single file.

Two methods are available for handling these characters that have special meaning for shell. First one is using quotes and second is with escape character.

Quotes

The simplest method is using quotes with the file name. Now, let us try creating the above file with quotes.

$ touch 'file with spaces'
$ ls -l
total 0
-rw-r--r-- 1 raghu raghu 0 2012-08-11 09:04 file
-rw-r--r-- 1 raghu raghu 0 2012-08-11 09:10 file with spaces
-rw-r--r-- 1 raghu raghu 0 2012-08-11 09:04 spaces
-rw-r--r-- 1 raghu raghu 0 2012-08-11 09:04 with

Now, you can see the file named "file with spaces". Both single quotes and double quotes can be used with file names, but it is advised to use single quotes. This is because some special characters cannot be escaped by double quotes. Single quotes handle them as well. Other special characters like $, *, ? etc can also be used in file names.

$ touch '?file'
$ touch '*file'
$ touch '$file'
$ ls -l
total 0
-rw-r--r-- 1 raghu raghu 0 2012-08-11 09:17 ?file
-rw-r--r-- 1 raghu raghu 0 2012-08-11 09:17 $file
-rw-r--r-- 1 raghu raghu 0 2012-08-11 09:17 *file

Another example

$ rm ‘File*.txt’

In the above example quotes are more important. Because * is a shell wildcard that matches any character, you will be in trouble if you don’t use the quotes. Without the quotes, the shell removes File*.txt as intended, but in addition File.txt, File2.txt, File22.txt and others will get deleted.

Escaping characters

This is another method that can be used for files with special characters in their names. The backslash character escapes the character following it. It means that the character that immediately follows backslash character is treated as normal character by the shell and not as special character. For example, we can use backslash for creating a file that contains spaces in its name.

$ touch escaping\ with\ backslash.txt
$ ls -l
total 0
-rw-r--r-- 1 raghu raghu 0 2012-08-11 09:23 escaping with backslash.txt

Similarly, the other special characters can also be escaped with backslash:

$ touch escape\?
$ touch escape\*
$ touch escape\$
$ ls -l
total 0
-rw-r--r-- 1 raghu raghu 0 2012-08-11 09:24 escape?
-rw-r--r-- 1 raghu raghu 0 2012-08-11 09:24 escape$
-rw-r--r-- 1 raghu raghu 0 2012-08-11 09:24 escape*
-rw-r--r-- 1 raghu raghu 0 2012-08-11 09:23 escaping with backslash.txt

Now, you have seen that the quotes (single or double) and the backslash characters are the special characters that are used for handling special characters. So how can you use these characters in your file names? For quotes, you can use backslash:

$ ls
$ touch quote\'mark
$ touch quote\"mark
$ ls -l
total 0
-rw-r--r-- 1 raghu raghu 0 2012-08-11 09:28 quote'mark
-rw-r--r-- 1 raghu raghu 0 2012-08-11 09:28 quote"mark

That was simple. So what about backslash? Remember, backslash escapes every special character immediately following it? So we use backslash for escaping backslash itself.

$ touch escape\\backslash
$ ls -l
total 0
-rw-r--r-- 1 raghu raghu 0 2012-08-11 09:30 escape\backslash

Filed Under : HOWTOS, LINUX HOWTO

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.