How to delete all files with specific word in filename?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi and welcome to LinuxQuestions! What about the find command? It has features that match exactly what you're looking for: searching file names by pattern, recursion, ability to delete the items found. Take a look at man find or at http://www.gnu.org/software/findutils/manual/find.html. Check the -delete option (but be careful to use it only when you're absolutely sure of the search results). Feel free to post your attempts if in doubt.
You should really have a look at the manpage for find, especially the options -name and -delete. No need for a grep the way you are doing it, you can do all that with the available options of find.
Yes colucix is right you can use find command to get the listing of files and folders with a particular pattern. As he mentioned look at the man page of find command using "man find" to get the listing of switches that you can use with it. In your case you are looking for files with SomeText under /home/movie/wp-content/uploads/ so you can run the following command:
In which text mentioned between single quotes will be the patter you are looking for.
Once you are sure that you found the files & folders you can run the following command to delete them:
rm -r -f /home/movie/wp-content/uploads/SomeText*
Beware rm -r -f the matched pattern without any user intervention. If you want to be prompted before deletion then just use rm -r /Pattern. -r is used for recursive and -f is used for force
Also I would suggest you to backup the files in USB stick or other partition if you think you will be required the files at later point. You can use the following command to copy them over
rm -r -f /home/movie/wp-content/uploads/SomeText* will only work if those 'SomeText*' files are located directly under directory /home/movie/wp-content/uploads/ it won't scan some directories to match the pattern.
Else go with the other command -delete that I have mentioned in my previous post.
rm -r -f /home/movie/wp-content/uploads/SomeText* will only work if those 'SomeText*' files are located directly under directory /home/movie/wp-content/uploads/ it won't scan some directories to match the pattern.
Else go with the other command -delete that I have mentioned in my previous post.
I understand, just one more question...will this command delete all files and folders starting from "root" folder or only under /uploads/ folder?
When you run find / -name it searches from / if you give the specified path like crts said find /home/user1h -name 'SomeText*' or find /tmp/downloads/test/ -name 'SomeText*' it will search only under those directories.
I understand, just one more question...will this command delete all files and folders starting from "root" folder or only under /uploads/ folder?
find / -name 'Sometext*' -print -delete
The first you've mentioned and very dangerous indeed. The first(s) argument(s) of the find command are the item you search for, basically the starting point (directory) from which you want to perform a search:
Code:
find /home/movie/wp-content/uploads
without any option you will find all the objects below the specified search path. Then you may want to refine your search to find 1) only files (exclude directories, links and so on) using the -type predicate, 2) only files whose name matches a specific pattern using -name. These are the predicates of the find command:
Code:
find /home/movie/wp-content/uploads -type f -name \*SomeText\*
This gives you the result of your search. Then find offers a bunch of actions you can perform on every single item found, for example -ls to retrieve a detailed list, -delete to remove the items or -exec to run a custom command. As previously stated, first check the result of the search to be sure your criteria have matched only the files you really want to delete. If this is true, run the command again adding the -delete action and your files will vanish!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.