I am new to Bash and am practicing some shell scripts. I wanted to write a script to list all the folders and their file count, if a root path is passed as a parameter value from the terminal. The problem I am facing is when I run the specific command on the terminal using ls or find it works.
Desired output:
/Users/abc/Documents : 10
/Users/abc/Documents/Subfolder1 : 11
/Users/abc/Documents/Subfolder2 : 12
...
The code is given below:
#Assign path passed from terminal to var
ROOT_PATH=$1
#list all dirs under the root path to traverse
find "$ROOT_PATH" -type d -name "*" | grep -v ".git" | while read line; do echo "$line"; done > ./Temp.txt
while IFS= read -r line
do
echo "$line"
let a=0
find "$line" -type f -name "*" #| wc -l <<<a | echo "$line : $((a-1))"
echo "---Next---"
done < ./Temp.txt
I execute the script as bash script.sh /Users/abc/Documents/ and get the error find: "/Users/abc/Documents": No such file or directory
The Temp.txt file contains:
cat Temp.txt
"/Users/abc/Documents/"
"/Users/abc/Documents/Subfolder1"
"/Users/abc/Documents/Subfolder2"
The problem is, if I just pick up the find command from the while loop and run it on the terminal (I have a mac), it works!!
find "/Users/abc/Documents/" -type f -name "*"
/Users/abc/Documents///1.txt
/Users/abc/Documents///2.txt
/Users/abc/Documents///3.txt
What am I doing wrong? I've read elsewhere on globs and to quote all variables in the script which I am doing. I am also ensuring to store full paths and not expandable ones in the Temp.txt file. The same problem also happens with the ls command in the while loop.
Any help will be greatly appreciated!!