Operating System - HP-UX
1755778 Members
3660 Online
108838 Solutions
New Discussion юеВ

Re: Unix ls command part II

 
SOLVED
Go to solution
Landen
Occasional Contributor

Unix ls command part II

Hi,
Previous message ...
I was wondering if the ls command can list all files in a current directory with file size that is greater than some number and less than some number? ...

Actually, this is part of a script I am writing. Now, how would i be able to check if anything is returned? ie.

...
elif [ $1 -lt $2 ] ; then
find size ... -print
if [ (pseudo) find size = 0 ] ; then
echo "No such files!"
fi
fi
...

I guess the problem is that I don't know the command i would use in (pseudo) ... any ideas?

gracias ...
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor
Solution

Re: Unix ls command part II

Hi:

In your previous post I offered you the following:

If you want to use 'ls' and limit the results returned by filesize, consider this method:

# ls -l /tmp|awk '$5 > 1000 && $5 < 3000 {print $0}'

...this will return all files in the /tmp directory whose size is greater than 1000 and less than 3000.

The fifth ($5) field returned is the size of the file. If you want to pipe this command string into a file, you could then test whether that file was empty or not.

You can also add the -R' option to the 'ls' command to make the search recursive.

The bulk of this solution I suggested to you in your first post [to which you assigned *no* points, I might add].

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: Unix ls command part II

Hi,

One very easy way to do this (if you are willing to limit yourself to 512-byte block resolution) is to use 2 -size args to find connected with -a (and).

-size +1 will find all files greater than 1 block -size -5 will find all files less than 5 blocks.

LO=1
HI=5

find . \( -size +${LO} -a -size -${HI} \)


Regards, Clay
If it ain't broke, I can fix that.
Rodney Hills
Honored Contributor

Re: Unix ls command part II

If you are looking to only display a message when no files are found, then replace your code of...
if [ (pseudo) find size = 0 ] ; then
echo "No such files!"
fi

with (borrowing from Clay's example)
find . \( -size +${LO} -a -size -${HI} \) >/dev/null && echo "No such files!"



There be dragons...
Jared Westgate_1
Valued Contributor

Re: Unix ls command part II

Hello Landen,

I'm not sure what you are asking. Do you still need help with the command to show all files less than a number and greater than another number? It almost sounds like this is a component to a larger script.

Or, are you wanting some script writing help? If so, it would be helpful if you could post more of your script. Also, a little more information on what you want the final script to do would be helpful.

Thanks,

Jared
Rodney Hills
Honored Contributor

Re: Unix ls command part II

Oops, my mistake...

Use the following-
if [ `find . \( -size +${LO} -a -size -${HI} \) -print` ] ; then echo "No such Files!" ; fi
There be dragons...