Special Characters to Avoid in Names


Every operating system has certain no-no's when it comes to the characters you can use when naming files and directories. If you use Mac OS, the colon (:) isn't allowed; Windows users, on the other hand, can't use the backslash (\). Linux has its verboten characters as well. Before looking at those, however, here are the characters that are always safe: numbers, letters (either uppercase or lowercase), dots (.), and underscores (_). Other items on your keyboard might work perfectly, others might work but present complications due to the fact that your shell will try to interpret them in various ways, and some won't work at all.

/ is never an option because that particular character is used to separate directories and files. Let's say you want to keep a file listing books you want to buy. You somehow manage to name the file books/to_buy.txt (with a forward slash) to distinguish it from books/on_loan.txt and books/lost.txt. Now when you try to refer to your file at /home/scott/documents/books/to_buy.txt, your command isn't going to work because your shell thinks that a books directory is inside the documents directory, but it doesn't exist.

Instead of a forward slash, use an underscore (as I did for the to_buy part of the filename), or cram the words together (as in booksToBuy.txt or BooksToBuy.txt).

You could use a dash, forming books-to-buy.txt, but I find that underscores work nicely as word separators while remaining more unobtrusive than dashes. If you do use a dash, though, do not place it at the beginning of a filename, as in -books_to_buy.txt, or after a space, as in books - to buy. As you're going to see later, if you're using a command and you want to invoke special options for that command, you preface the options with dashes. As you're going to see in Chapter 2, "The Basics," the rm command deletes files, but if you tried typing rm -books_to_buy.txt, your shell would complain with the following error message:

rm: invalid option -- b 


You can use spaces if you'd like, forming books to buy.txt, but you have to let your shell know that those spaces are part of the filename. Your shell usually sees a space as a separator between arguments. Attempts to delete books to buy.txt confuses the shell, as it would try to delete a file named books, then one named to, and finally one named buy.txt. Ultimately, you won't delete books to buy.txt, and you might accidentally delete files you didn't want to remove.

So how do you work with spaces in filenames? Or the * and ? characters, which you'll learn more about in the next section? Or the ' and " characters, which also have special meanings in your shell? You have several choices. Avoid using them, if at all possible. Or escape them by placing a \ in front of the characters, which tells the shell that it should ignore their special usage and treat them as simple characters. It can grow tiresome, however, making sure that \ is in its proper place all the time:

$ rm Why\ don\ 't\ I\ name\ files\ with\ \*\?.txt 


Yuck. A simpler method that's a bit less onerous is to surround the filename with quotation marks, which function similarly to the \:

$ rm "Why don't I name files with *?.txt" 


This will work, but it's still a pain to have to use quotation marks all the time. A better solution is just not to use these characters in the first place. Table 1.1 has some characters and what to do about them.

Table 1.1. How to Use Special Characters in Filenames

Character

Advice

/

Never use. Cannot be escaped.

\

Must be escaped. Avoid.

-

Never use at beginning of file or directory name.

[ ]

Must be escaped. Avoid.

{ }

Must be escaped. Avoid.

*

Must be escaped. Avoid.

?

Must be escaped. Avoid.

'

Must be escaped. Avoid.

"

Must be escaped. Avoid.




Linux Phrasebook
Linux Phrasebook
ISBN: 0672328380
EAN: 2147483647
Year: 2007
Pages: 288

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net