I am trying to loop through every file in a user specified directory. Here's my code:
clear
echo "enter the directory path: \n"
read directory
for file in $directory; do
echo $file
done
My input, e.g.: /home/user/Downloads
Output I get: /home/user/Downloads
If I use
clear
for file in *; do
echo $file
done
It works, but it shows only the contenets of current directory
If you only want the files non-recursively in the current directory, combine what you have:
read -p 'Enter the directory path: ' directory
for file in "$directory"/*; do
echo "$file"
done
If you want to loop recursively and you have bash 4, it's not much harder:
shopt -s globstar
for file in "$directory"/**/*; do …
But if you only have bash 3, you'd be better off using find
.
find "$directory"
Try
dir="${GOL_HOME}/test_dir"
file="file_*.csv"
for file in `cd ${dir};ls -1 ${file}` ;do
echo $file
done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With