help-bash
[Top][All Lists]
Advanced

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

Re: Sequential files


From: Alex fxmbsw7 Ratchev
Subject: Re: Sequential files
Date: Sun, 28 Nov 2021 08:04:59 +0100

does the * in %0*d indicate to use the given data as value ?

On Sun, Nov 28, 2021, 06:01 Daniel Mills <danielmills1@gmail.com> wrote:

> On Sat, Nov 27, 2021 at 8:32 PM MN <promike1987@gmail.com> wrote:
>
> > Alright,
> > Since I got no replies, I guess this is how it is to be done.
> > I changed my mind about counter=0 and I'm using counter=1 now.
> >
> > I was wondering if I could zero pad the files?
> > If there are less than 9 files then I wouldn't change anything, but
> > when I reach example_10.png, I would rename the first 9 files to
> > example_01.png, example 02.png and so forth.
> > And I would do the same thing with hundred and thousand.
> > example_001.png and example_0001.png
> >
> > On 21-11-24 12:08:52, MN wrote:
> > > Hello,
> > > I wrote a script, which works, but I'd like you to review it.
> > > Programs that I use don't offer sequential file savings.
> > > My script waits for them and numbers them in sequence one by one.
> > >
> > > First, I have to save the file.
> > > If the first file is 'example.png', then I run my script like that
> > ./myscript example.png
> > >
> > > From now on I don't have to worry about the name of the files,
> > > I can save them under the very same name.
> > >
> > > Actual script:
> > >
> > > #!/bin/bash
> > > counter=0
> > > while true; do
> > > until [[ -e "$1" ]] ; do
> > > sleep 1
> > > done
> > > mv -i ./"$1" "${1%.*}_$((counter++))".${1##*.}
> > > done
> > >
> > >
> > > How could this be improved?
> >
> >
> You can 0-pad numbers with printf, -v is nice for storing the result in a
> variable. For example,
> pad_width=2
> printf -v outfile "%s_%0*d.%s" "${1%.*}" "$pad_width" "$((counter++))"
> "${1##*.}"
> mv -i ./"$1" "$outfile"
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]