I am trying to get the total size of my five largest files found in a directory but I am not getting du to work on my list. I have two ways of finding and sorting, the five largest files.
#!/bin/bash
DIR=$1 #The starting directory path
if [ ! -d "$DIR" ]; then #if the directory is not found
echo "The directory doesn't exist!"
exit 1
fi
echo "Five largest files using ls are:"
test_ls=$( ls -lhR "$DIR" | grep '^-' | sort -r -k 5 -h | head -n 5 )
du -ch "$test_ls"
echo "Five largest files using find/DU are:"
test_find=$( find "$DIR" -type f -exec du -ch {} + | sort -rh | head -n 5 )
du -ch "$test_find"
echo "Total number of files: "
ls -lhR "$DIR" | grep '^-' | wc -l
echo "Total size of files: "
du -sh "$DIR" | awk '{print $1}'
If I apply du on the ls variable I get :
du: invalid option -- 'r'
du: invalid option -- 'w'
du: invalid option -- '-'
du: invalid option -- 'r'
and if I apply it on the find variable I get this for each of the five files
du: cannot access '429M': No such file or directory
Both the ls version and find version works fine for listing the five largest files under the given directory, but I am relly lost in how I could add their sizes together.