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 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

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.

Remove All Files

We can delete all files by using bash glob feature. Glob means all files and folders in this or specified directory

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

Delete Specific Extension

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

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 .

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.

LEARN MORE  How To Remove or Delete A Directory In Linux?

Remove and Delete Forcefully

Remove and Delete Forcefully

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 .

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.

Ask Before Delete

Ask Before Delete

Verbose Removal

While removing files and folders we can print detailed information about removal. We will use verbose -v option in order to provide detailed output.

Verbose Removal

Verbose Removal

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)