So I'm creating a script that counts the size of all the files in the current directory and all the files in the subdirectories recursively:
#!bin/bash
Count () {
size=0
items=`ls "${1}/"`
for item in $items
do
if [ ! -d $item ]
then
cursize=`ls -l $item | awk '{ print $6 }'`
size=$[$size+$cursize]
else
echo "$1/$item"
Count $1/$item
size=$[$size+$?]
fi
done
echo "Done"
return $size
}
Count ~
echo "$?"
However when I run the script I get the following output:
/home/161161/backup
Done
/home/161161/dir
ls: xoe1.txt: No such file or directory
script.sh: line 11: 28+: syntax error: operand expected (error token is "+")
1
xoe1.txt is a file inside the dir directory and I don't know why it makes this problem when clearly when I do an ls -l on the directory:
ls -l dir
total 4
-rw-r--r-- 1 161161 domain users 23 Jun 2 22:55 test1.txt
-rw-r--r-- 1 161161 domain users 0 Jun 2 15:27 test2.txt
-rw-r--r-- 1 161161 domain users 0 Jun 2 15:27 test3.txt
-rw-r--r-- 1 161161 domain users 0 Jun 2 22:42 xoe1.txt <--
-rw-r--r-- 1 161161 domain users 0 Jun 2 22:42 xor1.txt
[161161@os ~]$
It shows that file does in fact exits.
Any ideas?
ls
and quote your variables. – jasonwryan Jun 02 '18 at 21:47find ... -type f -printf '%s\n'
– Hauke Laging Jun 02 '18 at 22:09ls -l *xoe1.txt* | cat -A
shows such problems. – Hauke Laging Jun 02 '18 at 22:18