I am trying to find the largest file in a directory recursively. If there is a subdirectory inside of that directory the function needs to go inside that directory and check to see if the largest file is there. Once the largest file is found the output is displayed with the relative path name and the name and size of the largest file.
EX:
dude@shell2 (~...assignment/solutions) % bash maxfile.sh ~/test
class/asn
dude.h.gch: 9481628
This is what I have:
#!/bin/sh
clear
recursiveS() {
for d in *; do
if [ -d $d ]; then
(cd $d; echo $(pwd)/$line; du -a; recursiveS;)
fi
done
}
recursiveS
I have been stuck for a while now. I cannot implement this by pipelining a number of existing Unix tools. Any ideas would be nice!
Best Answer
use
find
(here assuming GNUfind
) to output file names with the file size. sort. print out the largest one.That assumes file paths don't contain newline characters.
Using a loop in
bash
with the GNU implementation ofstat
:This will be significantly slower than the find solution. That also assumes that file names don't end in newline characters and will skip hidden files and not descend into hidden directories.
If there's a file called
-
in the current directory, the size of the file open on stdin will be considered.Beware that versions of
bash
prior to 4.3 followed symbolic links when descending the directory tree.