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!
That is quite simpler way to do it:
And you'll get this:
8445 examples.desktop
Quote from this link-
Try the following one-liner (display top-20 biggest files):
or (human readable sizes):
So these aliases are useful to have in your rc files:
outputs
If you just want the filename:
This avoids using
awk
and allows you to use whatever flags you want inls
.Caveat. Because
xargs
tries to avoid building overlong command lines, this might fail if you run it on a directory with a lot of files becausels
ends up executing more than once. It's not an insurmountable problem (you can collect thehead -n 1
output from eachls
invocation, and runls -S
again, looping until you have a single file), but it does mar this approach somewhat.This lists files recursively if they're normal files, sorts by the 7th field (which is size in my
find
output; check yours), and shows just the first file.The first option to
find
is the start path for the recursive search. A -type off
searches for normal files. Note that if you try to parse this as a filename, you may fail if the filename contains spaces, newlines or other special characters. The options tosort
also vary by operating system. I'm using FreeBSD.A "better" but more complex and heavier solution would be to have
find
traverse the directories, but perhaps usestat
to get the details about the file, then perhaps useawk
to find the largest size. Note that the output ofstat
also depends on your operating system.To list the larger file in a folder
The output of
ls -sh
is a sizeds
and humanh
understandable view of the file size number.You could use
ls -shS /pathFolder | head -n 1
. The biggerS
fromls
already order the list from the larger files to the smaller ones but the first result its the sum of all files in that folder. So if you want just to list the bigger file, one file, you need tohead -n 2
and check at the "second line result" or use the first example withls sort head
.