Tuesday, September 25, 2001

File names with Special Characters

Some times, either by mistake, downloading something from the Internet or other reasons, you get files with non-standard characters in their names that can give you problems. Let me be more specific with a particular example: suppose you have a file called Important file, that is, the full name of the file is the words Important and file separated by a single space (I will also assume that this is the only file in the current directory, to avoid particular cases where the commands given below do not behave as I explain). If you want to see the file contents, say with less, the following command will not work:

less Important file

Instead of the contents of the file you will get an error from the system telling you that the files Important and file do not exist. This is because the space character is also used by the system to separate words in the command line. So, inthe above example, what the system understand is the command less applied to two files, called Important and file, which do not exist in your directory (remember I’m assuming that there is only one file in the currect directory, with that "funny" name).

How can you get around this problem? Well, what you need to do is to give the space character as part of the file name, and not as a separation character between file names. This is called escaping the space character. One possible way is by enclosing the file name in double quotation marks:

less "Important file"

You can do this with any other command, not only less. Another possible way is by putting a slash (\) before the space, so the Operating System (rather, the shell) knows that the space is understood as that. The command in this case will be like this:

less Important\ file

There are other characters in file names that give trouble, for example names starting with hyphens (-). This is because most options for commands in Linux are given by the hyphen, for example rm -v and things like that. For example, a file called -myfile cannot be removed with this command:

rm -myfile

You will get an error message saying invalid option – m to the command rm. What can you do? Here is another way out:

rm — -myfile

The two hyphens tell the system that what comes after them are not options any longer but rather the argument to the command rm, that is, the name of the file that you want to remove.

No comments: