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
From the above screen capture, TestDel is the parent directory with its absolute path being /home/dnyce/TestDel.
$ pwd
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
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
$ tree /home/dnyce/TestDel
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
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
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.
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
$ tree /home/dnyce/TestDel
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
The awk command to execute is as follows:
$ awk -v q='"' '$0 = "rm -r " q $0 q' /home/dnyce/tobeDeleted.txt
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:
Run the tree command in reference to the cat command output to confirm the deletion of the files.
$ cat /home/dnyce/tobeDeleted.txt
$ tree /home/dnyce/TestDel
You have successfully added a new skill to your Linux file management routines.
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!