Linux Question

List files greater than 100K in bash

I want to list the files recursively in the

HOME
directory. I'm trying to write my own script , so I should not use the command
find
or
ls
. My script is:

#!/bin/bash

minSize=102400;

printFiles() {
for x in "$1/"*; do
if [ -d "$x" ]; then
printFiles "$x";
else
size=$(wc -c "$x");
if [[ "$size" -gt "$minSize" ]]; then
echo "$size";
fi
fi
done
}

printFiles "/~";


So, the problem here is that when I run this script, the terminal throws
Line 11: division by 0
and
/home/gandalf/Videos/*: No such file or directory
. I have not divided by any number, why I'm getting this error?. And the second one?

Alternatively, I can't use
find
or
ls
because I have to display the files one by one asking to the user if he want to see the next file or not. This is possible using the command
find
or
ls
or only can be done writing my own function?

Thanks.

Answer
size=$(wc -c "$x");

That's the line that is failing. When you run that wc command manually you should be able to see why:

$ wc -c /tmp/out
5 /tmp/out

The output contains not only the file size but also the file name. So you can't use $size with the -gt comparator on the next line. One way to fix that is to change the wc line to use cut (or awk, or sed, etc) to keep just the file size.

size=$(wc -c "$x" | cut -f1 -d " ")

A simpler alternative suggested by @mklement0:

size=$(wc -c < "$x")
Comments