I'm working on looping through files in bash script and for the most part it is working but the directory name is echoed twice. How could I change the code to only echo the files in the directory and not the directory itself.
This is my code:
Directory=C:/temp/
find $Directory
for filename in "$Directory"/;
do
echo $filename
done
This is what I see in my terminal:
C:/temp/
C:/temp/QJ07312433_10_19_2021_snapshot.xml
C:/temp/QJ07312433_10_28_2021_snapshot.xml
C:/temp/
CodePudding user response:
The find
command prints all the names in the hierarchy headed by $Directory
. Then you loop through the single string $Directory/
, and echo that. This is why you get the second echo of the directory name.
If you want the loop to process the find
output in the loop, you need to pipe to it:
find "$Directory" | while read -r name; do
echo "$name"
done