[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: how to rename multiple files

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"

Your mileage may of course vary, though.

Colin Watson                                  [cjwatson@flatline.org.uk]

Reply to: