Linux rm Command Tutorial With Examples – POFTUT

Linux rm Command Tutorial With Examples

Linux is a file-based operating system and actually everything is a file. Sometimes we need to clear our server from some of the files. How can we achieve that? There are alternative ways to delete files but the most popular command is rm .

Remove File

rm command definition is “remove files” or directories. By default, rm command does not remove directories. In this example, we will remove file named test

$ rm test

Remove Recursively

We may need to remove files in the current working directory and subdirectories. In these 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 the bash glob feature. Glob means all files and folders in this or specified directory

$ rm *

As we see we deleted files but can not delete directories.

Delete Specific Extension

We can specify a specific extension to delete it. We will provide the file name by using glob * and file extension. In this example, we will remove files with *.txt extension.

$ rm *.txt
Delete Specific Extension
Delete Specific Extension

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 httpd-2.4.23.tar.bz2 and httpd-2.4.23.tar.bz2.asc .

$ 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

Normally rm command does not delete 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 folder .

$ rm -d folder

Ask Before Delete

It can be set to ask before the delete operation. So we can specify which files will be deleted which will not one by one.

$ rm -i *.zip

Verbose Delete

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 *
Verbose Removal
Verbose Removal

LEARN MORE  How to Deal With Dashed File Names Like Open, Remove?

Leave a Comment