Print out list of files less than specified file size

command linefileslsscriptingsearch

I'm trying to create a script that can be executed to search a list of files, compare the file size field to the specified file size, and then display the files that are less than the specified file size.

I understand that I must use 'ls -l' in order to get a detailed list of files. However, how can I go about searching the list and pulling the files?

Best Answer

Your approach is clumsy (and fair to say wrong). There are dedicated tools for these sort of tasks, find is one of those.

For example, to find all files in the current directory that are less than 1 MiB (1048576 Bytes), recursively:

find . -type f -size -1048576c

Or with shells that provide such size based glob qualifiers e.g. zsh, recursively:

print -rl -- **/*(.L-1048576)

Here, contrary to find above, without the hidden files. Add the D glob qualifier to include them.

Related Question