How To Delete File In Linux?
Deleting files in Linux can be sometimes tricky. We have tool named
rm which is the shortcut for the
remove word. In this tutorial we will look how to remove or delete file in Linux with different examples and way.
rm Command Syntax
rm command syntax is same as the most the Linux command. We can provide options before specifying the file and directories we cant to operate or delete.
rm OPTIONS FILENAME
OPTINSstates the behavior of the
rmcommand as we will see below in detail.
FILENAMEis the file or directory name we want to delete or operate.
Remove or Delete Single File
We will start with simple steps just deleting a single file. We will just specify the file name we want to delete. In order to remove the file successfully we should have privileges to modify file. For example if we try to remove the file owned by
root with a regular user we will get an error and would not delete the file.
In this example we will delete file named
$ rm foo.txt
Remove or Delete Multiple Files
We have the ability to delete multiple files in a single
rm command. We will just put file names we want to delete by separating them with space. In this example we will delete file names
bar.txt but we can add more if we need.
$ rm foo.txt bar.txt
Remove or Delete Files Recursively
rm command provides the ability to delete or remove files recursively. Recursive removal will check sub directories for files to remove with the directories. We will remove directory name
test with all sub-directories and files in this example. We will use
-R option for recursive operation.
$ rm -R test
Remove or Delete File with Prompt Before Every Removal
While removing files and directories we may need to aproval for each file to delete. In this case we can use
-i option which will prompt to accept or deny deletion of the given file.
$ rm -R -i test
Print Verbose Output About Delete Operation
While deleting files and directories we may want to see details of the removal operation.
rm command provides verbose option which will list information about each deletion of file or directory. We will use
-voption for this.
$ rm -R -v test
Delete empty Directories or Folders
In some cases we need to delete empty folders.
rm without options will not work in this case as we can see this in the following screenshot. We case use
rmdir command to remove empty directory or folder.
$ rmdir delete_me/
Read File Names From Text File For Delete or Removal
Another interesting use case for
rm command is providing file or directory names from a list like text file. We will use
xargs command to read list and redirect to the
$ xargs rm < delete_file.txt
Delete File Names Starts with Dash
Another interesting case is
- problem where file or directory names starting with
dash . As we know Linux commands options are specified with
dash. So how can
rm recognize file name from option. We will use
-- or double dash where specify the file or directory name start.
Say we have a file named
-file.txt and we want to remove. We will use following command. As we can see file name is specified after double dash. Options are specified before double dash.
$ rm -i -- -file.txt