Linux “rm” Command Examples

Unix includes two commands that you can use to delete files and directories: rm (remove) and rmdir (remove directory). You can also use “rm” command to remove a directory as well. In this post, we are going to discuss on rm command in Linux. rm is a Linux terminal command used to delete/remove files & directories. It’s completely free application comes as in build application with Linux OS Installation. The main author of this application is Paul Rubin, Richard M. Stallman, Jim Meyering and David MacKenzie and released under GNU GPLv3 license.

Delete a File

Delete a file using rm command in Linux.

$ rm file.txt   # Delete a File

Delete a File with output

If you wish to get output after delete a file then use rm command with argument -v. Refer the command below.

$ rm -v file.txt   # Delete a file with Output
removed 'file.txt'

Prompt before Delete a File/Directory

rm command with argument -i will prompt you a warning message before delete a file. You will get the warning message before deleting each file if you are deleting multiple files.

$ rm -i file.txt   # Prompt a Warning Message before delete the file
rm: remove regular empty file 'file.txt'? y

Delete a Empty Directory

To delete a empty directory use rm command in Linux with argument -d. Refer the command below.

$ rm -d data/   # Delete Empty Directory

Delete a Directory with it’s Content

To delete a directory with all it’s content recursively use rm command with argument -R. Use the argument -f with the argument -R to delete the directory forcefully.

$ rm -Rf data/   # Delete a Directory with content

You can also use the below command to delete a directory with it’s content recursively.

$ rm -rf data/

Delete a file Forcefully

To delete a file forcefully use rm command in Linux with argument -f. Refer the command below.

$ rm -f file.txt   # Delete a File Forcefully

Prompt before deleting more then 3 files

rm command with argument -I will Prompt a warning message once before deleting more the three files.

$ rm -I file*   # Prompt a warning message before deleting more than 3 files
rm: remove 5 arguments? y

Delete Multiple Files using Wildcard

Delete multiple files at once using rm command in Linux using Wildcard. Here I have five text files. So to delete all files use the below command.

$ ls
file1.txt  file2.txt  file3.txt  file4.txt  file5.txt
$ rm file*   # Delete Multiple Files using Wildcard

You can also delete multiple files at once with the help of file extension using rm command in Linux. Refer the command below.

$ ls
file1.txt  file2.txt  file3.txt  file4.txt  file5.txt
$ rm *.txt
Related Post