1

the file has been generated by

ls -Raltr -block-size=M /* > /home/root/all_files.txt

I have tried several commands with grep to no avail.

  • 2
    Parsing ls, even indirectly, is prone to failing in edge cases (groups with spaces in their name, filenames that have newlines in their name, etc). – Jeff Schaller Jul 17 at 13:00
3

The easiest way is to use -S with ls, to sort directory contents by size. Files are still grouped by directory, which might not be what you want:

ls -RaltrS --block-size=M /*

This approach might not be an option here; perhaps all_files.txt is an old file or was generated on a different computer. It's still possible to sort the filenames by file size. For a typical file, a line of output produced by ls -Raltr --block-size=M (note that --block-size starts with two hyphens instead of one) looks like this:

-rw-rw-r-- 1 owner group 1M Apr 16 05:37 file.txt

Use sort with -h (for human-readable numbers such as 1M), and with -k 5 to specify that the file sizes are in the fifth column (delimited by blank space) of text:

sort -k 5 -h /home/root/all_files.txt

Sorting the ouput of ls -Raltr --block-size=M /* that is stored in /home/root/all_files.txt can be messy because of the extra lines that do not list file sizes, so use grep first to get only the lines you want:

grep '^-' /home/root/all_files.txt | sort -k 5 -h

Here, grep '^-' discards lines that do not start with a hyphen (eg, lines that start with d for directory or l for link).

This anwswer works with GNU tools on Xubuntu 16.04. It has not been tested on MacOS or BSD systems.

  • Using ls's sorting option is the right way to go here. – Jeff Schaller Jul 17 at 13:00
1

To sort all regular files in or under the current directory by size, the zsh shell provides the handy ** globbing pattern that matches across / in pathnames (i.e. "recursively down into subdirectories). It also allows for qualifying a pattern so that you e.g. get only a match of regular files, and that the results are sorted according to the size of these files.

In the zsh shell, this pattern would look like

**/*(.OL)

This would return all regular files (.) ordered in reverse (O) size order (L).

To get the listing of just the files, use

printf '%s\n' **/*(.OL)

To get ls "long listing" output, use

ls -fl **/*(.OL)

(the -f option prevents ls from doing its own sorting of the files).

If zsh is not your ordinary shell, you can still use these commands, assuming zsh is installed on your system:

zsh -c 'ls -fl **/*(.OL)'

You could obviously also add --block-size=M to that if you're using GNU ls.

If you are interested in just the ten largest files, use the pattern

**/*(.OL[1,10])

instead.

  • I'm impressed with zsh's globbing features. Too bad I handle a fleet of servers that includes a lot of legacy systems, so I need to stick to lowest common denominator. – Wildcard Jul 18 at 23:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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