We're just starting a UNIX class and are learning a variety of Bash commands. Our assignment involves performing various commands on a directory that has a number of folders under it as well.
I know how to list and count all the regular files from the root folder using:
find . -type l | wc -l
But I'd like to know where to go from there in order to find the largest file in the whole directory. I've seen somethings regarding a du
command, but we haven't learned that, so in the repertoire of things we've learned I assume we need to somehow connect it to the ls -t
command.
And pardon me if my 'lingo' isn't correct, I'm still getting used to it!
This will find the largest file or folder in your present working directory:
To find the largest file in all sub-directories:
Try following command :
This will print the largest file name and size and more than 500M. You can move the
if($1 > 500000)
,and it will print the largest file in the directory.du -aS /PATH/TO/folder | sort -rn | head -2 | tail -1
or
du -aS /PATH/TO/folder | sort -rn | awk 'NR==2'
To find the top 25 files in the current directory and its subdirectories:
find . -type f -exec ls -al {} \; | sort -nr -k5 | head -n 25
This will output the top 25 files by sorting based on the size of the files via the "sort -nr -k5" piped command.
Same but with human-readable file sizes:
find . -type f -exec ls -alh {} \; | sort -hr -k5 | head -n 25
On Solaris I use:
or
because anything else posted here didn't work. This will find the largest file in
$PWD
and subdirectories.This script simplifies finding largest files for further action. I keep it in my ~/bin directory, and put ~/bin in my $PATH.