One of Linux’s basic file system administration tasks involves creating, modifying, and deleting different types of files and directories. Therefore, knowing some essential tools and concepts for file deletion is handy and can save you time.
Related: 20 Basic Linux Commands for Beginners Explained with Examples
This article will show you several ways to delete files and directories in Linux. We will also provide brief information on the various flags and options you can use while deleting files and directories from your Linux system.
Delete files Using the rm Command
To delete a file, you need to use the rm
(remove) command and tell it what file or files you want it to delete. It has the following general syntax:
rm [OPTIONS] FILENAME
Code language: CSS (css)
For example, to delete a single file named file.jpg
, type the following in the command line:
rm file.jpg
Code language: CSS (css)
The rm
command displays a confirmation dialog for write-protected files. Otherwise, the command will directly delete the file. To make rm
always prompt before deleting a file, you can use -i
flag:
rm -i file.jpg
Code language: CSS (css)
The rm
command in Linux can also delete more than one file. Bypassing multiple filenames separated by a space as arguments to rm
, you can delete multiple files:
rm file1.jpg file2.jpg file3.jpg
Code language: CSS (css)
You can use the -f
(force) flag to delete write-protected files without asking for confirmation:
rm -f file.jpg
Code language: CSS (css)
In addition, the rm
command also supports regular expressions. If you want to delete all three files (file1.jpg
, file2.jpg
, and file3.jpg
), you can use:
rm file*.jpg
Code language: CSS (css)
If you need it, here’s the man page for the rm
command.
Delete files Using the unlink Command
The unlink
command also deletes a given file. This is another though not so popular, way of deleting a file in Linux.
You can use the unlink
command to permanently delete a single file named file.jpg
by typing the following:
unlink file.jpg
Code language: CSS (css)
You are probably wondering what the difference between rm
and unlink
is.
Above all, both commands are wrappers to the same fundamental function, an unlink()
system call. But the unlink
command suffers from the following restrictions:
- Unable to delete directories.
- Unable to recurse.
- Can only take one argument at a time.
- Has no options other than
--help
and--version
. - Less sanity checking.
For more about the unlink
command in Linux, consult its manual page.
Delete Directories Using the rm Command
By adding the -r
(recursive) option to the rm
command in Linux, you can delete a directory and all its contents (files, subdirectories, etc.).
For example, to remove a directory named myfiles
, type the following in the command line:
rm -r myfiles/
The rm
command would ask you to validate the procedure if the specified directory or a file inside it is write-protected. To remove a directory without confirmation:
rm -rf myfiles/
To delete multiple directories (for example, myfiles1, myfiles2, and myfiles3), type rm -rf followed by the directory names or path to directories, separated by a space, as follows:
rm -rf myfiles1/ myfiles2/ myfiles3/
Delete Directories Using the rmdir Command
Something important to note here is that the rmdir
command is used when deleting empty directories in Linux. If you need to remove a non-empty directory, use the rm
command.
If a specified directory is not empty, the output will display an error, as shown below.
rmdir: failed to remove 'myfiles/': Directory not empty
Code language: PHP (php)
To remove a single empty directory, type rmdir
followed by the directory name or path to the directory as follows:
rmdir myfiles/
To remove multiple directories (for example, myfiles1
, myfiles2
, and myfiles3
), type rmdir
followed by the directory names or path to directories, separated by a space, as follows:
rmdir myfiles1/ myfiles2/ myfiles3/
If the command finds content in any listed directories, it will skip it and move on to the next one.
With -p
options added to the rmdir
command, each directory argument is treated as a pathname, of which all components will be removed if they are already empty, starting from the last component.
For example, the following command will delete both: the parent myfiles
directory and its subdirectory subdir
.
rmdir -p myfiles/subdir/
If you need it, hereโs the man page for the unlink
command.
Conclusion
By now, you should clearly understand how to delete files and directories in Linux from the command line.
It is important to remember that when you delete a file or directory in Linux using rm
, unlink
, and rmdir
, it is instantly removed instead of moving towards Trash. Therefore, you will need to be careful while using these commands as you will not recover the removed files.
Practice the examples mentioned in this article, and you should be ready.