Click to See Complete Forum and Search --> : remove files of size xK


kowalsky
06-11-2001, 05:05 PM
Hi all,
I was wandering if anybody has already a short script (shell/perl) that removes only files of some size (< 50 K let's say) in a given directory,
Thanks,
kowalsky

Ben Briggs
06-11-2001, 06:32 PM
Here's a link from CCAE (http://www.codeexamples.org). You can edit it to delete the file if what is returned by stat("$_")[7] is less than or equal then the desired size (it's in bytes I believe).

File Size (http://www.codeexamples.org/cgi-bin/c2h/c2h.cgi?type=HTMLdetail&filename=filesize.pl)

[edit] Fixed Link

[ 11 June 2001: Message edited by: Ben Briggs ]

jemfinch
06-11-2001, 11:33 PM
#!/usr/bin/env python

import os, stat

if len(sys.argv) < 3:
print 'Usage: %s <size> <file> [file ...]' % sys.argv[0]
sys.exit(-1)

size = int(sys.argv[1])

for file in sys.argv[2:]:
stats = os.stat(file)
if stats[stat.ST_SIZE] < size:
print file


That should work. It's untested. To remove the file, replace "print file" with "os.remove(file)". Obviously it's easily customizable to whatever your needs are.

Someone (Ben Briggs, <nudge> :)) should comment this or expand on it somehow and throw it on CCAE so Perl isn't the only language with an example of the stat function :)

Jeremy

Ben Briggs
06-12-2001, 11:41 AM
Jemfinch:

Nice subtlty :). I will comment it and send it over to CCAE. If I ever get my act together, I'll write that spiffy ls replacement you suggested and know all about stat :).

EyesWideOpen
06-12-2001, 12:03 PM
FYI.

Someone posted a command to do it if you don't want to go the sript route.
http://www.linuxnewbie.org/cgi-bin/ubbcgi/ultimatebb.cgi?ubb=get_topic&f=3&t=004919

kowalsky
06-13-2001, 08:32 PM
thanks everybody, I eventually came up with:

#!perl

$dir="D:\\ptest";

opendir( DIR, $dir ) || die "Cannot open directory $dir \n$!";
while ( defined($file = readdir DIR) ) {
next if ($file !~/(.*txt)/);
$file1 = $dir."\\".$file;
@st = stat $file1;
print "$file1\t$st[7]";
if ($st[7] < 5000){
print "\tthis file will be removed!\n";
system ("del $file1");
}
else{
print "\n";
}
}
closedir( DIR );

and this is for (oh! we're trapped, dammit) Windows; look at the beauty of Linux:

" demian: Hey, this is linux! Of course there is a way:
find . -size -10k -exec rm {} \;"

Interesting to see that stat $file would give you zilch because stat would not understand who file_01.txt is, so I had to rebuild the whole path. Going with the whole path from the beginning defeats the purpose of using opendir & readdir!!! I spent almost 3 hours for 14 lines of code... There's no future in programming for me :o
kowalsky

jemfinch
06-13-2001, 11:10 PM
Originally posted by kowalsky:
Interesting to see that stat $file would give you zilch because stat would not understand who file_01.txt is, so I had to rebuild the whole path. Going with the whole path from the beginning defeats the purpose of using opendir & readdir!!! I spent almost 3 hours for 14 lines of code... There's no future in programming for me


At least not in Perl. Perhaps you should try Python instead. It's a bit higher level and certainly much more easy to learn.

Jeremy