How to delete files on Linux using the rm command

All operating systems have the option of deleting files easily. If you’re a command line kind of person, you can delete files on Linux using the rm command which permanently removes file by default. Unlike other file deletion tools on desktop operating systems, once you use the rm command on Linux, you will not be able to receiver the deleted files.

By structure, the rm command follows a strict syntax when deleting files on Linux, and this includes checking whether you are deleting a single file or a whole directory. On usage, the rm command will remove files without prompting you for any action, and you can easily identify whether you’re about to delete a file or directory by viewing the first long listing identifier.

For example, when you list files with the command below.

$ ls

Your output will be similar to the following.

total 32                                                                        
drwxr-xr-x 2 sysadmin sysadmin 4096 Feb  8  2021 Desktop                        
drwxr-xr-x 4 sysadmin sysadmin 4096 Feb  8  2021 Documents                      
drwxr-xr-x 2 sysadmin sysadmin 4096 Feb  8  2021 Downloads                      
drwxr-xr-x 2 sysadmin sysadmin 4096 Feb  8  2021 Music                          
drwxr-xr-x 2 sysadmin sysadmin 4096 Feb  8  2021 Pictures                       
drwxr-xr-x 2 sysadmin sysadmin 4096 Feb  8  2021 Public                         
drwxr-xr-x 2 sysadmin sysadmin 4096 Feb  8  2021 Templates                      
drwxr-xr-x 2 sysadmin sysadmin 4096 Feb  8  2021 Videos                         
-rw-rw-r-- 1 sysadmin sysadmin    0 Feb 23 19:37 techpointmag.com 

As per the above example, techpointmag.com is the only file because it starts with the identifier - instead of d which starts for directory. Hence, when you delete the file using the rm command, it is permanently deleted with no error. But if you attempt to delete a directory, you are presented an error similar to the one below.

rm: cannot remove 'Desktop': Is a directory

So, by usage, you can delete files on linux using the rm command using the syntax below.

$ rm -[option] <file or directory>

The following are the options accepted by the rm command:

OptionDescription
-iInteractive deletion. When used the rm command will ask for user confirmation before deleting a file or directory.
-rRecursive deletion. When used, the rm command will delete a directory and any sub-directories under it.
-dRemove empty directories that contain Zero files or subdirectories.
-vVerbose. When uses, the rm command will output each and every file being deleted.

By default, the rm command runs in forced (f)mode and does not give require any confirmation prompts unless you define interactive deletion i. In alliance with the rmdir command, you can safely delete files and directories on Linux as explained below.

Delete a single File on Linux using the rm command

To delete a single file on Linux using the rm command, follow the syntax below.

$ rm <file name>

Example:

$ rm techpointmag.com

The above command removes the file techpointmag.com if its present in your current directory. For safe deletion, we don’t recommend using a path since you may delete valuable information by mistake, but in any case, delete a single file from a path as below.

$ rm ~/techpointmag.com

The above command deletes the file techpointmag.com from your home directory. If the file does not exist, the command returns an error as below.

$ rm: /home/example/techpointmag.com: No such file or directory

Delete multiple files on Linux using the rm command

To delete multiple files on Linux using the rm command, you must use wildcard expressions which include ? that represents 1 character or more, * that represents all, and [] that accepts a range. By usage, you must follow the syntax below.

$ rm <expression>

Example

$ rm *

The above command removes all files in your current directory as the following command can remove files with exactly 3 characters.

$ rm ???

To provide a range, you must specify it in the curl brackets. For example, the following command removes the files named hello and hullo from your current directory.

$ rm h[eua]llo

The files hello, hullo and hallo are all matched for deletion if they exist in your directory.

Delete a directory on Linux using the rm command

Unlike files, to delete a directory on Linux using the rm command, you must specify an option as illustrated earlier in this article, and the command follows the syntax below.

$ rm -r <directory>

Alternatively, the rmdir works straight out of the box with a much easier syntax as below.

$ rmdir <directory>

Example:

$ rm -r Documents

OR

$ rmdir Documents

Either of the above commands deletes the Documents directory if it exists.

Delete multiple directories on Linux using the rm command

Just like deleting multiple files, to delete multiple directories on Linux using the rm command, you must supply a wildcard expression to follow. This is useful when deleting many directories you can’t mark individually. To achieve this, follow the syntax below.

$ rm -r <wildcard-expression>

Example

$ rm -r *

The above command deletes all directories and files in your current working directory. The * expression marks all files and directories for deletion, to limit the loss of important files, simply supply a range as below.

$ rm -r Do[cw]ments

The above command will match the directory Documents and Dowments for deletion while the following will match any directory with exactly 9 characters.

$ rm -r ?????????

Again, the Documents directory and any other with 9 characters matches for deletion.

Control the rm command on Linux

As explained in the above examples, the rm command is final, and deletes matched data permanently. So, how about controlling it to save your valuable data that you may never have intended to delete? Well, here is how you can achieve that with the i – Interactive option.

Syntax

$ rm -i<any other option> <file or directory name>

Example:

$ rm -ir *

The above command will prompt you to accept the deletion of all files and directories one by one as below.

rm: remove directory 'Desk'? yes
rm: remove directory 'hello'? no
rm: remove directory 'techpointmag.com'? no

You must reply either yes or no to each file or directory deletion. When you press enter, it is marked as yes by default.

Conclusion

The rm command is a very useful command since it does not keep any temporary files after deletion unlike the recycle bin on Windows. If you found this article useful, please leave us a comment in the section below.

Exit mobile version