How to delete files under several sub-dirs in one command
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
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.
Technically, it is correct. Your way is correct too. You could always go under the parent directory where the logs are stored, and issue the following command:
Code:
rm -r *.log
Of course, that would need to be modified. I'm not in linux right now, so I don't know the full arguments to make that work off the top of my head.
I'd think I should clarify how it works a little, based on my understanding, as the behavior can be a bit confusing at first:
Code:
echo ** #expands to all files and directories, recursively.
echo **.txt #identical to *.txt; does not recurse
#when combined with other glob patterns.
echo **/ #expands to sub-directories only, recursively.
echo **/*.txt #lists all .txt files in all directories recursively.
#IOW, appended regular globs are applied to the top
#level and all sub-directories.
echo **/* #identical to **
There's more than one way to skin a cat ... err ... log files.
It just depends on what one wants to specifically do. Deleting all the logs and the directories containing them is one option. But it might be desired to leave the directories in place. Are there other files in there that are not logs and want to be kept? Are the logs always at the same directory level? Is it desired to delete them from several directories but not from all directories? It's best to know exactly what the OQer wanted to precisely do. Any of the answers seen might well do, depending on the exact need.
The thing to do beyond this is to understand what each of the various commands actually do, and see if they fit the need. Maybe the command, or maybe the need, can be manipulated to find a simple solution. For example, I once wanted to remove every file but one the ones ending in .iso from a very large directory. The simple solution was to make a new directory, move the .iso files over to the new directory, completely remove the old directory full of junk (including the directory itself), and finally rename the temporary directory to the original name. That might not be a solution for your issue. But if you understand the commands, I think you will find that even in very complex situations, you can figure out a way to do what you want.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.