Linux rm Command Tutorial With Examles
Linux is a file based operating system and actually everything is a file. Sometimes we need to clear our server from some of files. How can we achieve that? There are alternative ways to delete files but the most popular command is
rm command definition is remove files or directories. By default rm command do not remove directories. In this example we will remove file named
$ rm test
We may need to remove files in the current working directory and subdirectories. In this situations we can use
-r option in order to remove all current and sub directories files. But keep in mind that this will not remove directories and hidden files.
$ rm -r test
Remove All Files
We can delete all files by using bash glob feature. Glob means all files and folders in this or specified directory
$ rm *
As we see we deleted files but can not deleted directories.
Delete Specific Extension
We can specify specific extension to delete. We will provide the file name by using glob
* and file extension. In this example we will remove files with
$ rm *.txt
Delete Multiple Files
Multiple files can be deleted by specifying them one by one. In this example we will remove two files but more files can be added. We will delete files named
$ rm httpd-2.4.23.tar.bz2 httpd-2.4.23.tar.bz2.asc
Remove and Delete Forcefully or Delete Directories
If there are some protection about files we want to delete we can specify forcefully parameter with recursive parameter.This option will also remove directories too.
$ rm -rf output2/
Delete Empty Directory
rm command do not deletes directories. But id the directory is empty we can delete this directory by using
-d option. In this example we will delete the empty directory named
$ rm -d folder
Ask Before Delete
It can be set to ask before delete operation. So we can specify which files will be deleted which will not one by one.
$ rm -i *.zip
While removing files and folders we can print detailed information about removal. We will use verbose
-v option in order to provide detailed output.
$ rm -v *