How To Delete File In Linux?

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/delete-file-linux/
PINTEREST
PINTEREST
INSTAGRAM

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.

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

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

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.

LEARN MORE  How to Deal With Dashed File Names Like Open, Remove?

Remove or Delete Files Recursively
Remove or Delete Files Recursively

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.

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 verbose option which will list information about each deletion of file or directory. We will use -voption for this.

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 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 text file. We will use xargs  command to read 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 where  specify the file or directory name start.

LEARN MORE  How To Remove Files Older Than 1 Day Recursively

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.

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/delete-file-linux/
PINTEREST
PINTEREST
INSTAGRAM