How to find (various -name + delete contents but excluding some user folders in /home
Hi again.
i wanted to delete entire contents of the /home folders and subdirs excluding the config files (the hidden dot files) I have this code: find /home -type f -mtime +15 -not -name ".*" -exec rm -rf {} \; This runs ok, but now in the /home folder there are also some users folder that i want to exlude from the search and not be deleted So, for example, in /home i have user1, user2 and user3 subfolders. The contents (files and subfolders) in user1 and user2 must be deleted but not the dot files, those are config files. user3 folder must not deleted nor itīs contents nor subfolders. this a special user. any easy option to add the find command with ? or this requires complex scripting.?? please help thx in advance |
You can do that by means of the find command:
Code:
find /home -wholename /home/user3 -prune -o -type f -mtime +15 -not -name ".*" -exec echo rm -rf {} \; Edit: a little advice. Put an echo in front of the rm command until you're sure of the results! :) |
Gnu find has a -delete option. No need to use -exec to run an external rm unless you're not using the standard linux tools.
And again, make damn sure you preview the find list before you do so you don't delete anything you don't want. Just use find's -print option instead. You can pipe the result into less or a text file if you want to preview it more leisurely. |
It works but subfolders not deleted
Ok thanks to both replies.
When i try to use with the -delete and removed excec rm block code the command find does not work properly: i applied find /home -wholename /home/user3 -prune -o -type f -mtime +15 -not -name ".*" -exec echo rm -rf {} \; the result is that files in subfolders get deleted (omitting user3, thatīs ok) but user 1 and user2 have subfolders. Those subfolders must also be deleted, so ... delete files and subfolders, empty or not. We do not want the users to create just dozens of subfolders (they are ftp users) over time, leaving them empty or not. The users home folder folder will allways be there but everything (and not the hidden dot files of course -not -name is ok) under must be delete older than 15 days. plz help. thx in advance |
At this point I would use two different commands. You specified -type f in your command line, so that directories are not taken into account. Following the suggestion by David I would do something like this:
Code:
find /home -wholename /home/user3 -prune -o -type f -mtime +15 -not -name ".*" -delete Code:
find /home -mindepth 2 -wholename '/home/user3/*' -prune -o -type d -not -name ".*" -exec echo rmdir {} + |
all the solutions provided worked ok!!!
thanks a lot !!!! |
All times are GMT -5. The time now is 04:48 AM. |