I'm confused why you would want to use eval
here. You could just write s=$(ls "$i"*.mkv)
. But calling ls
is pointless and would mangle file names. Just iterate over the files normally.
for dir in /media/mybook/Example*/; do;
if ! [ -d "$dir" ]; then continue; fi
for file in "$dir"/*.mkv; do
if ! [ -e "$file" ]; then continue; fi
echo "$file"
done
done
Note how "$dir"
is within double quotes (so that any special characters such as spaces in the directory name remain as they are), but the *
is outside the quotes so it's treated as a wildcard.
The lines with continue
are there to skip the special case where the wildcard matches nothing. In sh, when a wildcard doesn't match, it's left as is, and so for
sees a list of names with one element which is literally /media/mybook/Example*/
(for the outer loop). Some shells (ksh, bash, zsh) have a way to avoid this, for example in bash:
shopt -s nullglob
for dir in /media/mybook/Example*/; do;
for file in "$dir"/*.mkv; do
echo "$file"
done
done
If you're just processing the files and don't need to do anything for the directories, there's no point in having two nested loops.
for dir in /media/mybook/Example*/*.mkv; do
if ! [ -e "$file" ]; then continue; fi
echo "$file"
done
All these snippets act on files inside the Example*
directories themselves, no in their subdirectories. If you want to traverse the directories recursively, see Kusalananda's answer.