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.
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
|Interactive deletion. When used the |
|Recursive deletion. When used, the |
|Remove empty directories that contain Zero files or subdirectories.|
|Verbose. When uses, the |
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
To delete a single file on Linux using the rm command, follow the syntax below.
$ rm <file name>
$ 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
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>
$ 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
hullo from your current directory.
$ rm h[eua]llo
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>
rmdir works straight out of the box with a much easier syntax as below.
$ rmdir <directory>
$ rm -r Documents
$ rmdir Documents
Either of the above commands deletes the
Documents directory if it exists.
Delete multiple directories on Linux using the
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>
$ 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
Dowments for deletion while the following will match any directory with exactly 9 characters.
$ rm -r ?????????
Documents directory and any other with 9 characters matches for deletion.
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.
$ rm -i<any other option> <file or directory name>
$ 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
no to each file or directory deletion. When you press enter, it is marked as
yes by default.
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.