0

As we know, ls -S will list all files ordered by size.

What I'm trying to do is to list all files in the directory /usr ordered by size. So I executed the command as below:

find /usr -type f -exec ls -lS {} \;

However, it doesn't seem that this command lists all files ordered by size... -S doesn't work here.

0

2 Answers 2

4

You've explicitly asked find to search for files (-type f), and when you find one, you've asked it to execute ls -lS on it. You can't even fix this with the GNU find extension of + to pass more than one matching file at a time, as there may (likely) be too many files to pass to ls at once.

Instead, using GNU find, ask it to print the size of the file and the filename, then pass it to sort, all null-delimited.

find /usr -type f -printf "%s %p\n\0"|sort -zn

Sample output:

0 /usr/lib/python2.7/dist-packages/pkg_resources/_vendor/__init__.py
0 /usr/lib/python2.7/dist-packages/python_debian-0.1.30.egg-info/requires.txt
0 /usr/lib/python2.7/email/mime/__init__.py
... output omitted ...
24013304 /usr/lib/gcc/i686-linux-gnu/6/cc1plus
25675008 /usr/lib/i386-linux-gnu/libicudata.so.57.1
49547156 /usr/lib/i386-linux-gnu/libLLVM-3.9.so.1
1

If you want the output in the format of ls -l, then you can use

find . -type f -exec ls -l {} \;|sort -n -k5

Sample

$ find . -type f -exec ls -l {} \;|sort -n -k5
-rw-rw----+ 1 utsav utsav 0 Mar  6 02:23 ./file5.txt
-rw-rw----+ 1 utsav utsav 12 Mar  6 01:57 ./a.txt
-rw-rw----+ 1 utsav utsav 15 Mar  6 02:10 ./foo.txt
-rw-rw----+ 1 utsav utsav 15 Mar  6 02:11 ./c.txt
-rw-rw----+ 1 utsav utsav 27 Mar  6 01:57 ./b.txt

You must log in to answer this question.

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