I want to filter the output of the ls command based on file size. Any .jpg or .png files greater than 100KB should be reported in the output.

I've been able to filter the .png / .jpg files, but I'm not able to discard any files smaller than 100KB.

Here is what I'm currently using:

ls -lah | grep '.png\|.jpg'

Any ideas how I can do this?

link|improve this question

71% accept rate
feedback

3 Answers

up vote 2 down vote accepted

As others have suggested, find will allow you to find files in specified size ranges. Find outputs just the path to each file, though. Also, without further qualification, find will find all the files in the current directory and in every directory below the current directory. The following searches only the current directory and uses ls to dispaly the results.

find . -maxdepth 1 -size +200 \( -name \*.png -o -name \*.jpg \) -print | xargs ls -ldh

Note that the size is in blocks, where a block is often if not always 512 bytes.

link|improve this answer
feedback

Use find instead of ls:

find . -type f -size +100k \( -name \*.png -or -name \*.jpg \)
link|improve this answer
feedback

You can do that using find:

find . -type f -size +100k | grep '.png\|.jpg'

Where +100k specifies the size in KB, meaning that only files larger than this should be output. find also has some other nice options, for example to only list files that were created a certain amount of time ago. See man find for more details.

The above could also be rewritten as

find . -type f -size +100k -name "*.png" -o -name "*.jpg"
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.