Greg Hinkel's UNIX Tip of the Week for April 28, 1996
Handling weird file names.
File names containing characters like - (dash), ; (semicolon), & (ampersand) in
them can cause difficulties with many UNIX commands. Actually - (dash) is only
a "problem" if it is the first character of the file name. More on that
later. Let's assume you have a file named "file1.vms;1" that you'd like to
read. If you enter
more file1.vms;1
The shell will run the program "more" and try to open a file named "file1.vms"
It will also try to execute the program "1"
Why? Because the semicolon is used to separated commands on a line. You
can tell the shell not to interpret the semicolon by quoting it. You can do
this by using single quotes, double quotes or the backslash character. Any
of the following will work
more "file1.vms;1"
more 'file1.vms;1'
more file1.vms\;1
Now lets assume you have a file named "file2&more" that you want to copy.
If you enter "cp file2&more fileX" the shell will run the "cp" program and
try to open a file named "file2" It will also try to run the program "more"
with "more" waiting to read from stdin. The & tells the shell (C shell and
Korn shell) to send the command to the background for processing. You can
use the same quoting methods, as above, to get the desired response.
cp "file2&more" fileX
cp 'file2&more' fileX
cp file2\&more fileX
Now assume you have a file named "-myfile"
You might try to list it with "ls -l -myfile" However, that won't work cause
both dashes (one before l and one before myfile) are interpreted as "options"
(or switches) to the ls command. So, you might try to put quotes around it
like this
ls -l '-myfile'
But, you'll get the same results. The best thing to do with a file that
begins with a dash is to delete it or rename it, cause "rm" and "mv" each
have a special option that allows you to manipulate such a file. So, you
can do one of the following.
rm - -myfile
mv - -myfile myfile
Previous Tip of the Week ·
Next Tip of the Week ·
Index
Greg's Home Page
Greg Hinkel / (hinkelgc AT ornl.gov)
Last Modified: Friday, 02-Sep-2005 12:52:27 EDT
Visitors: 4702 since March 18, 1996