I'm writing a script to get all files within a folder(including sub-folder):
#!/bin/bash
function loop() {
files=`ls -1Fd $1`
echo "$files" |
while IFS= read -r file; do
if [[ "$file" == */ ]]; then
loop "$file*"
else
echo "$file"
fi
done
}
loop "$PWD/*"
I tried to test the script in this way:
#create folders and files
mkdir test\ folder && mkdir test\ folder/test\ subfolder && touch test\ folder/test\ subfolder/test\ file && cd test\ folder
#execute the script
~/path_to_the_script/test.sh
But it doesn't work, here is the error:
ls: cannot access /home/user/Documents/test: No such file or directory
ls: cannot access folder/*: No such file or directory
How to modify the script to achieve it?
find . -type f
already