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:

 noOfFiles=$(find $dirPath -type f -ctime $noDaysOld  | wc -l) 

And the $noOfFiles gets an entry in log.

Step 2:

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? :confused:

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..

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

Thanks Penchal!!

Is it possible to use teecommand here?

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

find . -type f \( -ctime $noDaysOld -o -ctime +$noDaysOld \) |tee wc -l > noOfFiles | rm -f {} \;

The files were not removed. :frowning:

So, is there any way in making this work? Again, if I use xargs here, will this work?