i have the following script:
#!/bin/bash
for ((i=1; i<=$#; i++));
do
if [ ! -d $i ]
then
echo $i its not a directory >> file.txt
else
DIRECTORY=$(ls -l $i | grep ^d | wc -l)
LINK=$(find $i -type l | wc -l)
FILE=$(ls -A $i | wc -l)
echo `date "+%H:%M:%S %d-%m-%y"` directory $i file count: $FILE link count: $LINK subdirectory count: $DIRECTORY >> file.txt
fi
done
this script count the subdirectories, links and files from a directory entered by parameter(it can be more than 1).
i am struggling with the loop, it returns "echo $i its not a directory" reading $i as 1 instead of $1, i do understand why it does that but i am starting with scripts and don't know how to fix this. i think a "while" can be a substitute for that "for" but don't know how to use it properly.
thanks for the help!
${!i}
) also, but in your case, just looking at the arguments themselves ($@
) is simpler. – DopeGhoti May 26 '16 at 19:55find
instead. e.g.DIRECTORY=$(find "$i" -maxdepth 1 -type d ! -name '.*' -print0 | sed -z -n -e '$='
(requires a recent GNU sed for the-z
option). and use something similar for symlinks (-type l
) and regular files (-type f
). The! -name '.*'
excludes hidden dot-files/dirs from being counted. – cas May 27 '16 at 09:04