Linux/Unix – Removing Files and Directories

 

Use rm (“remove”) to delete a file and remove it from the system. Give the name of the file to remove as an argument.

To remove the file `notes’ in the current working directory, type:

$ rm notes

 

To remove a directory and all of the files and subdirectories it contains, use the `−R’ (“recursive”) option.

To remove the directory `waste’ and all of its contents, type:

$ rm −R waste

 

To remove an empty directory, use rmdir; it removes the empty directories you specify. If you specify a directory that contains files or subdirectories, rmdir reports an error.

To remove the directory `empty’, type:

$ rmdir empty

 

A Safe Way to Remove a File

Once a file is removed, it is permanently deleted and there is no command you can use to restore it; you cannot “undelete” it. (Although if you can unmount the filesystem that contained the file immediately after you deleted the file, a wizard might be able to help reconstruct the lost file by using grep to search the filesystem device file.)

A safer way to remove files is to use del, which is simply an alias to rm with the `−i’ option. This specifies for rm to run in interactive mode and confirm the deletion of each file. It may be good practice to get in the habit of using del all the time, so that you don’t make an accidental slip and rm an important file.

 

Removing a File with a Strange Name

Files with strange characters in their names (like spaces, control characters, beginning hyphens, and so on) pose a problem when you want to remove them. There are a few solutions to this problem.

One way is to use tab completion to complete the name of the file. This works when the name of the file you want to remove has enough characters to uniquely identify it so that completion can work.

To use tab completion to remove the file `No Way’ in the current directory, type:

$ rm NoTAB Way

 

In the above example, after TAB was typed, the shell filled in the rest of the file name (` Way’). When a file name begins with a control character or other strange character, specify the file name with a file name pattern that uniquely identifies it. Use the `−i’ option to verify the deletion.

To delete the file `^Acat’ in a directory that also contains the files `cat’ and `dog’, type:

$ rm −i ?cat RET

rm: remove `^Acat’? y

$

In the above example, the expansion pattern `?cat’ matches the file `^Acat’ and no other files in the directory. The `−i’ option was used because, in some cases, no unique pattern can be made for a file—for example, if this directory also contained a file called `1cat’, the above rm command would also attempt to remove it; with the `−i’ option, you can answer n to it.

These first two methods will not work with files that begin with a hyphen character, because rm will interpret such a file name as an option; to remove such a file, use the `−−’ option−−it specifies that what follows are arguments and not options. To remove the file `−cat’ from the current directory, type:

$ rm −− −cat 


Advertisements

About msotela

This blog is for anyone who wants to access the power of a Linux system as a systems administrator or user. You may be a Linux enthusiast, a Linux professional, or possibly a computer professional who is increasingly finding the Windows systems in your data center supplanted by Linux boxes.

Posted on March 16, 2009, in Unix/Linux. Bookmark the permalink. Leave a comment.

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: