help-bash
[Top][All Lists]
Advanced

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

Re: Sequential files


From: Daniel Mills
Subject: Re: Sequential files
Date: Sun, 28 Nov 2021 00:01:11 -0500

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]