Linux (and Unix) filesystems were not originally designed to handle blanks in filenames. This can cause quite a few problems, as the shell treats each item after a blank as another file or parameter. A solution is to use quotes, the backslash, or the Tab key.
The following sections assume the user has not modified the Bash Internal Field Separator (IFS) variable.
See the following screenshot. I purposely created three "bad" filenames:
Run
ls -la file with blanks.txt
and notice the errors.Now run it again, but enclose the filename in quotes:
ls -la "file with blanks.txt"
; it will work properly now.Enter
ls -la file
and press Tab. It will escape the blanks for you.Run
ls -la special>.txt
. Observe the error.Enclose in quotes as before using the following command:
ls -la "special>.txt"
Now try
ls -la -startswithdash.txt
and then try quoting it. Doesn't work, right?Precede the filename with the
./
operator using the following command:ls -la ./-starWtswithdash.txt
As you can see, this can also be a problem if special characters have been used in the filename. Study this one a bit and it will become clear. Remember the Tab key; it works really well for just about every case. If the file starts with a dash, use the ./
operator. It means to refer to the file in the current directory.
The issue of blanks and special characters is even more of a problem in scripts. This will be covered in more detail in Chapter 8, Working with Scripts.