File and Directory Removal on a Linux Server

Most of us are accustomed to the comforts of a desktop operating system. Deleting files usually involves clicking on their icons and pressing the delete key or dragging them to the trash. Regardless of which OS you use, you probably rarely drop to the command line to do your deleting.

For a Unix or Linux server user, using the command line is the fastest and most efficient way to remove files. While a graphical web-based file manager may be sufficient for a few files every now and then, real heavy file removal warrants a program that can do it quickly and painlessly.

The rm command

For most tasks, it is sufficient to use the rm command, short for “remove”. To remove a single file, you would type:

rm filename

You can remove a directory using the same command, but without any additional options, rm will give an error that looks like this:

rm: cannot remove `dname’: Is a directory

To remedy this, you need the “-r” option, which tells the delete to be recursive, removing any files or directories contained in the one you want to remove. Be careful with this one.

rm -r dname

On many systems, rm will not prompt you or give any feedback during or after removal of files. This is one reason why it is so fast. In some situations, however, you may want to have some type of prompting, just in case you accidentally tell it to remove the wrong files. With the “-i” option, rm will prompt you before every removal.

rm -i file1 file2 file3 file4 file5

The output will look like this:

rm: remove regular empty file `file1′? y

rm: remove regular empty file `file2′?

Furthermore, if you want confirmation after each deletion, you can use the “-v” (verbose) option to force rm to provide more information. The output will look like this:

removed `file2′

removed `file3′

If you are receiving prompts or other information and want to ignore all of it, use the “-f” or “–force” option.

rm -r -f file2 file3

For a file with spaces in the name, you will need quotes around it.

rm “a file with spaces”

To remove all of the files in a directory, you can use a * to represent them.

rm – r- f *

To remove only certain file extension types, use * followed by the extension.

rm -r -f *.php

To remove all files that begin with a certain set of characters, place the asterisk at the end.

rm -r -f file*

More drastic measures

While “rm” is not designed to work like a trashcan or recycle bin on your server, it is sometimes possible to recover files or their remnants. In situations where recovery would be less than ideal, you should take more drastic measures to ensure the files are truly gone.

The “shred” command is one such option. Rather than simply removing the file, shred will write over it several times to make sure its contents cannot be easily recovered. For example, file5 is text file that contains a single password. From the command line, type:

shred file5

Open the file to view the contents, and it will now looks something like this:

X^V5{Y^W��t���i^D�� ^G�*c�߀^K /���$ �>Uy�Krǩk^Do`ߩrЩ�:B��T��

If you want shred to also automatically remove the files after writing over them, add the “-u” option or “–remove”.

shred -u files

If you are particularly paranoid and do not believe writing over the files 3 times is enough, you can specify your own number of iterations.

shred –iterations=15

More File Removal Options

As a rule, whenever you make changes to your dedicated server, it is a good idea to have backups. Some web hosts, such as clustered web hosting company, offer backup solutions to customers. It is also a good idea to not get in the habit of deleting files as root unless they are files that only root can delete. Because these tools can delete massive amounts of data in mere seconds, remember to use them carefully.