Deleting file by size

Discussion of programming on Linux, including shell scripting, perl, python, c/c++, mono, java. Whatever tickles your fancy.
azfar
Captain
Posts: 598
Joined: Tue Mar 23, 2004 1:16 am
Location: Karachi
Contact:

Deleting file by size

Postby azfar » Mon Sep 19, 2005 2:49 pm

Is there any way to delete bunch of files from a particular directory whose size is larger, smaller or similler to my given value.

I looked at largefile command but unable to understand it.

I am on SOLARIS 7.
Azfar Hashmi
Email : azfarhashmi@hotmail.com

lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Re: Deleting file by size

Postby lambda » Mon Sep 19, 2005 3:14 pm

azfar wrote:Is there any way to delete bunch of files from a particular directory whose size is larger, smaller or similler to my given value.

yes.

oh, you want the code for that. here's a one-liner to print files larger than 3,000,000 bytes:

Code: Select all

ls -1 | perl -nle 'chomp; print if (-f $_ && (stat($_))[7] > 3000000);'

if you want to delete the files instead of printing them, change "print" to "unlink".

jargon
Lieutenant Colonel
Posts: 691
Joined: Mon Oct 13, 2003 9:40 am

Postby jargon » Tue Sep 20, 2005 6:06 am

easier to use the 'find' command w/ the size argument.

e.g. , 'find . -size +10000'

and delete all files that it prints

check the man page for the system for details.
jargon

lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Postby lambda » Tue Sep 20, 2005 1:18 pm

did you try it? 'find' doesn't work for me.

wacky
Naik
Posts: 94
Joined: Thu Jun 10, 2004 7:42 pm
Location: London, UK

Postby wacky » Tue Sep 20, 2005 3:06 pm

Most of what Jargon says is correct. I've just extended the command to do what you want to achieve:

find /your/dir -size +10000c -exec rm {} \;

will delete all files in /your/dir over 10000 bytes in size.

Regards
wacky

lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Postby lambda » Wed Sep 21, 2005 12:55 am

and that'll work on solaris 7? a five year old operating system that doesn't use gnu find?

jargon
Lieutenant Colonel
Posts: 691
Joined: Mon Oct 13, 2003 9:40 am

Postby jargon » Wed Sep 21, 2005 5:08 am

lambda:
suspect!
I did try it on SunOS 5.9, so unless find was way diff. in Solaris 7 then my assumption that it would work would be wrong. But like I said it works on a SunOS 5.9 if that means anything to anyone.
jargon


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 1 guest