Remove the —- Dashes —- (Unix)

September 29, 2007 at 4:35 pm (Operating Systems, Tips n Tricks)

A method of removing files that begin with dashes “-“.

There will be times when you stumble on a file at the top of the directory that contains dashes. Many users sometimes “fat finger” the keys and accidentally create a file with dashes.

-rwxrwxrwx  4 root          512 Aug 24 21:01 -F
-rwxrwxrwx  4 root          512 Aug 24 21:01 ---wow
drwxrwxrwx  4 root          512 Aug 24 21:01 .
drwxr-xr-x 19 root         7680 Jul  2 10:41 ..

If you attempt to remove them through normal methods, UNIX will attempt to use them as command-line options and the command will break out with an error:

# rm -F
Illegal option – F
Usage: rm [-fir] file…

# rm ---wow
Illegal option -- -
Illegal option -- -
Illegal option -- w
Illegal option -- o
Illegal option -- w
Usage: rm [-fir] file…

So how do you get rid of these files? There are a few ways to perform this task. The files can be removed by a file manager, by hiding the dashes, by fighting a dash with a dash, or by deleting the directory.

Example One: Using File Manager

Flavors: Those with a GUI-based file manager.

Most GUI-based UNIX interfaces today are packaged with a file manager. The theory behind this interface is to simplify the life of a system administrator. Again, I said in theory—this isn’t always the case.

File manager programs are pretty intelligent. They recognize file types and remove this misnamed file without any problems. If you feel that critical files are in danger from removing this file manually then use the file manager to remove the file.

Example Two: Hiding in the Directory

Flavors: AT&T and BSD

Syntax:

rm "./file"

This example uses a method of hiding the dash from the rm command.

$ rm ./-F
$ rm "./---wow"

By placing the ./ (dot, slash) in front of the filename, you hide the option and treat it as part of the file. The lines remove the file (not the option) from the current directory. Whenever in doubt, quotation marks (" ") help define the file and should be used.

Example Three: Fighting a Dash with a Dash

Flavors: AT&T and BSD

Syntax:

rm –- file

In this method you fight a dash with a dash.

$ rm -- -F
$ rm -- ---wow

The double dash before the file will treat it as a file and not as an option. It is similar to example two. It treats what UNIX thinks is an option as a file.

Example Four: Brute Force—rm -r

Flavors: AT&T and BSD

Syntax:

rm -r directory

This is the brute-force approach to solving this problem and should be used only as a last resort. If the file in question is in a nonsystem partition or directory, you may be safe to use this.

$ mv /usr/people/jdoe /usr/people/tmp/jdoe
$ rm -r /usr/people/jdoe
# mkdir /usr/people/jdoe
$ cp -pr /usr/people/jdoe/[A-Za-z]* /usr/people/jdoe

Line 1: Move all the contents of the directory, with the exception of the file in question, to another area.

Line 2: Go up one level and remove all the contents of that directory.

Line 3: Remake the directory.

Line 4: Copy the original data back.

Warning

You will destroy your system if you attempt to execute this procedure on a directory that the system uses, such as: /, /etc, /usr, /bin, /sbin.

Reason

The potential for deleting the wrong files exists for even the most experienced administrator. Use these methods and you shouldn’t have to rely on your backups to restore any unnecessarily deleted files. You will also not have to stay at work any later than you have to.

Real World Experience

There is nothing worse for an administrator than to be tested by your users. Every once in a while a user will attempt to play a practical joke and see whether you fall into the trap. Here is how it works.

While cruising around in systems and viewing the long listing of various directories with ls, I sometimes notice something very odd at the root level of the directory tree, a file called -r *.

-rw-------  1 root          147 Jan 19  1996 -r *
drwxr-xr-x 22 root         1024 Aug 21 21:16 .
drwxr-xr-x 22 root         1024 Aug 21 21:16 ..
-rw-------  1 root          147 Jan 19  1996 .Xauthority
-rw-r--r--  1 root          366 Sep 30  1994 .Xdefaults
-rw-r--r--  1 root          260 Jul 24  1997 .cshrc

Some admins get sucked into this prank and do a rm -r * and wipe out their system. Examples one and two will remove this file, but remember to put quotes around the entire file.

# echo "" > "-r *"

# rm ./"-r *"
# rm – "-r *"
Advertisements

1 Comment

  1. Remove the —- Dashes —- (Unix) said,

    […] computers2007 Did a great write-up today on computers2007.wordpress.comThe first paragraph is here: […]

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: