How To Delete File In Linux? – POFTUT

How To Delete File In Linux?


Deleting files in Linux can be sometimes tricky. We have a tool named rm which is the shortcut for the word remove. In this tutorial, we will look at how to remove or delete a file in Linux with different examples and ways.

rm Command Syntax

rm  command syntax is the 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
  • OPTINS states the behavior of the rm  command as we will see below in detail.
  • FILENAME is the file or directory name we want to delete or operate.

rm Command Help

rm command help information can be printed to the console with the --help command which is like below. Help information will provide some popular options and usages.

$ rm --help
rm Command Help
rm Command Help

Delete Single File with rm Command

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 a 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 the file named foo.txt

$ rm foo.txt

Delete Multiple Files with rm Command

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 foo.txt and bar.txt but we can add more if we need it.

$ rm foo.txt bar.txt

Delete Files According To Their Extensions/Types with rm Command

Linux bahs provides the glob or asterisk in order to specify the files with a specific name or extension. We can use glob * in order to specify a specific extension like *.txt , *.pdf , *.tmp etc. We can use this extension or name specification in order to delete specific files. In this example, we will delete all *.deb extensions.

$ rm -v *.deb
Delete Files According To Their Extensions/Types
Delete Files According To Their Extensions/Types

We can also specify names like deleting all files which name starts with pof like below.

$ rm pof*

Delete Files Recursively

rm command provides the ability to delete or remove files recursively. Recursive removal will check subdirectories for files to remove with the directories. We will remove the directory name ndiff with all sub-directories and files in this example. We will use -R option for the recursive operation.

$ rm -R -v ndiff
Delete Files Recursively
Delete Files Recursively

Delete File with Prompt Before Every Removal

While removing files and directories we may need approval 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
Remove or Delete File with Prompt Before Every Removal
Remove or Delete File with Prompt Before Every Removal

Print Verbose Output About Delete Operation

While deleting files and directories we may want to see details of the removal operation. rm  command provides a verbose option which will list information about each deletion of file or directory. We will use -voption for this.

$ rm -R -v test
Print Verbose Output About Delete Operation
Print Verbose Output About Delete Operation

Delete empty Directories or Folders with rmdir Command

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 an empty directory or folder.

$ rmdir delete_me/
Delete empty Directories or Folders
Delete empty Directories or Folders

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 a text file. We will use xargs  command to-read the list and redirect to the rm command.

$ xargs rm < delete_file.txt
Read File Names From Text File For Delete or Removal
Read File Names From Text File For Delete or Removal

Delete File Names Starts with Dash -

Another interesting case is dash or - 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 were to specify the file or directory name start. For example we have a file named -file.txt and we want to remove. We will use the following command. As we can see the file name is specified after double dash. Options are specified before the double dash.

$ rm -i -- -file.txt

Delete Files By Reading Their Names From A File/List

In some cases, we may need to read a list that contains the file names we want to delete. This is generally a simple text file where each file name with their path is specified line by line. We can use xargs command to redirect the list contents to the rm command which will delete them one by one. In this example, we will read the list file names file-list.txt.

$ xargs rm -v < file-list.txt
Delete Files By Reading Their Names From A File/List
Delete Files By Reading Their Names From A File/List

Delete Files By Finding them with find Command

find is a very useful command which is used to find files and folders. find command also provides some options like running commands on the search results. We can also remove or delete files found by the find command. In this example, we will delete files that are older than 3 days.

$ find . -type f -mtime +3 -exec rm '{}' ';' -print
Delete Files By Finding them with find Command
Delete Files By Finding them with find Command

LEARN MORE  How To Remove Files Older Than 1 Day/1 Week/1 Month Recursively

Leave a Comment