Deleting a million of files ..


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Deleting a million of files ..
# 1  
Old 04-14-2014
Deleting a million of files ..

Hi,

Which way is faster
Code:
rm -rf /path/

or
Code:
find / -name  -exec rm {} \;

and why?
# 2  
Old 04-14-2014
Quote:
Originally Posted by cain82
Hi,

Which way is faster
Code:
rm -rf /path/

or
Code:
find / -name  -exec rm {} \;

and why?
Code:
rm -rf /path/

will be orders of magnitude faster than
Code:
find / -name  -exec rm {} \;

Which operation do you think is more costly, invoking the rm utility once to remove a million files or invoking the rm utility one million times having each invocation remove one file?
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 04-14-2014
plane 'rm' is faster than with 'find' as find will consume time and 'rm' with '-exec' is slower as it will remove one file at a time...
If you have no other option but to use 'find', I would suggest you use 'xargs' so that all the files will be removed at a time.
Code:
find / -name "*.log" | xargs rm

This User Gave Thanks to SriniShoo For This Post:
# 4  
Old 04-14-2014
Quote:
Originally Posted by Don Cragun
Code:
rm -rf /path/

will be orders of magnitude faster than
Code:
find / -name  -exec rm {} \;

Which operation do you think is more costly, invoking the rm utility once to remove a million files or invoking the rm utility one million times having each invocation remove one file?
I suppose - once rm is faster than million times Smilie

---------- Post updated at 02:29 AM ---------- Previous update was at 02:28 AM ----------

What about
Code:
find /path/ -type f -delete

is this a fastest method to remove a million files more than 200gb ?
# 5  
Old 04-14-2014
Quote:
Originally Posted by SriniShoo
plane 'rm' is faster than with 'find' as find will consume time and 'rm' with '-exec' is slower as it will remove one file at a time...
If you have no other option but to use 'find', I would suggest you use 'xargs' so that all the files will be removed at a time.
Code:
find / -name "*.log" | xargs rm

With a million files to be removed, xargs rm will not remove all of the files in one invocation of rm. Furthermore, if any of the files to be removed have names containing whitespace characters, single quotes, double quotes, or backslashes, using xargs that way will not work.

If you need to use find to select a subset of files to be removed by name, size, date, etc., use:
Code:
find /path selection_criteria_operands -exec rm {} +

Using the find utility's -exec command {} \; executes command once for each selected file. Using the find utility's -exec command {} + executes command with groups of one or more file operands chosen such that system ARG_MAX limits will not be exceeded. (This last form matches what xargs tries to do, but doesn't suffer from the filename limitations that you run into by passing filenames through a pipe and having xargs try to find filename boundaries.)
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 04-14-2014
Quote:
Originally Posted by cain82
I suppose - once rm is faster than million times Smilie

---------- Post updated at 02:29 AM ---------- Previous update was at 02:28 AM ----------

What about
Code:
find /path/ -type f -delete

is this a fastest method to remove a million files more than 200gb ?
You're comparing apples and oranges:
Code:
rm -rf directory

attempts to remove directory and any links to files (of any type) in the file hierarchy rooted in directory.

On the other hand:
Code:
find directory -type f -delete

attempts to remove only links to regular files in the file hierarchy rooted in directory (leaving the directory structure and all other file types as is). Furthermore, the -delete primary in find is an extension to the standards that is available on some systems, but is not available on other systems.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 7  
Old 04-15-2014
If actual elapse time is critical because of ongoing processing, could I suggest:-
Code:
mv /path /path-to-del
mkdir -m 777 /path
chown user:group /path

Doing this will be a near instantaneous operation and then with the directory renamed and a new empty one created, your processing can continue whilst you dispose of what is now called /path-to-del at your leisure.

The -m 777 on the mkdir sets the permissions for the directory. The user:group on the chown sets both the owner and group in one operation, rather than having to do a chown and a chgrp


I might be way off course, but I was confused as to why the timing matters so much. If you still want a choice for your original question, I would go with rm -f /path


Another alternate might be available if this is a filesystem you can unmount for a few moments:-
Code:
umount /path
newfs your-options /dev/logical-volume
mount /path

You don't give us an OS or any clue about what devices the disks are on, so I've tried to be generic here. You make have to use mkfs, crfs, fscreate or other things depending what you are running. Of course, this may not be an option.



I hope that these suggestions might help.

Robin
Liverpool/Blackburn
UK
This User Gave Thanks to rbatte1 For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Zip million files taking 12 hours or more

Hi I have task to zip files based on modified time but they are in millions and it is taking lot of time more than 12 hours and also eating up high cpu is there any other / better way to handle it quickly with less cpu consumptionfind . ! -name \"*.gz\" -mtime +7 -type f | grep -v '/.*/' |... (2 Replies)
Discussion started by: reldb
2 Replies

2. Shell Programming and Scripting

Bash script deleting my files, and editing files in subdirectories question

#!/bin/bash # name=$1 type=$2 number=1 for file in ./** do if then filenumber=00$number elif then filenumber=0$number fi tempname="$name""$filenumber"."$type" if (4 Replies)
Discussion started by: TheGreatGizmo
4 Replies

3. UNIX for Dummies Questions & Answers

Pls. help with script to remove million files

Hi, one of the server, log directory was never cleaned up. We have so many files. I want to remove all the files that starts with dfr* but I get error message when I use the *. rm qfr* bash: /usr/bin/rm: Arg list too long I am trying to write this script but not working. ... (4 Replies)
Discussion started by: samnyc
4 Replies

4. Shell Programming and Scripting

Fast processing(mv command) of 1 million+ files using find, mv and xargs

Hi, I'd like to ask if anybody can help improve my code to move 1 million+ files from a directory to another: find /source/dir -name file* -type f | xargs -I '{}' mv {} /destination/dir I learned this line of code from this forum as well and it works fine. However, file movement is kinda... (6 Replies)
Discussion started by: agentgrecko
6 Replies

5. Shell Programming and Scripting

Matching 10 Million file records with 10 Million in other file

Dear All, I have two files both containing 10 Million records each separated by comma(csv fmt). One file is input.txt other is status.txt. Input.txt-> contains fields with one unique id field (primary key we can say) Status.txt -> contains two fields only:1. unique id and 2. status ... (8 Replies)
Discussion started by: vguleria
8 Replies

6. Shell Programming and Scripting

Deleting files

Hi all, I have developed a shell script to copy the files from source to destination and simultaneously to delete the copied files in source. I can copy the files but the files cannot be deleted in source side. (3 Replies)
Discussion started by: Venkatesan
3 Replies

7. Shell Programming and Scripting

AIX system.... deleting files in remote directory after retrieving files

Hi Friends, I am new to this , I am working on AIX system and my scenario is to retrive the files from remote system and remove the files from the remote system after retreving files. I can able to retrieve the files but Can't remove files in remote system. Please check my code and help me out... (3 Replies)
Discussion started by: vinayparakala
3 Replies

8. Solaris

Need to know command to delete more than 3 million files from /var/spool/clientmqueue

Hi I need to delete more than 3 million files from /var/spool/clientmqueue. When I give the following command to delete the files, I get the error # pwd /var/spool/clientmqueue # rm -f * /usr/bin/rm: arg list too long Please tell me how can I delete the files (5 Replies)
Discussion started by: sb200
5 Replies

9. Shell Programming and Scripting

Need help comparing two files and deleting some things in those files!

So I have two files: File1 pictures.txt 1.1 1.3 dance.txt 1.2 1.4 treehouse.txt 1.3 1.5 File2 pictures.txt 1.5 ref2313 1.4 ref2345 1.3 ref5432 1.2 ref4244 dance.txt 1.6 ref2342 1.5 ref2352 1.4 ref0695 1.3 ref5738 1.2 ref4948 1.1 treehouse.txt 1.6 ref8573 1.5 ref3284 1.4 ref5838... (24 Replies)
Discussion started by: linuxkid
24 Replies
Login or Register to Ask a Question