Home » programming » Filenames may be treated as flags in shell

Filenames may be treated as flags in shell

Be careful when running commands like rm *. A filename may be interpreted as a switch. That happens because the shell tries to interpret the filename before passing it to the program.

For instance, in the following example, we see that rm * is executed differently, depending on the names of the files of the current directory. The (double dash) signifies the end of the switches.

$ mkdir a
$ mkdir b
$ touch a.txt
$ rm *
rm: cannot remove `a’: Is a directory
rm: cannot remove `b’: Is a directory
$ ls
b/ a/
$ touch a.txt
$ touch — -rf
$ ls
b/ a/ -rf a.txt
$ rm *
$ ls
-rf

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: