6.1 Directory and File Management Using the Command Line
6.1.10 Removing files and directories
Files that are deleted from the command line on a UNIX system are permanently deleted and cannot be recovered. However, if you are using the File Manager in CDE, you have the ability to take a deleted file in the trash can and drag it back to a folder, thus "undeleting" it. CDE File manager will be covered later. The rm command is a versatile one that can be used to remove (delete) files or directories from the UNIX file system.

Removing Files
The
rm command can remove a single file or multiple files. You can remove several files at once by specifying their names after the rm command or you can use the asterisk (*) and question mark (?) metacharacters (wildcards).

Files that are deleted on a UNIX system are permanently deleted and cannot be recovered unless you are using the CDE graphical interface. The rm command can be used with the -i (interactive) option which prompts the user before removing files. Use the rm -i command as a precaution to avoid accidentally deleting files.

Command Format:
rm [-i] filename(s)

In the first example in Figure , the cd command is used to change to the letters subdirectory under your home directory which you can see by the results of the pwd command. The ls command lists the files currently in the letters directory. The touch command is used to create an empty file named projection which you can see in the next listing. The rm command is then used to delete the research and project2 files and the ls command shows that they were removed. The second example uses the -i or interactive option with rm command to remove the projection file created earlier with the touch command.

Removing Directories
The
rm -r (recursive) command is used to remove directories. It will remove the directory being targeted including all subdirectories and files in it. When the rm command is used with the -r option, it can remove a single directory (empty or not) or an entire section of the directory tree. Figure shows examples of removing files and directories using the rm command.

Command Format:
rm -r [i] directory_name(s)
Interactive Media Activity (Flash, 96 kB)
  Removing Directories with the rm Command

In this media activity, you are logged in as user2 and your current working directory is: /home/user2/dir4. Refer to the Class File Tree Structure by clicking the 'tree' button located on the Menu Bar and type the commands that would accomplish the requested objectives. 
Note: Be sure to press enter after each one. Click on step 1 to begin.

Lab Activity (Adobe Acrobat Reader, 47 kB)
  Basic Command Line File Management

In this lab, you will work with file management commands from the command line. The guidelines for file and directory naming, which are known as naming conventions, will be reviewed. You will create a simple directory structure and then create some files in those directories. You will practice creating and removing both files and directories.