How to loop through files in subdirectories?
Read ex How to loop through a directory recursively to delete files with certain extensions
This is the code I've written:
Check your scripts with http://shellcheck.net
Do not use `. Use $(...)
instead.
To get file name and dir see basename
and dirname
commands. Your name=echo "$i" | cut -d'.' -f1
will fail when directory has a dot.
Use printf "%s\n" "$i"
or in bash <<<"$i"
here string instead echo "$i" |
. See ex. https://unix.stackexchange.com/questions/65803/why-is-printf-better-than-echo .
Prefer lower case variables in your scripts.
so it might be a very simple mistake I'm not seeing.
You are not changing the direcotry at all. You need to cd
somewhere to that dir and then cd -
back (or pushd
+ popd
). But just reference directories relative to your current dir anyway. Maybe something along:
for dir in ./*/; do
# create the directory in dir
output_dir="./$dir/mp3"
mkdir "$output_dir"
# find files aac inside $dir/
for i in "$dir"/*.aac; do
name=$(basename "$i" .acc)
echo "$name"
ffmpeg -i "$i" -vn -ar 44100 -ac 2 -b:a 192k "$output_dir/${name}.mp3"
done
done