How to Remove/Delete Files and Directories in Linux Using rm Command?

rm command is frequently used by Linux administrator to remove files and directories in Linux.

But it’s very dangerous command so, be careful when you are running this on your system.

Because once the files and directories are deleted it cannot be recovered.

However, we can recover the deleted files and folders but it would take time and need some additional software.

Moreover, these recovery software are not assuring 100% recovery probability.

so, it doesn’t assure that the deleted files and folders will be recovered 100%.

What’s rm Command?

rm stands for remove. rm command removes given file. By default, it does not remove directories.

Also, it does not work on directories that contain files. You have to use appropriate option with rm command to remove directories and sub-directories.

How to Remove Files in Linux?

To remove/delete a single file, use the rm command followed by the file name.

By default, it doesn’t show any output while deleting a file, if you want to see the action then use the -v option with rm command.

$ rm test1.txt

To delete a file with verbose. It will tell you what action being performed.

$ rm -v test2.txt

removed 'test2.txt'

How to Remove Multiple Files in Linux?

To delete multiple files at once use the rm command followed by the file names. Each file should be separated with space.

$ rm -v test3.txt test4.txt

removed 'test3.txt'
removed 'test4.txt'

How to Remove Group of Files in Linux?

If you would like to remove group of files, use a wildcard (*) and regular expansions to match it.

If you would like to remove all .png files. use, the following format to remove it.

$ rm -v *.png

removed 'test1.png'
removed 'test2.png'
removed 'test3.png'

How to Remove Files after getting a Confirmation in Linux?

This is the best and suggested way to remove files in Linux since it’s ask your confirmation before removing the given files.

I would advise newbies to use this format while using rm command in Linux.

$ rm -iv test5.txt test6.txt

rm: remove regular empty file 'test5.txt'? y
removed 'test5.txt'

rm: remove regular empty file 'test6.txt'? y
removed 'test6.txt'

How to Remove Files Forcefully in Linux?

This format is not advisable even for experienced users so, i personally advise to not to use this.

It doesn’t ask your confirmation while removing files or directories and removes forcefully.

$ rm -vf test7.txt test8.txt test9.txt

removed 'test7.txt'
removed 'test8.txt'
removed 'test9.txt'

Most of the distribution by default has created an alias for rm command to use rm -i instead of rm.

This can be found in user .bashrc file.

# cat .bashrc | grep -i rm
alias rm='rm -i'

How to Remove Empty Directories (Folders) in Linux?

Add -d option with rm command to remove empty directory.

$ rm -vd dir1

removed directory 'dir1'

Also, this option doesn’t remove non-empty directory. See the following output, which will tell you what it is.

$ rm -vd dir2

rm: cannot remove 'dir2': Directory not empty

As i told in the beginning of the article, by default rm command doesn’t delete a directory. See the following output for better understanding.

$ rm  dir2

rm: cannot remove 'dir2': Is a directory

How to Remove Directories (Folders) in Linux?

To remove a directory/folder in Linux, we need to use -r or --recursive option with rm command.

$ rm -rv dir2

removed 'dir2/test1.txt'
removed directory 'dir2'

If you would like to ask your confirmation before deleting it, use -i option with rm command. It will ask your confirmation for each and every files and directories while deleting it.

$ rm -rvi dir3

rm: descend into directory 'dir3'? y

rm: remove regular file 'dir3/test1.txt'? y
removed 'dir3/test1.txt'

rm: remove directory 'dir3'? y
removed directory 'dir3'

This will delete sub-directories and their files then followed by the parent directory files and directories.

If you would like to remove a directory forcefully, use -f option with rm command.

$ rm -rfv dir4

removed 'dir4/dir1/test1.txt'
removed directory 'dir4/dir1'
removed 'dir4/test1.txt'
removed directory 'dir4'

How to Remove Multiple Directories (Folders) in Linux?

To delete multiple folders at once use the rm command followed by the directory names. Each directory should be separated with space.

$ rm -rv dir5 dir6

removed 'dir5/dir1/test1.txt'
removed directory 'dir5/dir1'
removed directory 'dir5'
removed 'dir6/dir1/test1.txt'
removed directory 'dir6/dir1'
removed directory 'dir6'

rm Command Cheat Sheet

Command Description
rm [File Name] To remove given file
rm -v [File Name] To remove given file with details. Actually, it shows what is being done.
rm -v [File Name1] [File Name2] To remove multiple file at once.
rm -v [*.Extension] To remove set of files based on their extension. Example, .jpg, .png, .pdf, etc.
rm -vi [File Name] It prompts your confirmation before removing the given file.
rm -vf [File Name] It doesn’t prompt and remove given file forcefully.

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...