Re: Handle each file at a time in loop?
Foss User <fossist@gmail.com> wrote:
> $ ls
> convert.sh Track 1.wav Track 3.wav Track 5.wav Track 7.wav Track 9.wav
> Track 1.mp3 Track 2.wav Track 4.wav Track 6.wav Track 8.wav
>
> So, you can see there are file names with spaces in them. I have
> written a script like this to handle one file name at a time.
>
> for file in `ls *.wav`
> do
> echo $file
> done
>
> Of course, this doesn't do what I need. The names are split wherever
> there are spaces.
Since you're in shell, it will do the expansion for you;
for file in *.wav
do
echo $file
done
When actually working with the filenames, make sure you encase the
variable name in double quotes so they dont get split up on the commandline;
for file in *.wav
do
newfile=$(echo "$file" | sed -e "s,foo,bar,g")
mv -v "$file" "$newfile"
done
Cheers,
Tyler
Reply to: