How to Remove Files and Directories in Linux using "rm" Command

You can permanently remove files and directories from the directory hierarchy. You can use the rm command to remove files and directories. You can use the rmdir command to remove empty directories. Files and directories are removed without prompting you for confirmation.

Removing Files

You can use the rm command to remove a file or multiple files on the same command line. The syntax for the rm command is:

$ rm option filename

To remove the file named projection from the letters directory, perform the rm command.

$ cd ~/letters
$ ls
mailbox     project2    projection  research    results

$ rm projection
$ ls
mailbox   project2  research  results

To remove the research file and the project2 file from the letters directory, perform the rm command:

$ pwd
/export/home/user1/letters
$ ls
mailbox   project2  research  results

$ rm research project2
$ ls
mailbox  results
$

The -i option prompts you for confirmation before removing any file:

  • A yes response completes the removal of the file.
  • A no response prevents the rm command from removing the file.

To remove the contents of a directory, perform the rm command with the -i option.

$ rm -i *
rm: remove file1: (yes/no) ? Y
rm: remove file2: (yes/no) ? Y
rm: remove file3: (yes/no) ? Y
rm: remove file4: (yes/no) ? Y

$ ls
$

Removing Directories

There are two ways to remove directories. You can use the rmdir command to remove empty directories. You can use the rm command with the -r option to remove directories that contain files and subdirectories.

To remove a directory in which you are currently working, you must change to its parent directory.

Removing an Empty Directory

You can use the rmdir command to remove empty directories. The syntax for the rmdir command is:

$ rmdir directories

If a directory is not empty, the rmdir command displays the following error message:

rmdir: directory “directory_name” : Directory not empty

To remove the empty directory, perform the rmdir command:

$ cd
$ pwd
/export/home/user1
$ cd newdir
$ ls -F
empty/

$ rmdir empty
$ ls
$

Removing a Directory With Contents

You can use the rm command with the -r option to remove directories that contain files and subdirectories. The syntax for the rm command is:

$ rm options directories

If you do not use the -r option, the following error message appears:

rm: directoryname : is a directory

The table below describes the options that you can use with the rm command when you are removing directories.

Option Description
-r Includes the contents of a directory and the contents of all subdirectories when you remove a directory
-i Prevents the accidental removal of existing files or directories

The -i option prompts you for confirmation before removing a file or directory.

  • A yes response completes the removal.
  • A no response prevents the removal.

To remove the letters directory and its contents, perform the rm -r command.

$ cd
$ pwd
/export/home/user1
$ ls letters
results

$ rm -r letters
$ ls letters
letters: No such file or directory
$

To interactively remove a directory and its contents, perform the rm -r command with the -i option. Create a new directory called rmtest in your /export/home/user1 directory.

$ pwd
/export/home/user1
$ mkdir rmtest
$ ls
Reports    dir1        dir4        file.1     file2    fruit2
brands     dir10       dir5        file.2     file3    newdir
dante      dir2        feathers    file.3     file4    rmtest
dante_1    dir3       feathers_6  file1      fruit    tutor.vi

$ cd rmtest
$ touch testfile
$ cd
$ rm -ir rmtest
rm: examine files in directory rmtest (yes/no)? y
rm: remove rmtest/testfile (yes/no)? y
rm: remove rmtest: (yes/no)? y
$ ls
Reports    dir1        dir4        file.1     file2    fruit2
brands     dir10       dir5        file.2     file3    newdir
dante      dir2       feathers    file.3     file4    tutor.vi
dante_1    dir3       feathers_6  file1      fruit
$