Tugger the SLUGger!SLUG Mailing List Archives

Re: [SLUG] spaces in filenames - bash scripting problem


On Sat, 2006-05-20 at 15:25 +1000, david wrote:

My latest attempt below... and it works all the way down to the mv line,
but then fails because of the spaces. I get this diagnostic:

mv: when moving multiple files, last argument must be a directory


#!/bin/bash
# takes the first 3 chars of each directory name and
# prepends it to the file names within that directory.

ls | while read x
do
        y=$(echo "$x" | cut -c -3)
        ls "$x" | while read z
        do
                mv "$x"/"$z" "$x"/$y"$z"
        done
done




> On Sat, 2006-05-20 at 15:12 +1000, Howard Lowndes wrote:
> > 
> I've changed the scrip thus:
> 
> #!/bin/bash
> cd /home/david/test
> for x in $(ls)
> do
>         y=$(echo "$x" | cut -c -3)
>         for z in $(ls "$x")
>         do
>                 mv "$x"/"$z" "$x"/$y"$z"
>         done
> done
> 
> but this is what i get:
> 
> david@test:~ $ ls -l test
> total 12
> drwxr-xr-x  2 david david 4096 2006-05-20 15:16 1test
> drwxr-xr-x  2 david david 4096 2006-05-20 15:16 2test
> drwxr-xr-x  2 david david 4096 2006-05-20 15:14 3 test
> david@test:~ $ ls test/3\ test/
> 3 test
> david@test:~ $ ./shelltest
> ls: 3: No such file or directory
> ls: test: No such file or directory
> david@test:~ $
> 
> 
> 
> > david wrote:
> > > The scrip works as long as there are no blanks.... unfortunately there
> > > are lots of blanks in both directories and filenames. Can anyone tell me
> > > what i should be doing?
> > > 
> > > #!/bin/bash
> > > # takes the first 3 chars of each directory and
> > > # prepends it to the file names in that directory.
> > > 
> > > #cd /home/david/Desktop/musicbyalbum
> > > cd /home/david/test
> > > for x in $(ls)
> > > do
> > 
> > I think this might work for you
> > 
> >           y=$(echo "$x" | cut -c -3)
> >           for z in $(ls "$x")
> >           do
> >                   mv "$x"/"$z" "$x"/"$y""$z"
> > 
> > 
> > >         done
> > > done
> > > 
> > > 
> > 
> > -- 
> > Howard.
> > LANNet Computing Associates - Your Linux people <http://lannetlinux.com>
> > When you want a computer system that works, just choose Linux;
> > When you want a computer system that works, just, choose Microsoft.
>