Operating System - HP-UX
1834975 Members
1996 Online
110073 Solutions
New Discussion

Finding files with specific size

 
SOLVED
Go to solution
Sanjay Verma
Super Advisor

Finding files with specific size

find . -local -size +9999 -print | ls -l

Hi Friends,
I am using the above command to display the files with the size specified but I am getting the files which are even smaller than the size listed above. Although I've looked into the -size n & c options, but still it doesn't help.

I am trying to search for files with size=84bytes and delete those. Am I missing something somewhere...??
Co-operation - The biggest chain reaction
11 REPLIES 11
Michael Steele_2
Honored Contributor
Solution

Re: Finding files with specific size


"...-size +n..." equates to :
+ : greater than
n : n blocks of 512 bytes.

"...-size -n..."
- : less than n blocks.

"...-size n..."
equal to n blocks.

"...-size +nc..."
+ : greater than
nc : characters (* one char = on byte *)

For "...size=84 bytes and delete..."

cd /dir
find . -size 84c -exec rm {} \;
Support Fatherhood - Stop Family Law
John Poff
Honored Contributor

Re: Finding files with specific size

Hi,

I'm not sure how to find files of an exact size using the 'find' command since it uses blocks for the size. I did figure out a way to do it using Perl. There is a Perl utility called find2perl which will take the parameters for a find command and write a little Perl script to do the same thing. I did that and modified the script to find files of exactly 84 bytes. I've posted the script as an attachment.

JP
T G Manikandan
Honored Contributor

Re: Finding files with specific size

#find -size 84c -exec ll {} \;
John Poff
Honored Contributor

Re: Finding files with specific size

Oh, never mind. I guess I should do a 'man find' sometime. Duh! :)

JP

Sanjay Verma
Super Advisor

Re: Finding files with specific size

Hi Fred, John & Mainkandan
That's really great to have good people like you. Yes, I've implemented the same and it's working fine. Still doing some more cosmetic changes in the perl script to suit the needs.

Would you be able to indicate the possibility of merging the 2 commands into 1, i.e., display it first and then delete it?
find -size 84c -exec ll {} \;
find . -size 84c -exec rm {} \;
Co-operation - The biggest chain reaction
T G Manikandan
Honored Contributor

Re: Finding files with specific size

you can use this format which will ask for removing files interactively.


#find -size 84c -ok rm {} \;
H.Merijn Brand (procura
Honored Contributor

Re: Finding files with specific size

Though the perl script is OK, it's rather unusable that way. These things are for extending, not for command line use.


# perl -MFile::Find -e'find(sub{-f&&-s==84&&print},".")'

does find all files with size 84, and in a system independant way. Here it is on cygwin:

LT03:/tmp 6 $ ll ????.???
-rwxrwxrwx+ 1 Administ None 32768 Nov 5 21:49 MMC3.tmp*
-rwxrwxrwx+ 1 Administ None 32768 Nov 5 21:49 MMC4.tmp*
-rwxrwxrwx+ 1 Administ None 32768 Nov 5 21:49 MMC5.tmp*
-rwxrwxrwx+ 1 Administ None 10678 Dec 2 16:22 Off2.tmp*
-rwxrwxrwx+ 1 Administ None 11158 Dec 5 07:28 Off3.tmp*
-rwxrwxrwx+ 1 Administ None 13558 Feb 11 18:40 Off4.tmp*
-rw-rw-rw- 1 lt03 None 1824 Jan 10 15:03 XWin.log
LT03:/tmp 7 $ perl -MFile::Find -le'find(sub{-f&&-s==1824&&print},".")'
XWin.log
LT03:/tmp 8 $

replace the 'print' with any action you like

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Sanjay Verma
Super Advisor

Re: Finding files with specific size

Hi Procura, Will appreciate if you can explain your comments in detail. -Sanjay
Co-operation - The biggest chain reaction
H.Merijn Brand (procura
Honored Contributor

Re: Finding files with specific size

# perl -MFile::Find -e'find(sub{-f&&-s==84&&print},".")'

option -M means use module ...
in this case, I want to use File::Find, a module shipped with the perl5 CORE distribution, giving you system independent 'find' functionality in perl
use 'man File::Find' to find out more about it

here's the script in verbose mode:

--8<---
#!/usr/bin/perl

use strict;
use warnings;

use File::Find;

my @dirs_to_find = (".");

sub action
{
-f or return; # only 'files' please, no dir's or pipes
-s == 84 or return; # Only need files with size 84
unlink $File::Find::name; # remove the file
}, @dirs_to_find);
-->8---

clear?

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
H.Merijn Brand (procura
Honored Contributor

Re: Finding files with specific size

uhh, thinko's

Change
--8<---
sub action
{
-f or return; # only 'files' please, no dir's or pipes
-s == 84 or return; # Only need files with size 84
unlink $File::Find::name; # remove the file
}, @dirs_to_find);
-->8---

to

--8<---
sub action
{
-f or return; # only 'files' please, no dir's or pipes
-s == 84 or return; # Only need files with size 84
unlink $File::Find::name; # remove the file
}
find (\&action, @dirs_to_find);
-->8---

I'm so used to the 'find (sub{},".")' notation, that I used it :/

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Robin Wakefield
Honored Contributor

Re: Finding files with specific size

Hi,

If you want to do the ll and rm in "one go" with find:

find . -size 84c -type f | xargs -i echo "ll {};rm {}" |sh

rgds, Robin.