Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Systems > Linux / BSD / OS X

Reply
 
LinkBack Thread Tools Display Modes
Old 04-09-2008, 10:13 PM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
remove thousands of files from a directory

i have a directory with about 650,000 files in it that i need to remove. is the best way to do it with find piped to xargs?
__________________
Mike
sde is offline   Reply With Quote
Old 04-10-2008, 12:05 AM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
Code:
ls | xargs rm
this worked much faster than find
__________________
Mike
sde is offline   Reply With Quote
Old 04-10-2008, 03:43 AM   #3 (permalink)
ShadyCraig
Recruit
 
Join Date: Sep 2005
Location: Loughborough, England, UK
Posts: 12
ShadyCraig is on a distinguished road
what's wrong with
Code:
rm *
ShadyCraig is offline   Reply With Quote
Old 04-10-2008, 08:28 AM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
Quote:
Originally Posted by ShadyCraig View Post
what's wrong with
Code:
rm *
that's what i tried first. it can't handle that many files.
__________________
Mike
sde is offline   Reply With Quote
Old 04-10-2008, 09:13 AM   #5 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,711
redhead is on a distinguished road
# find . -exec rm -f {} \;

Would be my choice.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 04-10-2008, 07:39 PM   #6 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,161
Belisarius is on a distinguished road
Can't you just go up a directory then "rm -rf <dir>"?

Barring that, redhead's idea would be my next choice.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 04-10-2008, 08:36 PM   #7 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
Quote:
Originally Posted by Belisarius View Post
Can't you just go up a directory then "rm -rf <dir>"?

Barring that, redhead's idea would be my next choice.
no, that's why i needed an alternative. 650,000 files is too many for rm to handle.

find was taking forever. ls | xargs completed within a minute.
__________________
Mike
sde is offline   Reply With Quote
Old 04-10-2008, 11:33 PM   #8 (permalink)
ShadyCraig
Recruit
 
Join Date: Sep 2005
Location: Loughborough, England, UK
Posts: 12
ShadyCraig is on a distinguished road
Quote:
Originally Posted by sde View Post
that's what i tried first. it can't handle that many files.
I though it might be, but I didn't want to try it to find out

Cheers
ShadyCraig is offline   Reply With Quote
Old 04-11-2008, 12:43 AM   #9 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
so, how does one get 650,000 files in the first place? i had a user on my server who never checked their system account for the last year or so. their mail file got very big.

i recently installed dovecot, which puts each email into an individual file, instead of all emails in 1 file, and vua la. 650k message is now 650k files
__________________
Mike
sde is offline   Reply With Quote
Old 04-11-2008, 08:09 AM   #10 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 676
DJMaze is on a distinguished road
650k file != 650k files * 4k

Sometimes it's better to have 1 file instead of 650k because a fle of 1kb takes up 4kb of space.

Based on file system this can vary from 1k (Raiser) to 64k (FAT32).

That's why you always format a drive based on what tasks a server may do
__________________

UT: Ultra-kill... God like!
DJMaze is offline   Reply With Quote
Old 04-11-2008, 09:10 AM   #11 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
no, that was supposed to be 650k messages, i.e. emails in that spool file. it was actually a couple GB in size.
__________________
Mike
sde is offline   Reply With Quote
Old 04-13-2008, 11:50 AM   #12 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
Find - exec is much slower than find |xargs rm, because in the case of n files find -exec will invoke rm n times whereas find|xargs will only invoke rm n mod <max commandline args> times, so you see a major speed up in terms of process invocation overhead.

The ultimate power combo is find -print0|xargs -0 rm -f
It uses null characters rather than whitespace to separate the output/input so it won't break in the face of files with spaces or newlines embedded in the names.
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
want to get directory files and folders in list box salmanjoo PHP 2 08-17-2005 03:39 PM
Get directory files into html links listing Wysocki PHP 1 04-01-2005 10:31 AM
reading files from a directory and printing philthee Java 4 10-29-2004 05:01 AM
Populating tree control with specified directory folders and files HanaDin Platform/API C++ 2 07-14-2004 05:16 PM
Installing and using CMUgraphics library. Valmont Standard C, C++ 12 03-29-2003 08:39 AM


All times are GMT -8. The time now is 11:23 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting