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!
use find
(here assuming GNU find
) to output file names with the file size. sort. print out the largest one.
find . -type f -printf "%s\t%p\n" | sort -n | tail -1
That assumes file paths don't contain newline characters.
Using a loop in bash
with the GNU implementation of stat
:
shopt -s globstar
max_s=0
for f in **; do
if [[ -f "$f" && ! -L "$f" ]]; then
size=$( stat -c %s -- "$f" )
if (( size > max_s )); then
max_s=$size
max_f=$f
fi
fi
done
echo "$max_s $max_f"
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.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments