I need to find the largest file in the current and subsequent directory. I tried
ls -Rlh | awk '{print $3 " " $5 " " $9}'
but do not know if it is ok, how to sort and select the largest file.
I need to find the largest file in the current and subsequent directory. I tried
ls -Rlh | awk '{print $3 " " $5 " " $9}'
but do not know if it is ok, how to sort and select the largest file.
GNU find + sort + head solution (for any directory depth level), assuming file paths don't contain newline characters:
find . -type f -printf "%s %p\n" | sort -nr | head -1
%s - format specificator pointing to file size (in bytes)%p - format specificator pointing to file namesort -nr - sort records numerically in reversed orderhead -1 - print the TOP first line/recordTo get a human-readable file size value - extend the pipeline with GNU numfmt command (if supported):
find . -type f -printf "%s %p\n" | sort -nr | head -1 | numfmt --to=si
With zsh, for the biggest regular file:
ls -ld -- **/*(.DOL[1])
(of course you can replace ls -ld -- with any command. If using GNU ls or compatible see also the -h option for human readable sizes)
.: only regular files (not directories, symlinks, devices, fifos...)D: include hidden ones and descend into hidden dirsOL: reverse-ordered by size (Length).[1]: only the first match.If there are ties, you'll get any one of them at random. If you want the first in alphabetical order, add an extra on (order by name) to sort ties alphabetically.
zsh" part. What shell are you trying it in? In which way does it not work?
– Stéphane Chazelas
Nov 20 '17 at 16:16
bash, you can always do zsh -c 'ls -ld -- **/*(.DOL[1])' but if you're often finding yourself looking for the quickest way, you may want to consider switching your shell to zsh (also for safest way , best way and more efficient way).
– Stéphane Chazelas
Nov 20 '17 at 16:32