How to Delete Multiple Similarly Named Files from the Terminal
Have you ever had a task that requires you to test a file loader, file links or download a template to be able to preview Markup? If so you may have run into the issue of an overly cluttered Downloads directory. Users coming from a Windows or GUI background may try to solve this by instinctively opening explorer or documents and using the right click and delete combo to remove files.
For a long time I did this, but then I found the power of the terminal. Through the use of the rm command you can more quickly navigate and delete files all with a simple set of characters. In this article we will look at what the rm command is and arguments you can pair with it to find and destroy any file on your computer.
Basics of Deletion from the Terminal
The first thing you must know and always remember about using rm is that a file acted on by rm is immediately deleted. No, it is not moved to trash for further review before being destroyed. It is immediately wiped from your computer. You have been warned…
With that covered, let’s now return to what the rm command is. The rm command is a Linux, macOS and Unix command that allows users to specify a relative or absolute path to a file to be deleted. A very basic implementation is:
rm my_first_file.txt
For this example we are declaring a relative path (unless this file is stored in your root directory) which means that we are in the current folder where this file is located. If we run this command and then run ‘ls’ we should not see the file there anymore.
Of course you don’t need to work on files in the current directory. You could, for instance run a command like:
rm ./semester_1/english/essay.txt
Though you are not in the ‘english’ file where the ‘essay.txt’ file is located, specifying the path, as we do above, will clear up what resource you wish to delete. Going further, we can delete multiple files at once. The rm command can take many arguments, or files, to delete.
Say we are clearing up our English directory from earlier. The 2 drafts we have are of no use now so there’s no reason to take up memory. To remove these we could run:
rm essay_draft_1.txt essay_draft_2.txt
This will remove both of our drafts.
Safer Deletions
Remember what I said earlier about all resource removals being final? If you’re nervous you may delete a prized slide deck or an important email, don’t worry. There’s an argument for that. By including ‘-i’ flag as an argument to your rm command, you will have to confirm the file you want to delete before deleting it. This command will require you confirmation:
rm -i unicorn_company_slide_deck.pptx
remove unicorn_company_slide_deck.pptx?
All you have to do is type ‘yes’ and hit enter. Once you’ve done this the command will run and delete your file. Safe and final!
How to Remove Multiple Similarly Named Files
Let’s get to what you came here for: how to delete similarly named files. If you are familiar with regex then this shouldn’t be too much new material for you. If you aren’t familiar with regex then let me explain what it is.
Regex is a short name for regular expressions and is a way to manipulate text by patterns. There are various different symbols and characters that can be combined to search, modify or delete certain parts of text. In our example it will be to group and delete similarly named files.
The regex symbol we will use is ‘*’ or the wildcard symbol. This symbol takes the place of one or more characters in a string, or in our case a file name. We may know the first three letters of our file ‘abc’ but not know the exact timestamp that has been appended to the file name.
For our example we will assume that we are in a files directory containing 3 files. We run the ls command within the files directory to display the following files
ls > abc_033122112214.txt abc_033122112231.txt abc_033122112257.txt
If we don’t want these files but don’t care to type out a full command for each one, we can use our previous learning to delete them at once. In order to do that we will run the command:
rm -i abc*
This will not immediately delete all of them, as I have added in the “-i” flag. It is good practice to add this flag if you are doing a batch delete using a regular expression. You never know when you may screw up the format of your regex.
Our command will run through our directory and group together all 3 files. From this queue we will get three expressions asking if we’d like to delete the file. We say yes all times and we are done!
Conclusion
Working from the terminal is both quick and empowering. This is only scratching the surface of dealing with files and file structures as we haven’t even touched directories. I hope this article helped you clean up you folders. Let me know in the comments your favorite, lesser known terminal command!
Notes
https://www.howtogeek.com/409115/how-to-delete-files-and-directories-in-the-linux-terminal/