UNIX Hints & Hacks

ContentsIndex

Chapter 1: Topics in Administration

 

Previous ChapterNext Chapter

Sections in this Chapter:

1.1 Collecting System Information

 

1.7 Swap on-the-Fly

 

1.13 Remove the ---- Dashes ----

1.2 Backup Key Files!

 

1.8 Keep It Up with nohup

 

1.14 echo Does ls

1.3 Execution on the Last Day of a Month

 

1.9 Redirecting Output to Null

 

1.15 Building Large Dummy Files

1.4 Dealing with Unwanted Daemons

 

1.10 Keeping Remote Users Out

 

1.16 Burning-in Disk Drives

1.5 Keep Those Daemons Running

 

1.11 Rewinding Tapes Fast

 

1.17 Bringing a System Down

1.6 fuser Instead of ps

1.12 Generating a Range of Numbers

 

 

1.13 Remove the ---- Dashes ----

1.13.1 Description

1.13.1 Description

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 *"

Other Resources

Man pages:

rm

UNIX Hints & Hacks

ContentsIndex

Chapter 1: Topics in Administration

 

Previous ChapterNext Chapter

Sections in this Chapter:

1.1 Collecting System Information

 

1.7 Swap on-the-Fly

 

1.13 Remove the ---- Dashes ----

1.2 Backup Key Files!

 

1.8 Keep It Up with nohup

 

1.14 echo Does ls

1.3 Execution on the Last Day of a Month

 

1.9 Redirecting Output to Null

 

1.15 Building Large Dummy Files

1.4 Dealing with Unwanted Daemons

 

1.10 Keeping Remote Users Out

 

1.16 Burning-in Disk Drives

1.5 Keep Those Daemons Running

 

1.11 Rewinding Tapes Fast

 

1.17 Bringing a System Down

1.6 fuser Instead of ps

1.12 Generating a Range of Numbers

 

 

© Copyright Macmillan USA. All rights reserved.