Home Linux Commandline Tips How to Delete Files Listed in Another File in Linux

How to Delete Files Listed in Another File in Linux

Under a Linux operating system distribution, anything goes. Such open-source operating system environments take you through a roller coaster on anything there is to know about operating systems.

One key aspect that strongly defines the need and continued need for an operating system is file management. No operating system handles file management better than Linux. Whether you want to restrict, create, or enhance the security of your system and user files, Linux provides the best user experience and performance.

The Linux terminal or command-line interface is a flexible environment for manipulating files through commands associated with creating, renaming, moving, and deleting files. What if you learned another file manipulation tweak?

This article will walk us through deleting files listed in another file on a Linux operating system environment.

Practical Scenario

We will come up with a dummy directory tree for reference. Consider the following directory tree structure:

$ tree
Recursive Directory Listing
Recursive Directory Listing

From the above screen capture, TestDel is the parent directory with its absolute path being /home/dnyce/TestDel.

$ pwd
Print Working Directory
Print Working Directory

The TestDel directory has four other directories (docx_files, jpg_file, pdf_file, and xml_files) inside it populated with files of different formats (docx, jpg, pdf, and xml).

Now, if we want to delete some of these files, we will first need to list their absolute path in a file. We will create a text file called tobeDeleted.txt and add the path to some of these files.

$ sudo nano tobeDeleted.txt
Create File with File Paths
Create File with File Paths

Now that we have the file from which the listed files will be deleted, we can now look into the viable methodologies that will initiate and execute their deletion.

Method 1: Using xargs Command

Under the xargs approach, inputs from stdin are read by the xargs command and then converted to use arguments that can be associated with other commands.

To delete the files listed in the above tobeDeleted.txt file, we will implement and execute the xargs command in the following manner. The xargs command should be pointed to the absolute path of the tobeDeleted.txt text file.

$ xargs -I{} rm -r "{}" < /home/dnyce/tobeDeleted.txt 

The command reads the absolute path of the listed files and then checks their actual location for the deletion to commence.

The place holder "{}" quotes the listed filenames paths for the rm -r command to delete after -I replaces the string with the next file name path to be deleted.

We can now proceed and execute the tree command to be certain that the listed files were actually deleted with the following cat command output as a reference point.

$ cat tobeDeleted.txt
View File with File Paths
View File with File Paths
$ tree /home/dnyce/TestDel
Confirm File Deletion
Confirm File Deletion

As you can see, we have 10 files remaining out of the initial 13 files.

Method 2: Using sed Command

The sed’s compact one-line approach will easily help us delete files listed in another file. Update tobeDeleted.txt with new entrants.

$ sudo nano tobeDeleted.txt 
Create File with New File Paths
Create File with New File Paths

The sed command to use for the deletion of the files displayed in the above screen capture will look like the following:

$ sed 's/.*/rm -r "
$ sed 's/.*/rm -r "\0"/' /home/dnyce/tobeDeleted.txt
"/' /home/dnyce/tobeDeleted.txt
Delete Files with Sed Command
Delete Files with Sed Command

The placeholder "\0" points to the file name path for deletion by the rm -r command where s treats the file paths as separate items on the list with varying file extensions (.*).

As you might have noted, the sed command does not perform the actual file deletion for us but rather generates the appropriate deletion commands that we can comfortable execute.

Delete Files in Linux
Delete Files in Linux

Re-run the tree command to be sure that the files are deleted with the cat command output as a reference point.

$ cat /home/dnyce/tobeDeleted.txt
View File with File Locations
View File with File Locations
$ tree /home/dnyce/TestDel
Confirm File Deletion
Confirm File Deletion

We managed to delete the listed files.

Method 3: Using awk Command

Again, update the tobeDeleted.txt file with new entrants.

$ sudo nano tobeDeleted.txt 
Add New File Paths
Add New File Paths

The awk command to execute is as follows:

$ awk -v q='"' '$0 = "rm -r " q $0 q' /home/dnyce/tobeDeleted.txt
Delete Files with Awk Command
Delete Files with Awk Command

The -v assigns the variable q with placeholder value " and '$0 = "rm -r " q $0 q' generates the resulting command for deleting each of the files listed.

As noted, the awk command does not also perform the actual deletion of the listed files for us but generates the commands we can use:

Commands to Delete Files
Commands to Delete Files

Run the tree command in reference to the cat command output to confirm the deletion of the files.

$ cat /home/dnyce/tobeDeleted.txt
Confirm Files Deletion in File
Confirm Files Deletion in File
$ tree /home/dnyce/TestDel
Confirm File Deletion
Confirm File Deletion

You have successfully added a new skill to your Linux file management routines.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

1 thought on “How to Delete Files Listed in Another File in Linux”

  1. Nice article! However, a quick description of the reasons for the program flags and arguments would be much better for newbies to understand the logic of the process!

    Reply

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.