LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-12-2011, 05:20 AM   #1
Virtuose
LQ Newbie
 
Registered: Jan 2011
Posts: 7

Rep: Reputation: 0
Question 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
 
Old 01-12-2011, 05:26 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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!

Last edited by colucix; 01-12-2011 at 05:29 AM.
 
Old 01-12-2011, 06:08 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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.
 
Old 01-12-2011, 06:30 AM   #4
Virtuose
LQ Newbie
 
Registered: Jan 2011
Posts: 7

Original Poster
Rep: Reputation: 0
Smile 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
 
Old 01-12-2011, 07:26 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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.
 
1 members found this post helpful.
Old 01-12-2011, 11:16 AM   #6
Virtuose
LQ Newbie
 
Registered: Jan 2011
Posts: 7

Original Poster
Rep: Reputation: 0
all the solutions provided worked ok!!!

thanks a lot !!!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to delete all folders and their contents except 3 rjo98 Linux - Newbie 3 01-28-2010 09:41 AM
find -exec command to recursively delete files and folders in folders with X name Joan Murt Linux - Newbie 2 07-08-2009 05:35 PM
delete .exe files in user's home folders Winanjaya Linux - Newbie 5 11-06-2008 09:52 PM
find -delete folders Meson Linux - General 7 04-16-2008 02:30 PM
Excluding Folders with CP Luke_C Linux - General 17 07-27-2006 05:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration