File deletion is an important yet sensitive aspect of Linux administration. The Linux commands behind file deletion operations help us get rid of files we do not need.
There is usually a high chance that the files we wish to delete reside in a single folder or directory. If we did not need all these files; once we navigate to that directory, it would be easier to get rid of them with a simple rm command implementation.
$ cd directory $ rm * or $ sudo rm *
However, you might find yourself in a circumstance where the number of directory files you need to delete exceeds the number of directory files you wish to preserve.
This scenario requires some careful steps into the implementation of the appropriate Linux command so as not to accidentally delete important files like usable log files.
This article guide is here to address this issue.
Since this article seeks to demonstrate the deletion of multiple files within a directory except for a few, we need to create some dummy files for demonstration purposes and also to make the tutorial a bit exciting.
$ touch file1.txt file2.txt file3.zip file4.txt file5.html file6.css
Remove Files Using the Linux rm Command
As per its man page
($ man rm), the rm command belongs to the GNU Coreutils package and is primarily used for the deletion/removal of unwanted files and directories from a file system.
The basic syntax for the rm command is as follows:
$ rm [option]… [file]…
Some useful command options associated with the rm command include
-v (--verbose) which is primarily helpful to users that need visual proof of the objectives achieved after the command executes.
$ rm -v sample.txt
The other useful rm command option is
-f (--force) which ignores arguments or files specified for deletion if they are nonexistence.
$ rm -f sample.txt
For this tutorial, we will focus more on the
--verbose option to successfully visualize the deletion of targeted files.
Activate Extended Pattern Matching
We will need to implement some pattern matching to be executed alongside the rm command if we want to be successful in meeting the objective of this article guide.
We will use the Linux shopt command to enable extglob which gives us the needed extended pattern matches to use alongside the rm command.
$ shopt -s extglob
In some Linux OS distributions, the above option is already active by default.
Option 1: Delete All Files and Reserve One
In this case, we will delete all the created files except for file1.txt.
$ rm -v !(file1.txt)
Option 2: Delete All Files and Reserve a Few
Let us try to delete all the files and leave three (file1.txt, file3.zip, and file5.html).
$ rm -v !(file1.txt|file3.zip|file5.html)
Option 3: Delete All Files Except File Extension
Let us try to delete all the files and leave css, html, and zip files.
$ rm -v !(*.css) $ rm -v !(*.html|*.zip)
You might also like to read the following related articles:
- How to Delete Files Listed in Another File in Linux
- How to Delete Files Older Than Specified Days in Linux
- How to Find and Delete Empty Directories in Linux
You can now flexibly sort and delete files in a directory in Linux via the rm command.