Re: how to rename multiple files

2003-01-07 Thread Rogier Wolff
On Sat, Jan 04, 2003 at 09:51:21PM -0500, Fraser Campbell wrote:
 On January 4, 2003 09:20 pm, the fabulous Gerald V. Livingston II wrote:
 
  Thank you. Took a couple of tries to get the syntax correct but I
  ended up with this:
 
  if [ `ls *.jpg 2/dev/null|wc -l` -gt 0 ]
 
  then for i in *.jpg; do mmv $i `date +%s`-$a.jpg; a=a+1; done
 
  fi
 
 If there were thousands of jpgs you'll probably still get a too many 
 arguments error with that for loop.  I usually do something like this:

Nope, The for is internal to the shell. It will not suffer from the
too many arguments problem. Whereas, the ls you suggest is external
and WILL have that problem!
 
 ls *.jpg | while read i; do
 mv $i `date +%s`-$a.jpg; a=a+1
 done

[ I created a directory whith too many files and then: ]

sh-2.05$ ls * | while read i ; do
 echo $i
 done
sh: /bin/ls: Argument list too long
sh-2.05$ 

[ whereas: ]

sh-2.05$ for i in * ; do echo $i; done

alksdjfklasjljkdasjkladsjklasdjlkasjkldfljksljkjklassdkljfljkdsfjlsalkfj999
h-2.05$

(ls IS internal on some shells, but apparently not in mine, or maybe
not when running non-interactively, but then again, I DID do this
interactively... )

Roger. 

-- 
** [EMAIL PROTECTED] ** http://www.BitWizard.nl/ ** +31-15-2600998 **
*-- BitWizard writes Linux device drivers for any device you may have! --*
* The Worlds Ecosystem is a stable system. Stable systems may experience *
* excursions from the stable situation. We are currently in such an  * 
* excursion: The stable situation does not include humans. ***


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: how to rename multiple files

2003-01-05 Thread Colin Watson
On Sat, Jan 04, 2003 at 09:55:42PM -0700, Bob Proulx wrote:
 Colin Watson [EMAIL PROTECTED] [2003-01-05 04:04:19 +]:
   And I've bumped into this. How *DOES* one test for the existence of
   ANY file with a given extension without getting a too many arguments
   error when there are multiple files?
  
  How about:
  
find . -maxdepth 1 -name '*.jpg' -print | grep -q .
 
 Very nice.  But if there _were_ thousands and thousands you spend cpu
 time grep'ing through all of them.  As long as you have one you can
 quit early.  How about this tweak?  Of course you spawn one more
 process.  But it is small and goes away quick.
 
   find . -maxdepth 1 -name '*.jpg' -print | head -n 1 | grep -q .

-q already stops at the first match, so there is no need to do this. To
be honest I'm more concerned about find spending ages looking for more
than one file (which involves time-consuming system calls, I/O, etc.)
than about grep looking at the output (which doesn't) anyway.

 Doing it this way instead of using the exit code I am trying to avoid
 the pipeline exit code problem.  In older shells it was the return
 code of the last program to exit and not necessary the last program in
 the pipeline.  Perhaps I am too paranoid these days.  Does anyone know
 what the standards (SUSv3?) say about this?

SUSv3:

  If the reserved word ! does not precede the pipeline, the exit status
  shall be the exit status of the last command specified in the
  pipeline. Otherwise, the exit status shall be the logical NOT of the
  exit status of the last command. That is, if the last command returns
  zero, the exit status shall be 1; if the last command returns greater
  than zero, the exit status shall be zero.

SUSv2's text differs only in having will instead of shall.

  (The first '.' and the '-print' are redundant with GNU find, but useful
  on other systems.)
 
 But '-maxdepth 1' is a GNU find extension and so this won't work on
 other systems.

D'oh! Thanks.

-- 
Colin Watson  [[EMAIL PROTECTED]]


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: how to rename multiple files

2003-01-05 Thread Mark Zimmerman
On Sun, Jan 05, 2003 at 04:13:49AM +, Colin Watson wrote:
 On Sat, Jan 04, 2003 at 06:58:18PM -0700, Mark Zimmerman wrote:
  shopt -s nullglob
  SOME=FALSE
  MATCH=*.jpg
  for f in $MATCH; do SOME=TRUE; break; done
  
  I tried [ -z $MATCH ] also but it always fails even though echo $MATCH
  prints an empty string.
 
 You probably need to double-quote $MATCH like so.
 

Yes, but that's not all. Setting MATCH=*.jpg does not seem to trigger
the globbing function in bash, to my surprise. The following does
work, though:

shopt -s nullglob
MATCH=$(printf %s *.jpg)
if [ -n $MATCH ]; then SOME=TRUE; else SOME=FALSE; fi

By the way, I noted many solutions in other threads that should also
work fine. The reason I wanted to figure this one out is because it
relies only on shell internals. In the event that huge lists of files
are involved it may be faster.

-- Mark


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: how to rename multiple files

2003-01-05 Thread Bill Moseley
On Sun, 5 Jan 2003, Mark Zimmerman wrote:

 Yes, but that's not all. Setting MATCH=*.jpg does not seem to trigger
 the globbing function in bash, to my surprise. The following does
 work, though:
 
 shopt -s nullglob
 MATCH=$(printf %s *.jpg)
 if [ -n $MATCH ]; then SOME=TRUE; else SOME=FALSE; fi

Ok, I realize this is about how to rename in a shell script, but...

Sure seems like this is one of those problems that Perl would make simple.

I'd probably call perl from procmail, split out the mime parts, create a
unique file name and perhaps update a database index and then set a flag
so a cron will see an update and generate a new web page using
Tempate-Toolkit...

-- 
Bill Moseley [EMAIL PROTECTED]


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: how to rename multiple files

2003-01-05 Thread Bob Proulx
Colin Watson [EMAIL PROTECTED] [2003-01-05 13:46:32 +]:
find . -maxdepth 1 -name '*.jpg' -print | head -n 1 | grep -q .
 
 -q already stops at the first match, so there is no need to do this.

[I had to check.  ;-) The option -q sets done_on_match and the code
quits at the first chance it can.
  grep.c: line 497:
static int done_on_match; /* Stop scanning file on first match */
]

Sweet!  I did not know that.  Thanks for educating me.

   shall be the exit status of the last command specified in the pipeline

[Therefore I no longer need to worry about the last command that exits
versus the last command in the pipeline.]

I have some scripts that can be cleaned up and simplified.

Thanks again!
Bob



msg22544/pgp0.pgp
Description: PGP signature


Re: how to rename multiple files

2003-01-04 Thread Gerald V. Livingston II
Colin Watson said:

 On Tue, Dec 10, 2002 at 11:20:31AM -0800, Osamu Aoki wrote:
 if [ -e *.JPG ]; then for i in *.JPG; do mv $i ${i%.JPG}.jpg;
 done fi

 That -e test looks dreadful ... surely it'll usually expand to lots of
 arguments which will confuse [, or perhaps to an empty string
 (nullglob)
 which will also confuse test?

 --
 Colin Watson
 [[EMAIL PROTECTED]]

And I've bumped into this. How *DOES* one test for the existence of
ANY file with a given extension without getting a too many arguments
error when there are multiple files?

I want TRUE if there is one or more zzz.jpg files in a directory,
FALSE if there are zero of them.

G


-- 
Gerald
http://www.phorce1.com
http://www.buskatiers.org


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: how to rename multiple files

2003-01-04 Thread Jamin W. Collins
On Sat, Jan 04, 2003 at 07:00:03PM -0600, Gerald V. Livingston II wrote:

 And I've bumped into this. How *DOES* one test for the existence of
 ANY file with a given extension without getting a too many arguments
 error when there are multiple files?
 
 I want TRUE if there is one or more zzz.jpg files in a directory,
 FALSE if there are zero of them.

Assuming you don't want the names of the files, just whether they are
there or not:

   ls *.ext 2 /dev/null | wc -l

If there are no files you get a 0, if there are you get the number of
them.  Thus a non-zero result means TRUE and a zero result means FALSE.

-- 
Jamin W. Collins


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: how to rename multiple files

2003-01-04 Thread Mark Zimmerman
On Sat, Jan 04, 2003 at 07:00:03PM -0600, Gerald V. Livingston II wrote:
 Colin Watson said:
 
  On Tue, Dec 10, 2002 at 11:20:31AM -0800, Osamu Aoki wrote:
  if [ -e *.JPG ]; then for i in *.JPG; do mv $i ${i%.JPG}.jpg;
  done fi
 
  That -e test looks dreadful ... surely it'll usually expand to lots of
  arguments which will confuse [, or perhaps to an empty string
  (nullglob)
  which will also confuse test?
 
  --
  Colin Watson
  [[EMAIL PROTECTED]]
 
 And I've bumped into this. How *DOES* one test for the existence of
 ANY file with a given extension without getting a too many arguments
 error when there are multiple files?
 
 I want TRUE if there is one or more zzz.jpg files in a directory,
 FALSE if there are zero of them.
 

This is icky but works:

shopt -s nullglob
SOME=FALSE
MATCH=*.jpg
for f in $MATCH; do SOME=TRUE; break; done

I tried [ -z $MATCH ] also but it always fails even though echo $MATCH
prints an empty string.

-- Mark


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: how to rename multiple files

2003-01-04 Thread Michael Naumann
05.01.2003 02:00:03, Gerald V. Livingston II [EMAIL PROTECTED] wrote:

I want TRUE if there is one or more zzz.jpg files in a directory,
FALSE if there are zero of them.

One solution might be:
ls | grep -q '\.jpg$'

Cheers, Michael



-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: how to rename multiple files

2003-01-04 Thread Gerald V. Livingston II
Jamin W. Collins said:

 On Sat, Jan 04, 2003 at 07:00:03PM -0600, Gerald V. Livingston II
 wrote:

 I want TRUE if there is one or more zzz.jpg files in a directory,
 FALSE if there are zero of them.

 Assuming you don't want the names of the files, just whether they are
 there or not:

ls *.ext 2 /dev/null | wc -l

 If there are no files you get a 0, if there are you get the number of
 them.  Thus a non-zero result means TRUE and a zero result means
 FALSE.

 --
 Jamin W. Collins

Thank you. Took a couple of tries to get the syntax correct but I
ended up with this:

if [ `ls *.jpg 2/dev/null|wc -l` -gt 0 ]

then for i in *.jpg; do mmv $i `date +%s`-$a.jpg; a=a+1; done

fi

Works beautifully. (I ripped out the directory names to prevent
wrapping as that stuff is all buried deep in /home/username/.)

G

-- 
Gerald
http://www.phorce1.com
http://www.buskatiers.org


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: how to rename multiple files

2003-01-04 Thread Raja R Harinath
Hi,

Gerald V. Livingston II [EMAIL PROTECTED] writes:

 Colin Watson said:

 On Tue, Dec 10, 2002 at 11:20:31AM -0800, Osamu Aoki wrote:
 if [ -e *.JPG ]; then for i in *.JPG; do mv $i ${i%.JPG}.jpg;
 done fi

 That -e test looks dreadful ... surely it'll usually expand to lots of
 arguments which will confuse [, or perhaps to an empty string
 (nullglob)
 which will also confuse test?

 --
 Colin Watson
 [[EMAIL PROTECTED]]

 And I've bumped into this. How *DOES* one test for the existence of
 ANY file with a given extension without getting a too many arguments
 error when there are multiple files?

 I want TRUE if there is one or more zzz.jpg files in a directory,
 FALSE if there are zero of them.

Try

  list=`echo *.jpg`
  case $list in
  '*.jpg') echo nothing to be done;;
  *) for i in $list; do mv $i `basename $i .jpg`.jpeg; done
  esac 

- Hari
-- 
Raja R Harinath -- [EMAIL PROTECTED]


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: how to rename multiple files

2003-01-04 Thread Colin Watson
On Sat, Jan 04, 2003 at 07:00:03PM -0600, Gerald V. Livingston II wrote:
 Colin Watson said:
  On Tue, Dec 10, 2002 at 11:20:31AM -0800, Osamu Aoki wrote:
  if [ -e *.JPG ]; then for i in *.JPG; do mv $i ${i%.JPG}.jpg;
  done fi
 
  That -e test looks dreadful ... surely it'll usually expand to lots of
  arguments which will confuse [, or perhaps to an empty string
  (nullglob)
  which will also confuse test?
 
 And I've bumped into this. How *DOES* one test for the existence of
 ANY file with a given extension without getting a too many arguments
 error when there are multiple files?

How about:

  find . -maxdepth 1 -name '*.jpg' -print | grep -q .

? This keeps the shell's wildcard expansion comfortably out of the
equation.

(The first '.' and the '-print' are redundant with GNU find, but useful
on other systems.)

-- 
Colin Watson  [[EMAIL PROTECTED]]


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: how to rename multiple files

2003-01-04 Thread Colin Watson
On Sat, Jan 04, 2003 at 06:58:18PM -0700, Mark Zimmerman wrote:
 shopt -s nullglob
 SOME=FALSE
 MATCH=*.jpg
 for f in $MATCH; do SOME=TRUE; break; done
 
 I tried [ -z $MATCH ] also but it always fails even though echo $MATCH
 prints an empty string.

You probably need to double-quote $MATCH like so.

-- 
Colin Watson  [[EMAIL PROTECTED]]


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: how to rename multiple files

2003-01-04 Thread Colin Watson
On Sat, Jan 04, 2003 at 08:20:18PM -0600, Gerald V. Livingston II wrote:
 Jamin W. Collins said:
  On Sat, Jan 04, 2003 at 07:00:03PM -0600, Gerald V. Livingston II
  wrote:
  I want TRUE if there is one or more zzz.jpg files in a directory,
  FALSE if there are zero of them.
 
  Assuming you don't want the names of the files, just whether they are
  there or not:
 
 ls *.ext 2 /dev/null | wc -l
 
  If there are no files you get a 0, if there are you get the number of
  them.  Thus a non-zero result means TRUE and a zero result means
  FALSE.
 
 Thank you. Took a couple of tries to get the syntax correct but I
 ended up with this:
 
 if [ `ls *.jpg 2/dev/null|wc -l` -gt 0 ]
 
 then for i in *.jpg; do mmv $i `date +%s`-$a.jpg; a=a+1; done
 
 fi

In general it's better to avoid putting backticks in the middle of ['s
arguments; it's just too fragile. Your arithmetic expansion looks a bit
dodgy too. Extending my earlier mail, I'd use:

  export a=0
  find . -maxdepth 1 -name '*.jpg' -print | while read i; do
mmv $i `date +%s`-$a.jpg
a=$(($a+1))
  done

Your mileage may of course vary, though.

-- 
Colin Watson  [[EMAIL PROTECTED]]


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: how to rename multiple files

2003-01-04 Thread Bob Proulx
Fraser Campbell [EMAIL PROTECTED] [2003-01-04 21:51:21 -0500]:
 On January 4, 2003 09:20 pm, the fabulous Gerald V. Livingston II wrote:
 
  Thank you. Took a couple of tries to get the syntax correct but I
  ended up with this:
 
  if [ `ls *.jpg 2/dev/null|wc -l` -gt 0 ]
  then for i in *.jpg; do mmv $i `date +%s`-$a.jpg; a=a+1; done
  fi

What does 'mmv' buy you over using the more standard 'mv' in this
example?  Just curious.  You were not using any of the mmv features
that I could tell here.

 If there were thousands of jpgs you'll probably still get a too many 
 arguments error with that for loop.  I usually do something like this:
 
 ls *.jpg | while read i; do
 mv $i `date +%s`-$a.jpg; a=a+1
 done
 
 If there are no files, no problem, if there are 10,000 files also no problem 
 (although there might be a faster way?).

That works.  I prefer 'xargs' myself.  Consider this alternative, not
replacement, example.  Also you do have the shell to help you out with
your unique name generation and I don't.  (Of course I only have the
'echo' there for testing because I am paranoid that someone would cut
and paste this literally.)  The {} is replaced with each name read
from standard input in turn.

  find . -name '*.jpg' -print0 | xargs -r0 -i echo mv {} /path/to/there/

However, if this is a command that is run frequently as I seem to
recall the OP saying earlier in this thread then I would assume that
it would not be possible to get up to ARG_MAX and would do the simpler
for loop above.

Bob



msg22452/pgp0.pgp
Description: PGP signature


Re: how to rename multiple files

2003-01-04 Thread Gerald V. Livingston II

Colin Watson said:

 Took a couple of tries to get the syntax correct but I
 ended up with this:

 if [ `ls *.jpg 2/dev/null|wc -l` -gt 0 ]

 then for i in *.jpg; do mmv $i `date +%s`-$a.jpg; a=a+1; done

 fi

 In general it's better to avoid putting backticks in the middle of ['s
 arguments; it's just too fragile. Your arithmetic expansion looks a
 bit
 dodgy too. Extending my earlier mail, I'd use:

   export a=0
   find . -maxdepth 1 -name '*.jpg' -print | while read i; do
 mmv $i `date +%s`-$a.jpg
 a=$(($a+1))
   done

 --
 Colin Watson

I cut out a bit too much of the script in my example (the dodgy math
is missing parts above it). I'm working on completing the bit I have
and will re-thread this with a new message (and an explanation) in a
bit.

G

-- 
Gerald
http://www.phorce1.com
http://www.buskatiers.org


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: how to rename multiple files

2003-01-04 Thread Bob Proulx
Colin Watson [EMAIL PROTECTED] [2003-01-05 04:04:19 +]:
  And I've bumped into this. How *DOES* one test for the existence of
  ANY file with a given extension without getting a too many arguments
  error when there are multiple files?
 
 How about:
 
   find . -maxdepth 1 -name '*.jpg' -print | grep -q .

Very nice.  But if there _were_ thousands and thousands you spend cpu
time grep'ing through all of them.  As long as you have one you can
quit early.  How about this tweak?  Of course you spawn one more
process.  But it is small and goes away quick.

  find . -maxdepth 1 -name '*.jpg' -print | head -n 1 | grep -q .

Because I have now guarded the amount of data this produces I would
probably use this following construct to test the existence of output.
I would never do this without making sure the output is bounded to
something reasonably small.  In this case the maximum length of one
filename.

  [ -n $(find . -maxdepth 1 -name '*.jpg' -print | head -n 1) ]

Doing it this way instead of using the exit code I am trying to avoid
the pipeline exit code problem.  In older shells it was the return
code of the last program to exit and not necessary the last program in
the pipeline.  Perhaps I am too paranoid these days.  Does anyone know
what the standards (SUSv3?) say about this?

 (The first '.' and the '-print' are redundant with GNU find, but useful
 on other systems.)

But '-maxdepth 1' is a GNU find extension and so this won't work on
other systems.

Bob



msg22461/pgp0.pgp
Description: PGP signature


Re: how to rename multiple files

2002-12-10 Thread Jason Majors
On Mon, Dec 09, 2002 at 11:33:03PM -0800, Osamu Aoki scribbled...
 On Mon, Dec 09, 2002 at 10:02:25PM -0600, Shyamal Prasad wrote:
  drew == drew cohan [EMAIL PROTECTED] writes:
  
  drew How do I rename all files in a directory matching the
  drew pattern *.JPG to *.jpg in a bash shell script?  Thanks to
  drew you guys I can check for the existence of jpgs in a
  drew directory, but can't seem get 'mv' to rename them for me
  drew (always complains that the last argument must be a
  drew directory).
  
  Still another way:
  
  for i in *.JPG
  do
mv $i `basename $i .JPG`.jpg
  done
 
 Simplest alternative without sub-shell nor special command:
 
 for i in *.JPG
 do
   mv $i ${i%\.JPG}.jpg
 done
 
My way:

perl.mmv
#!/usr/bin/perl -w

#  mmv
# Renames files based on regex patterns.

use Getopt::Std;
getopts(tv);

die bad usage\n unless (@ARGV  2);
my ($p1, $p2) = @ARGV;

$opt_t = 1 if defined $opt_t;
$opt_v = 1 if defined $opt_v;

my $newFile;
foreach my $file (@ARGV)
{
  if ($file =~ m#$p1#)
  {
$newFile = $file;
$newFile =~ s/$p1/$p2/;

print Move '$file' to '$newFile'\n if ($opt_t or $opt_v);
rename $file, $newFile unless ($opt_t);
  }
}


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: how to rename multiple files

2002-12-10 Thread Colin Watson
On Mon, Dec 09, 2002 at 11:33:03PM -0800, Osamu Aoki wrote:
 Simplest alternative without sub-shell nor special command:
 
 for i in *.JPG
 do
   mv $i ${i%\.JPG}.jpg
 done
 
 It works with any reasonable shell ash/bash/dash/... and uses only mv
 command. 

I like the various clever shell expansions. You don't need the backslash
though (so just ${i%.JPG}) - the pattern is just a shell wildcard, not a
regular expression.

-- 
Colin Watson  [[EMAIL PROTECTED]]


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: how to rename multiple files

2002-12-10 Thread Rick Pasotto
On Mon, Dec 09, 2002 at 11:33:03PM -0800, Osamu Aoki wrote:
 On Mon, Dec 09, 2002 at 10:02:25PM -0600, Shyamal Prasad wrote:
  drew == drew cohan [EMAIL PROTECTED] writes:
  
  drew How do I rename all files in a directory matching the
  drew pattern *.JPG to *.jpg in a bash shell script?  Thanks to
  drew you guys I can check for the existence of jpgs in a
  drew directory, but can't seem get 'mv' to rename them for me
  drew (always complains that the last argument must be a
  drew directory).
  
  Still another way:
  
  for i in *.JPG
  do
mv $i `basename $i .JPG`.jpg
  done
 
 Simplest alternative without sub-shell nor special command:
 
 for i in *.JPG
 do
   mv $i ${i%\.JPG}.jpg
 done
 

I've been offline so I haven't seen all the responses but you'd better
put double quotes around the $i or it will fail if the file name has one
or more spaces in it.

-- 
The alternative is not plan or no plan. The question is: whose
planning? Should each member of society plan for himself or should the
paternal government alone plan for all?
Rick Pasotto[EMAIL PROTECTED]http://www.niof.net


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: how to rename multiple files

2002-12-10 Thread Osamu Aoki
On Tue, Dec 10, 2002 at 10:11:20AM -0500, Rick Pasotto wrote:
 On Mon, Dec 09, 2002 at 11:33:03PM -0800, Osamu Aoki wrote:
  On Mon, Dec 09, 2002 at 10:02:25PM -0600, Shyamal Prasad wrote:
   drew == drew cohan [EMAIL PROTECTED] writes:
   
   drew How do I rename all files in a directory matching the
   drew pattern *.JPG to *.jpg in a bash shell script?  Thanks to
   drew you guys I can check for the existence of jpgs in a
   drew directory, but can't seem get 'mv' to rename them for me
   drew (always complains that the last argument must be a
   drew directory).
   
   Still another way:
   
   for i in *.JPG
   do
 mv $i `basename $i .JPG`.jpg
   done
  
  Simplest alternative without sub-shell nor special command:
  
  for i in *.JPG
  do
mv $i ${i%\.JPG}.jpg
  done
  
 
 I've been offline so I haven't seen all the responses but you'd better
 put double quotes around the $i or it will fail if the file name has one
 or more spaces in it.

Very good point (considering these upper case filenames may be in Samba
share drive where people tends to have such insane file names)

Nicest 1 liner shell script is:

if [ -e *.JPG ]; then for i in *.JPG; do mv $i ${i%.JPG}.jpg; done fi

This catches the case where no file with the name *.JPG

Am I correct? I hope so :)
-- 
~\^o^/~~~ ~\^.^/~~~ ~\^*^/~~~ ~\^_^/~~~ ~\^+^/~~~ ~\^:^/~~~ ~\^v^/~~~ +
Osamu Aoki [EMAIL PROTECTED]   Cupertino CA USA, GPG-key: A8061F32
 .''`.  Debian Reference: post-installation user's guide for non-developers
 : :' : http://qref.sf.net and http://people.debian.org/~osamu
 `. `'  Our Priorities are Our Users and Free Software --- Social Contract


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: how to rename multiple files

2002-12-10 Thread Colin Watson
On Tue, Dec 10, 2002 at 11:20:31AM -0800, Osamu Aoki wrote:
 if [ -e *.JPG ]; then for i in *.JPG; do mv $i ${i%.JPG}.jpg; done fi

That -e test looks dreadful ... surely it'll usually expand to lots of
arguments which will confuse [, or perhaps to an empty string (nullglob)
which will also confuse test?

-- 
Colin Watson  [[EMAIL PROTECTED]]


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




how to rename multiple files

2002-12-09 Thread drew cohan
How do I rename all files in a directory matching the pattern *.JPG to
*.jpg in a bash shell script?  Thanks to you guys I can check for the
existence of jpgs in a directory, but can't seem get 'mv' to rename them
for me (always complains that the last argument must be a directory).

 

TIA,

 

Drew Cohan

[EMAIL PROTECTED]

 




-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: how to rename multiple files

2002-12-09 Thread Craig Dickson
drew cohan wrote:

 How do I rename all files in a directory matching the pattern *.JPG to
 *.jpg in a bash shell script?  Thanks to you guys I can check for the
 existence of jpgs in a directory, but can't seem get 'mv' to rename them
 for me (always complains that the last argument must be a directory).

That's because the shell expands filenames. If you have the following files
in the current directory:

   1.JPG
   2.JPG
   3.JPG

then the command

   mv *.JPG *.jpg

turns into

   mv 1.JPG 2.JPG 3.JPG *.jpg

and so mv complains that if you're moving multiple files, the target
must be a directory (which makes sense). (The *.jpg is not expanded
because there are, I assume, no files ending with .jpg in the
directory.)

What you really want is something like this:

   for f in *.JPG; do mv $f `basename $f .JPG`.jpg; done

which means for each .JPG file, move it to the same name, but with the
.JPG ending stripped off, and .jpg appended. Thus 1.JPG becomes 1.jpg,
etc.

As long as your .JPG filenames don't have spaces or other shell-unfriendly
characters in them, this should do the job.

Craig


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: how to rename multiple files

2002-12-09 Thread Colin Watson
On Mon, Dec 09, 2002 at 03:03:10PM -0500, drew cohan wrote:
 How do I rename all files in a directory matching the pattern *.JPG to
 *.jpg in a bash shell script?  Thanks to you guys I can check for the
 existence of jpgs in a directory, but can't seem get 'mv' to rename them
 for me (always complains that the last argument must be a directory).

Either what Craig said, or one of:

  # apt-get install mmv
  $ mmv \*.JPG \#1.jpg

  $ rename 's/\.JPG$/.jpg/' *.JPG

-- 
Colin Watson  [[EMAIL PROTECTED]]


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: how to rename multiple files

2002-12-09 Thread Oliver Fuchs
On Mon, 09 Dec 2002, drew cohan wrote:

 How do I rename all files in a directory matching the pattern
 *.JPG to *.jpg in a bash shell script?  Thanks to you guys I can
 check for the existence of jpgs in a directory, but can't seem get
 'mv' to rename them for me (always complains that the last
 argument must be a directory).
 

Hi, see man rename:

[...]

DESCRIPTION
rename renames the filenames supplied
according to the rule specified as the first argument.  The
perlexpr argument is a Perl expression which is expected to
modify the $_ string in Perl for at least some of the
filenames specified.  If a given filename is not modified by
the expression, it will not be renamed.  If no filenames are
given on the command line, filenames will be read via
standard input.

For example, to rename all files matching *.bak to strip
the extension, you might say

   rename 's/\e.bak$//' *.bak

   To translate uppercase names to lower, you'd use

   rename 'y/A-Z/a-z/' *

[...]

Oliver 
-- 
... don't touch the bang bang fruit


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: how to rename multiple files

2002-12-09 Thread Andrei Smirnov
On Mon, Dec 09, 2002 at 03:03:10PM -0500, drew cohan wrote:
 How do I rename all files in a directory matching the pattern *.JPG to
 *.jpg in a bash shell script?  Thanks to you guys I can check for the
 existence of jpgs in a directory, but can't seem get 'mv' to rename them
 for me (always complains that the last argument must be a directory).
 
another way:
find dir -name '*.JPG' | xargs -iAAA basename AAA .JPG | xargs -iAAA mv \
AAA.JPG AAA.smth


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




RE: how to rename multiple files

2002-12-09 Thread drew cohan
On Mon, Dec 09, 2002 at 03:03:10PM -0500, drew cohan wrote:
 How do I rename all files in a directory matching the pattern *.JPG to
 *.jpg in a bash shell script?  Thanks to you guys I can check for the
 existence of jpgs in a directory, but can't seem get 'mv' to rename
them
 for me (always complains that the last argument must be a directory).

Thanks, once again, you guys are great.  One quick question about this
one:

  $ rename 's/\.JPG$/.jpg/' *.JPG

Shouldn't I literalize the second period like

  $ rename 's/\.JPG$/\.jpg/' *.JPG

or doesn't that make a difference?

-- Drew



-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: how to rename multiple files

2002-12-09 Thread martin f krafft
also sprach drew cohan [EMAIL PROTECTED] [2002.12.09.2229 +0100]:
 Thanks, once again, you guys are great.  One quick question about this
 one:
 
   $ rename 's/\.JPG$/.jpg/' *.JPG
 
 Shouldn't I literalize the second period like
 
   $ rename 's/\.JPG$/\.jpg/' *.JPG
 
 or doesn't that make a difference?

It makes no difference.

-- 
Please do not CC me! Get a proper mailer instead: www.mutt.org
 
 .''`. martin f. krafft [EMAIL PROTECTED]
: :'  :proud Debian developer, admin, and user
`. `'`
  `-  Debian - when you have better things to do than fixing a system
 
NOTE: The public PGP keyservers are broken!
Get my key here: http://people.debian.org/~madduck/gpg/330c4a75.asc



msg17970/pgp0.pgp
Description: PGP signature


Re: how to rename multiple files

2002-12-09 Thread Craig Dickson
drew cohan wrote:

 Thanks, once again, you guys are great.  One quick question about this
 one:
 
   $ rename 's/\.JPG$/.jpg/' *.JPG
 
 Shouldn't I literalize the second period like
 
   $ rename 's/\.JPG$/\.jpg/' *.JPG
 
 or doesn't that make a difference?

The replacement text is not a regular expression, so it isn't necessary.
It should be harmless, though, I think.

Craig


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: how to rename multiple files

2002-12-09 Thread Colin Watson
On Mon, Dec 09, 2002 at 04:29:37PM -0500, drew cohan wrote:
 On Mon, Dec 09, 2002 at 03:03:10PM -0500, drew cohan wrote:
  How do I rename all files in a directory matching the pattern *.JPG to
  *.jpg in a bash shell script?  Thanks to you guys I can check for the
  existence of jpgs in a directory, but can't seem get 'mv' to rename
  them for me (always complains that the last argument must be a
  directory).
 
 Thanks, once again, you guys are great.  One quick question about this
 one:
 
   $ rename 's/\.JPG$/.jpg/' *.JPG
 
 Shouldn't I literalize the second period like
 
   $ rename 's/\.JPG$/\.jpg/' *.JPG
 
 or doesn't that make a difference?

You only need to escape metacharacters on the left-hand side of s///
expressions, so no, it makes no difference.

-- 
Colin Watson  [[EMAIL PROTECTED]]


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]




Re: how to rename multiple files

2002-12-09 Thread Shyamal Prasad
drew == drew cohan [EMAIL PROTECTED] writes:

drew How do I rename all files in a directory matching the
drew pattern *.JPG to *.jpg in a bash shell script?  Thanks to
drew you guys I can check for the existence of jpgs in a
drew directory, but can't seem get 'mv' to rename them for me
drew (always complains that the last argument must be a
drew directory).

Still another way:

for i in *.JPG
do
  mv $i `basename $i .JPG`.jpg
done

Cheers!
Shyamal


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED] 
with a subject of unsubscribe. Trouble? Contact [EMAIL PROTECTED]