Re: Using stupid filenames in shell scripts
On Thu, May 15, 2003 at 02:59:35PM +0200, Robert Ian Smit wrote:
> I have copied some mp3 containing directories from a windows
> partition. In the days I created those files it seemed like a good
> idea to use filenames such as:
>
> 01 - Artist - Song1.mp3
> 02 - Artist - Song2.mp3
> etc.
>
> When I want to do anything useful with these files, I'd like to use a
> script to avoid repeating the same action say 15 times.
>
> For instance, I'd use something like:
>
> for i in `ls`; do echo $i; done
>
> This doesn't work properly. $i gets a lot of values like 01, -,
> Artist, Song.mp3 instead of one value per file.
Try this instead:
for i in *; do echo "$i"; done
The double-quoting is critically important to protect against word
splitting, and using a wildcard rather than `ls` saves you from the fact
that command substitution happens before word splitting (see the
EXPANSION section in bash(1)).
Cheers,
--
Colin Watson [cjwatson@flatline.org.uk]
Reply to: