[Gllug] error in bash script to rename files in a directory

Paul Cupis paul at cupis.co.uk
Fri Dec 27 23:19:28 UTC 2002


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Friday 27 Dec 2002 22:34, Liam Delahunty wrote:
> Hi Chaps,
>
> I tried the following "script" in a shell...
>
> for i in `ls *.php3`; do mv $i `echo $i|tr 'php3' 'php'`; done
>
> with the intention of renaming all the files in a folder from .php3 to .php
>
> Instead, I now have *.phpp
>
> Could some kind soul explain what should I have done (and what fundamental
> concept I've got wrong...)

Hmm, answering your other question... :)

- From tr(1)

  tr - translate or delete characters

  Options:

       -c, --complement
              first complement SET1

       -d, --delete
              delete characters in SET1, do not translate

       -s, --squeeze-repeats
              replace each input sequence of  a  repeated  character  that  is
              listed in SET1 with a single occurrence of that character

       -t, --truncate-set1
              first truncate SET1 to length of SET2

And running some quick test cases:

paul at kippax:~$ echo 'file.php3' | tr 'php3' 'php'
file.phpp
paul at kippax:~$ echo 'file.php3' | tr 'php3' 'p'
file.pppp
paul at kippax:~$ echo 'file.php3' | tr 'php3' 'php4'
file.php4
paul at kippax:~$ echo 'file.php3' | tr 'php3' 'php42'
file.php4
paul at kippax:~$ echo 'file.php3' | tr -d '3'
file.php
paul at kippax:~$ echo 'file3.php3' | tr -d '3'
file.php
paul at kippax:~$ echo 'file3.php3' | tr -d '3$'
file.php
paul at kippax:~$

So it looks like tr(1) cannot do what you wanted it to do, unless the numeral 
3 does not occur in any of your filenames except as the last character.

My previous two recommended ways of doing this stand.

Paul Cupis
- -- 
paul at cupis.co.uk

 
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.0 (GNU/Linux)

iD8DBQE+DOApIzuKV+SHX/kRAij2AJ9umpne+AgVZlueVPU0CsxV2YRdvACfQ/hT
oU2O4jwbF7EPZgylO9ijnRo=
=n/86
-----END PGP SIGNATURE-----

-- 
Gllug mailing list  -  Gllug at linux.co.uk
http://list.ftech.net/mailman/listinfo/gllug




More information about the GLLUG mailing list