How do I find the largest 10 files in a given directory, with Perl or Bash?
EDIT:
- I need this to be recursive.
- I only want to see large files, no large directories.
- I need this to work on Mac OS X 10.6 ('s version of find).
How do I find the largest 10 files in a given directory, with Perl or Bash?
EDIT:
This prints the 10 largest files recursively from current directory.
find . -type f -printf "%s %p\n" | sort -nr | awk '{print $2}' | head -10
$ find . -type f -printf "%s %p\n" | sort -nr | head -10
works. –
Reluctant -printf
option, you can try this: find . -type f -ls | awk '{print $7" "$11}' | sort -nr | head -10
–
Fabri find . -type f -ls | awk '{print $7" "$11}' | sort -nr | head -10
prints nothing. –
Reluctant $ alias ducks
alias ducks='du -cs * |sort -rn |head -11'
This is a way to do it in perl. (Note: Non-recursive version, according to earlier version of the question)
perl -wE 'say for ((sort { -s $b <=> -s $a } </given/dir/*>)[0..9]);'
However, I'm sure there are better tools for the job.
ETA: Recursive version, using File::Find:
perl -MFile::Find -wE '
sub wanted { -f && push @files, $File::Find::name };
find(\&wanted, "/given/dir");
@files = sort { -s $b <=> -s $a } @files;
say for @files[0..9];'
To check file sizes, use e.g. printf("%-10s : %s\n", -s, $_) for @files[0..9];
instead.
find
? –
Reluctant find
works internally, so I cannot say. It may also vary depending on your file system and number of files. You'd need to try it out and see. –
Alvy How about this -
find . -type f -exec ls -l {} + | awk '{print $5,$NF}' | sort -nr | head -n 10
[jaypal:~/Temp] find . -type f -exec ls -l {} + | awk '{print $5,$NF}' | sort -nr | head -n 10
8887 ./backup/GTP/GTP_Parser.sh
8879 ./backup/Backup/GTP_Parser.sh
6791 ./backup/Delete_HIST_US.sh
6785 ./backup/Delete_NORM_US.sh
6725 ./backup/Delete_HIST_NET.sh
6711 ./backup/Delete_NORM_NET.sh
5339 ./backup/GTP/gtpparser.sh
5055 ./backup/GTP/gtpparser3.sh
4830 ./backup/GTP/gtpparser2.sh
3955 ./backup/GTP/temp1.file
find's printf
option which isn't available in mac
. –
Arteriole {}
in shell part to avoid shell injection –
Shirr With recent find
(2023) and bash
:
I use GNU findutils 4.9.0
shopt -s nullglob
print0 () {
[ "$#" -eq 0 ] || printf '%s\0' "$@"
}
readarray -td '' files < <(
print0 * |
find -files0-from - -type f -printf '%b\t%p\0' |
sort -rzn |
cut -zf2 -
)
printf '%s\n' "${files[@]:0:10}"
${files[@]:0:10}
is taking files array element from key 0 to 10© 2022 - 2024 — McMap. All rights reserved.