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.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
The easiest way is to use
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
-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
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.
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.
zsh shell, this pattern would look like
This would return all regular files (
.) ordered in reverse (
O) size order (
To get the listing of just the files, use
printf '%s\n' **/*(.OL)
ls "long listing" output, use
ls -fl **/*(.OL)
-f option prevents
ls from doing its own sorting of the files).
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
If you are interested in just the ten largest files, use the pattern