Section 2.8. How Do I Deal with Spaces and Odd Characters in Filenames?

2.8. How Do I Deal with Spaces and Odd Characters in Filenames?

Sometimes, the defaults associated with Linux shells are annoying. For example, the spaces associated with many Microsoft directories, such as My Directory, can adversely affect a Linux command. Yes, you can use quotes, but if you have a variable or command inside the quotes, the wrong quote character can have unintended consequences. The asterisk (*) is a common character in many files, but it's also a wildcard. Therefore, if you want to search for an asterisk, you'll need to escape the meaning of the character.

You can manage the effect of spaces, special characters, commands, and variables with appropriate use of backslashes (\), single quotes ('), double quotes ("), and back quotes (`).

2.8.1. Single Quotes

Single quotes can help you manage spaces. For example, if you want to mount a shared "My Documents" directory from a Microsoft computer, you might try mounting it locally on the test/ directory with the following command:

 smbmount //allaccess/My Documents test 

This assumes, of course, that you've shared the My Documents directory from the computer named allaccess.

Unfortunately, this command would lead to an error message relating to how Linux is unable to resolve the mount point Documents. In other words, it thinks you're trying to mount a directory, shared under the name "My," in the local Documents directory. The simplest solution is to use single quotes. In other words:

 smbmount '//allaccess/My Documents' test 

But if you're working with an expression where you want to process a variable such as $NAME or a command such as date, single quotes won't work. For example, you might find the following directive in a script:

 echo 'Welcome to America, $USER' 

And the output is:

 Welcome to America, $USER 

because single quotes prevent the shell from interpreting the $USER variable. Other options can process this variable, as I'm about to illustrate.

2.8.2. Double Quotes

Double quotes can help if you have variables in your expressions. For example, if you have the following directive in a script, the double quotes allow Linux to interpret the variable. Thus, the following directive:

 echo "Welcome to America, $USER" 

is interpreted on my user account as:

 Welcome to America, michael 

2.8.3. Back Quotes

The back quote (`) key is commonly the lowercase character on the key above Tab on a U.S. keyboard. It is on the same key as the ~, which represents users' home directories. Sometimes known as a backtick, it allows you to process commands and scripts within quotes. For example, you might find the following directive in a welcome script:

 echo "Welcome to America, $USER, it is now `/bin/date`" 

This is interpreted on my user account as:

 Welcome to America, michael; it is Wed Dec 10 16:05:13 PDT 2005 

2.8.4. Escaping a Character

There are characters that can make life a bit more interesting on Linux. As described earlier, the space between "My" and "Documents" can make Linux think there's another expression in your command. If you're looking for an asterisk in the files of your home directory, the following command won't do what you want:

 grep * * 

While you might think this command searches for the asterisk (*) in all files in the local directory, you have to remember that the asterisk is itself a wildcard, a type of metacharacter. The shell doesn't use the asterisk as a search term. The shell interprets the asterisk before it is ever seen by the grep command, so the command uses the first file in your directory as a search term.

To make Linux use the asterisk as the search term, you have to "escape" the meaning of the character. To do so, you can use the backslash. In other works, the following command actually looks for the asterisk in all commands in the current directory:

 grep \* * 

But wait, the asterisk is actually a metacharacter in two wayswithin the bash shell and as a grep search term. So while this command works, technically it needs to be escaped twice:

 grep \\* * 

The backslash is useful in other ways. For example, if you're constructing a long command in a script, a backslash can help you make the command more readable for others who review that script.

Linux Annoyances for Geeks
Linux Annoyances for Geeks: Getting the Most Flexible System in the World Just the Way You Want It
ISBN: 0596008015
EAN: 2147483647
Year: 2004
Pages: 144
Authors: Michael Jang

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: