---

Fixing Unix/Linux/POSIX Filenames

“A Tale of Complications
magine that you don’t know Unix/Linux/POSIX (I presume you really
do), and that you’re trying to do some simple things with it. For
example, let’s try to print out the contents of all files in the
current directory, putting it into a file above:

cat * > ../collection

“The list doesn’t include “hidden” files (filenames beginning
with “.”), but often that’s what you want anyway, so that’s not
unreasonable. The problem is that although this usually works,
filenames could begin with “-” (e.g., “-n”). So if there’s a file
named “-n”, and you’re using GNU cat, all of a sudden your output
will be numbered! Oops; that means on every command we have to
disable option processing; for most commands that means using “–”
everywhere, except not all commands support “–” (ugh!). There are
lots of traps, e.g., for portability you’re supposed to use
printf(1) not echo(1) if the first parameter might start with a
“-“. Many people know that prefixing the filename or glob with “./”
can resolve these kinds of problems (e.g., “cat ./*”), and most
versions of “find” do prefix filenames by default. But not all
programs do such prefixing, because it’s not immediately obvious
that you need to do so. As a result, when you’re writing a new
program, you can’t guarantee that the caller has prefixed it nicely
for you.”

Complete
Story

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends, & analysis