Is there an efficient way in finding and deleting files?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Is there an efficient way in finding and deleting files?
# 1  
Old 05-21-2008
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? Smilie
# 2  
Old 05-21-2008
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 04:38 AM.. Reason: added code tags
# 3  
Old 05-21-2008
Thanks Penchal!!
# 4  
Old 05-27-2008
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. Smilie

So, is there any way in making this work? Again, if I use xargs here, will this work?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

More efficient for loop on files

is there a more efficient way to write the following code: for eachlfile in $(ls -ltcrd ${BACKUPDIR}/${BACKUPNAME}*/content_${BACKUPNAME}* 2>/dev/null | awk '{print $NF}') do echo "==========================" ... (4 Replies)
Discussion started by: SkySmart
4 Replies

2. Shell Programming and Scripting

Most efficient method to extract values from text files

I have a list of files defined in a single file , one on each line.(No.of files may wary each time) eg. content of ETL_LOOKUP.dat /data/project/randomname /data/project/ramname /data/project/raname /data/project/radomname /data/project/raame /data/project/andomname size of these... (5 Replies)
Discussion started by: h0x0r21
5 Replies

3. Shell Programming and Scripting

How to sort and compare files in more efficient manner?

Hello All, Iam using below method to sort and compare files. First iam doing sorting and changing the same file and then doing comparing and taking the final result to another file. sort -o temp.txt file1 mv temp.txt file1 sort -o temp.txt file2 mv temp.txt file2 sort -o temp.txt... (6 Replies)
Discussion started by: Vikram_Tanwar12
6 Replies

4. Shell Programming and Scripting

Finding a match and deleting the line above to it..

Hi All, I have a file with the data: Sun is Hot Moon is cool ; -- Mon Sep 10 08:54:10 CDT 2012 -- Mon Sep 11 08:54:10 CDT 2012 -- Mon Sep 12 08:54:10 CDT 2012 revoke connect from SREE; delete from = 'SREE'; grant connect to SREE with 'fastcar8'; I want to remove the line above... (8 Replies)
Discussion started by: raosr020
8 Replies

5. Shell Programming and Scripting

help with finding text and deleting line

HI All, I need to search for a particular pattern input by the user in order to delete the line. My username.txt has username@email.com:John:149.0.3.4:1 username1@email.com:Harry:149.0.3.4:1 username1@email.net:Alex:149.0.3.4:1 username1@email.edu:Nemo:149.0.3.4:1 The program i written ... (3 Replies)
Discussion started by: ichar
3 Replies

6. Shell Programming and Scripting

Finding a flatfile & deleting first line

I have a small script where I want to see if a file exists & then delete the first line from it. I have code to help me find if the file exists, but I am unsure as to how to then take in the answer and remove the first line from the flatfile: This is what I have so far just to output if the... (3 Replies)
Discussion started by: fatalxkiss
3 Replies

7. Shell Programming and Scripting

Finding duplicate lines and deleting folders based on them

Hi, I have research data, which is organized to 100 folders numbered 00-99. I have many sets of 100 folders, for different values of initial parameters. For some reason, the computer that ran the program to gather the data, didn't always create a unique seed for each folder. I anticipated that... (1 Reply)
Discussion started by: Jopi
1 Replies

8. Shell Programming and Scripting

Deleting / finding files older than X days missess a day

Hi When trying to find and delete files which are, say, 1 day, the find command misses a day. Please refer the following example. xxxd$ find . -type f -ctime +1 -exec ls -ltr {} \; total 64 -rw-rw-r-- 1 oracle xxxd 81 Apr 30 11:25 ./ful_cfg_tmp_20080429_7.dat -rw-rw-r-- 1... (4 Replies)
Discussion started by: guruparan18
4 Replies

9. UNIX for Dummies Questions & Answers

Help with finding certain files, and then deleting

I must really be bad at life today, because I couldn't even find a search option on the forums before asking for help, so I apologize if this is already listed somewhere. I'm pretty new to UNIX, and basically only know enough to be dangerous. I have been appointed a task to basically go to a... (2 Replies)
Discussion started by: Puck
2 Replies

10. Shell Programming and Scripting

finding duplicate files by size and finding pattern matching and its count

Hi, I have a challenging task,in which i have to find the duplicate files by its name and size,then i need to take anyone of the file.Then i need to open the file and find for more than one pattern and count of that pattern. Note:These are the samples of two files,but i can have more... (2 Replies)
Discussion started by: jerome Sukumar
2 Replies
Login or Register to Ask a Question