Search anything:

rm command in Linux

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

The rm command is used to delete files and directories in Linux. In this article we have covered commonly used commands and options used with rm to achieve its goal.

Table of contents.

  1. Introduction.
  2. Deleting files.
  3. Deleting directories.
  4. Summary.
  5. References.


The rm command is used to delete files and directories in Linux. It does this by unlinking data from the file name so that the user can overwrite on the storage space it is stored in.


The syntax is as follows,

rm [OPTION]... [FILE]...

Deleting files.

To delete a file, we write,

rm fileName

We can also delete multiple files by passing them as arguments as follows,

rm fileName, fileName1, fileName2

To delete all files with the extension .pdf, we write,

rm *.pdf

We can also delete files based on a string of characters.
Assuming we have files named sample1, sample2, sample3, sample4

We can delete them by matching their names as follows,

rm *samp*.*


rm *sample*.*

rm will delete all files with the string samp in the first command or sample in the second command.

We can also delete them as follows,

rm sample[1-3]

The command deletes files, sample1, sample2 and sample3 only.

To delete a file in a different directory, we can switch to the directory using cd command or just pass the path to the file as an argument.

rm fileName

If the file is in the current directory.

rm ~/Documents/files/sample.txt

If the file is located in ~/Documents/files/ directory.

We can also use the -i option so that we are prompted before deleting a file,

rm -i fileName

And we have the prompt,

rm: remove regular file 'fileName'?

We enter y to delete it and n to abort the operation.

We can also print out debugging information using the -v option,

rm -v fileName

When this command in executed, output is printed to confirm the file has been deleted.

The -f option is used to force a deletion,

rm -f fileName

We can also delete a file using unlink command as follows,

unlink fileName

Deleting directories.

To delete a directory using rm command we use -r option,
An example

rm -r DIR

To delete an empty directory we use the -d option,

rm -d DIR

We can also delete multiple directories as follows,

rm -r DIR, DIR1, DIR2

To force a deletion we combine -r option it with -f option as follows,

rm -rf DIR

We can also use the rmdir command which deletes empty directories only,

rmdir DIR

and to delete multiple empty directories we write,

rmdir DIR, DIR1, DIR2

Execute the command,

mkdir DIR && cd DIR && touch file{1..10}.txt && cd .. && rm -i DIR/*

We create a directory DIR, change into that directory, create 10 files and change back, then we delete each created file one by one.
A prompt will be displayed for each file, we can choose either to keep it or delete it.
y to delete, n to keep it.

rm: remove regular empty file 'DIR/file2.txt'? y
rm: remove regular empty file 'DIR/file5.txt'? n

If no option is selected by default the file is not deleted.


rmdir is used to delete empty directories.
rm -r deletes directories which have files.
We can use the rm -rf command to force deletions.
rm can be combined with other commands such as find to achieve a goal.
Once a file or directory is deleted there is no turning back, that is, there is no 'recycle bin' in Linux, however there might be a way to recover files using the testdisk tool. This is covered in another article, the link is given below.


  1. Execute rm --help or man rm for its manual page.
  2. Recovering deleted files with test disk
rm command in Linux
Share this