The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Deleting / finding files older than X days missess a day guruparan18 Shell Programming and Scripting 4 05-08-2008 03:09 AM
df -k and deleting files ajayr111 UNIX for Dummies Questions & Answers 4 03-14-2007 04:03 PM
Help with finding certain files, and then deleting Puck UNIX for Dummies Questions & Answers 2 03-13-2007 08:18 AM
finding duplicate files by size and finding pattern matching and its count jerome Sukumar Shell Programming and Scripting 2 12-01-2006 12:20 AM
Deleting old files shiroh_1982 Shell Programming and Scripting 2 06-21-2006 01:42 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-20-2008
Registered User
 

Join Date: May 2008
Location: India
Posts: 30
Stumble this Post!
Is there an efficient way in finding and deleting files?

Hi

I have a script to find and delete the files which are say, noDaysOld, I am interested to find the number of such files I am fniding for deleting and then deleting it.

So, the script I wrote, first finds the number of such files and then deletes, clearly this is two different steps.

Step 1:
Code:
 noOfFiles=$(find $dirPath -type f -ctime $noDaysOld  | wc -l)
And the $noOfFiles gets an entry in log.

Step 2:
Code:
find $dirPath -type f  -ctime $noDaysOld  -exec rm -f {} \;
The interesting question is, is there one single step which does all this?

Another point is, how can we write the command in step 2 with "xargs"? Will the command performs better with xargs?
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-20-2008
Registered User
 

Join Date: Apr 2008
Location: Bangalore
Posts: 120
Stumble this Post!
One way is Instead of having two find commands, U can store the output of first find in a file.
Then u can do wc -l and rm.

like..
Code:
find $dirPath -type f -ctime $noDaysOld   >> output 2>/dev/null
noOfFiles=`wc -l output`
cat output | xargs rm
This will be faster compared to the two find command approach.

xargs is a command that accepts list of words and provides those as input to the given command.

Iam think that calling a shell script in the -exec is a better approach..

Thanks
Penchal

Last edited by Yogesh Sawant; 05-21-2008 at 12:38 AM. Reason: added code tags
Reply With Quote
  #3 (permalink)  
Old 05-20-2008
Registered User
 

Join Date: May 2008
Location: India
Posts: 30
Stumble this Post!
Thanks Penchal!!
Reply With Quote
  #4 (permalink)  
Old 05-26-2008
Registered User
 

Join Date: May 2008
Location: India
Posts: 30
Stumble this Post!
Is it possible to use teecommand here?

I tried and I didn't get any error (any results either), the command was,

Code:
find . -type f \( -ctime $noDaysOld -o -ctime +$noDaysOld \) |tee wc -l > noOfFiles | rm -f {} \;
The files were not removed.

So, is there any way in making this work? Again, if I use xargs here, will this work?
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 09:05 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0