LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to find (various -name + delete contents but excluding some user folders in /home (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-find-various-name-delete-contents-but-excluding-some-user-folders-in-home-855780/)

Virtuose 01-12-2011 05:20 AM

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

colucix 01-12-2011 05:26 AM

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 {} \;
Take a look at man find or at the on-line manual to learn details about the -prune action. Feel free to ask for any clarification.

Edit: a little advice. Put an echo in front of the rm command until you're sure of the results! :)

David the H. 01-12-2011 06:08 AM

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.

Virtuose 01-12-2011 06:30 AM

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

colucix 01-12-2011 07:26 AM

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
previously use -print to test the results before actually deleting anything. Then I'd do:
Code:

find /home -mindepth 2 -wholename '/home/user3/*' -prune -o -type d -not -name ".*" -exec echo rmdir {} +
Here the -mindepth 2 option ensures that home directories are not removed, the -wholename expression is a pattern now since the whole path must be specified in order to prune it. The alternative form of -exec using + in place of \; put all the arguments on the same command line, so that the rmdir command is executed only once. Hope this helps.

Virtuose 01-12-2011 11:16 AM

all the solutions provided worked ok!!!

thanks a lot !!!!


All times are GMT -5. The time now is 06:46 PM.