Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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.

share|improve this answer
You should mention that 'ls' must have the '-d' flag for this to work. 'ls -lh' will show the unfiltered output. – cschol Sep 3 '12 at 14:37
@cschol: The reason for the -d is to prevent ls from listing the contents of any directories in the output of find. – garyjohn Sep 3 '12 at 16:28

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"
share|improve this answer

Use find instead of ls:

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .