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 remove word. In this tutorial, we will look at how to remove or delete a file in Linux with different examples and way.

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.

  • 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 Command Help

rm Command Help

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 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

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

LEARN MORE  How To Delete and Remove File and Directory with Python?

Delete Files According To Their Extensions/Types

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 an 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

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.

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 directory name ndiff with all sub-directories and files in this example. We will use -R option for the recursive operation.

Delete Files Recursively

Delete Files Recursively

Delete File with Prompt Before Every Removal

While removing files and directories we may need to 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.

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.

LEARN MORE  What Does "rm -rf" Command Do In Linux?

Print Verbose Output About Delete Operation

Print Verbose Output About Delete Operation

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

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.

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.

Say 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.

Delete Files By Reading Their Names From A File/List

In some cases, we may need to read a list which contains the file names we want to delete. This is generally a simple text file where each file names with their path are 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.

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 which are older than 3 days.

Delete Files By Finding them with find Command

Delete Files By Finding them with find Command

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)