Removing files with a leading dash

The other day I was running some bioinformatics software on the command line and I accidentally mixed up the order of the options on the command prompt. Understandably, the software crashed, but it also created an empty file in the directory with the filename -n.

$ ls -l
total 57744
-rw-r--r-- 1 root root        0 2012-02-24 16:46 -n
-rw-r--r-- 1 root root 19708438 2012-02-24 16:49 seq.0
-rw-r--r-- 1 root root 19708438 2012-02-24 16:49 seq.1
-rw-r--r-- 1 root root 19708438 2012-02-24 16:45 seq.2

When I tried to delete this file, I got errors on the command line. The rm program thought I was trying to define a command-line option, and putting the filename in quotes (my first fallback) didn’t seem to help.

$ rm -n
rm: invalid option -- 'n'
Try `rm ./-n' to remove the file `-n'.
Try `rm --help' for more information.
$ rm '-n'
rm: invalid option -- 'n'
Try `rm ./-n' to remove the file `-n'.
Try `rm --help' for more information.

If you’re more observant than I am, you’ll notice that the solution is given by the command itself. I did not, and ended up finding the solution a few minutes later on Google. Either way, here is the solution.

$ rm ./-n
$ ls -l
total 57744
-rw-r--r-- 1 root root 19708438 2012-02-24 16:49 seq.0
-rw-r--r-- 1 root root 19708438 2012-02-24 16:49 seq.1
-rw-r--r-- 1 root root 19708438 2012-02-24 16:45 seq.2

Just when you though you “knew Linux”, something simple like this shows you how little you know! As another example, I’ve been proud of myself the last several months in that I have been able to do an increasing percentage of my data processing tasks using command-line tools such as sed, awk, cut, grep, and so forth. Although I still write a lot of Perl, I was becoming less dependent on one-off scripts.

Then, today I learned that the commands head and tail have an -n option that allows you to specify how many lines from the top or the bottom of the file you want to spit out. I use head and tail frequently, but I was never aware you could change the default of 10 lines of output–this is something I was still using Perl one-liners for!

Maybe I need to come up with a list of 10-20 common Linux programs, and devote a week to learning the dirty details of each one–maybe devote 30 minutes a day to playing around with it and understanding all the options. Regardless of how much I learn, I will always have an appreciation for how much there is to learn. Applying these skills to solve important large-scale problems in biology is my focus, but the learning process will always be a fulfilling fringe benefit that will keep me engaged the rest of my life!

Advertisements

One comment

  1. Pingback: RTFM: grep, find, wc, head, tail « BioWize

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