Before we get beyond the topic of working around files and directories with oddball names, there's one more trick that's useful to know about -- how to terminate options processing with "--" (excuse the grammatical pun!). Typing -- within a Unix command essentially tells the shell to stop processing the text in the command line as if it contained command line options. The command "ls -- -l", for example, would list (if it exists) a file named "-l".
To understand how this works, think back to how the shell processes what it sees on the command line. If we type "ls -l a*", for example, the shell translates a* into a list of all files that begin with the letter "a" and then makes the appropriate system calls to provide a detailed listing of those files, complete with owners, sizes and most recently changed dates and times.
If the files you want to list all start with "-a", on the other hand, the command "ls -l -- -a*" would provide the detailed list, the double hyphens telling the system not to interpret the "-" in "-a*" as the beginning of a string of options.
To remove a file starting with a hyphen, you could use a command which includes "--" as in this example:
Similarly, you can use the same trick to work with the file in other ways. For example, to append the contents of another file to the end of the oddly named file, you could use a command like this:
cat -- otherfile >> -oddfile
|
You can also use this trick to create an oddly named file in the first place -- very useful if you want to try the commands in this columns to see how they work.
To get a more detailed view of what is happening when you use the "--" option, start a subshell using the -x option. This will show you what the shell is doing when you type commands requiring argument expansion.
Now let's see what happens when we try to list our oddly named file normally. Notice how the argument "-o*" is expanded to "-oddfile", but the shell then goes on to complain that the "e" in the file's name is not a valid command option; all the other letters in "oddfile" happen to be valid ls command options.
bash$ ls -o*
+ ls -oddfile
ls: illegal option -- e
usage: ls -1RaAdCxmnlogrtucpFbqisfL [files]
|
Then, try the same thing using the "--" option.
bash$ ls -- -o*
+ ls -- -oddfile
-oddfile
|
The file name argument is once again expanded, but the shell has no problems with it because it doesn't interpret any portion of the file's name as a command option.
Any arguments on the command line after this point are treated as file names or other types of arguments, but not as command options.
If you just happen to have files with names that both include embedded
unprintable characters and begin with a hyphen, you can use a combination
of the -b and -- options to work with them:
boson> ls -b -- -o*
-o\177\177file
|
Thanks to Jared Still for pointing out the "--" option, one that I'd somehow overlooked in my decades of working with Unix systems.