Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #237: Plate voltage too low on demodulator tube


comp / comp.unix.questions / Cut first 16 characters of all filenames in directory?

SubjectAuthor
* Cut first 16 characters of all filenames in directory?Tuxedo
+- Re: Cut first 16 characters of all filenames in directory?Helmut Waitzmann
`- Re: Cut first 16 characters of all filenames in directory?Keith Thompson

1
Subject: Cut first 16 characters of all filenames in directory?
From: Tuxedo
Newsgroups: comp.unix.questions
Organization: solani.org
Date: Sun, 7 Jul 2019 05:09 UTC
Path: eternal-september.org!news.eternal-september.org!reader01.eternal-september.org!reader01.eternal-september.org!feeder.eternal-september.org!weretis.net!feeder6.news.weretis.net!feeder5.news.weretis.net!news.solani.org!.POSTED!not-for-mail
From: tuxedo@mailinator.net (Tuxedo)
Newsgroups: comp.unix.questions
Subject: Cut first 16 characters of all filenames in directory?
Date: Sun, 07 Jul 2019 07:09:01 +0200
Organization: solani.org
Lines: 50
Message-ID: <qfrrd8$4mr$1@solani.org>
Mime-Version: 1.0
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 7Bit
X-Trace: solani.org 1562472682 4827 127.0.0.43 (7 Jul 2019 04:11:22 GMT)
X-Complaints-To: abuse@news.solani.org
NNTP-Posting-Date: Sun, 7 Jul 2019 04:11:22 +0000 (UTC)
User-Agent: KNode/4.14.10
X-User-ID: eJwFwQcBACAIBMBKrEeMg4z+EbyDOnsdc7hhsaQMapUJaeRwd5p0HcS7b2lZ00dD57KhSz4Z7BEi
Cancel-Lock: sha1:V0XkVKB+yOG3BOocB248+Ngq6FY=
View all headers

Hello,

I use a script to rename image files copied from a camera to include the
EXIF generated date into each filename.

The filenames originally appear as follows::

DSC00071.JPG
DSC00075.JPG
DSC00086.JPG
DSC00093.JPG
DSC00116.JPG
DSC00133.JPG

After running the script, they appear as:

20190531_083305_DSC00071.jpg
20190531_083429_DSC00075.jpg
20190531_092253_DSC00086.jpg
20190531_092643_DSC00093.jpg
20190531_093211_DSC00116.jpg
20190531_095200_DSC00133.jpg

Nothing wrong with the procedure, it works great, but sometimes I run it
twice on a batch of files by mistake, ending up with the following:

20190531_083305_20190531_083305_DSC00071.jpg
20190531_083429_20190531_083429_DSC00075.jpg
20190531_092253_20190531_092253_DSC00086.jpg
20190531_092643_20190531_092643_DSC00093.jpg
20190531_093211_20190531_093211_DSC00116.jpg
20190531_095200_20190531_095200_DSC00133.jpg

(... plus thousands more.)

Whenever it happens I erroneously prepend the date string and underscores to
previously processed files, i.e. the 16 characters of each filename as
above.

How can I remove the 16 characters in each filename, by renaming all files
in the current working directory to retain only the last 28 last characters?

But so I don't rename filenames that may still exist in the same directory
which are still correctly named, the procedure should apply only on files
which are exactly 44 characters long as the wrongly named files in this
example are.

Many thanks for any ideas and solutions.

Tuxedo

Subject: Re: Cut first 16 characters of all filenames in directory?
From: Helmut Waitzmann
Newsgroups: comp.unix.questions
Organization: albasani.net
Date: Sun, 7 Jul 2019 20:43 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!reader01.eternal-september.org!reader01.eternal-september.org!feeder.eternal-september.org!news.albasani.net!.POSTED!not-for-mail
From: nn.throttle@xoxy.net (Helmut Waitzmann)
Newsgroups: comp.unix.questions
Subject: Re: Cut first 16 characters of all filenames in directory?
Date: Sun, 07 Jul 2019 22:43:08 +0200
Organization: albasani.net
Lines: 81
Message-ID: <874l3xfudl.fsf@helmutwaitzmann.news.arcor.de>
References: <qfrrd8$4mr$1@solani.org>
Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: quoted-printable
X-Trace: news.albasani.net /PE0NoYp5+1Ze54+E3mEVpb2BW+LlKK4vZi6aS4JlS8gJkGGPNMtO3nMmEfZplJ4rZuN/KGI4Ft222oXjjgstA==
NNTP-Posting-Date: Sun, 7 Jul 2019 20:43:31 +0000 (UTC)
Injection-Info: news.albasani.net; logging-data="7i+cDVQJ5uP6m60yLLCEnti0IY7NJ7SQ7VRDlU+emDhba9ajLm7DeJOifmAEyDj2q01EHXCbOvj1uuidXRNIc1xEJcC6m0QiXGT54qTxMWgStQud8J3+u63ZCn78Cc8R"; mail-complaints-to="abuse@albasani.net"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
Mail-Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Cancel-Lock: sha1:Ssqz5mpD1udFYck+uL4x68lHN3E= sha1:VKxI1rPq5FGAOZcB1kGfH980idw=
Mail-Copies-To: nobody
View all headers

Tuxedo <tuxedo@mailinator.net>:

>I use a script to rename image files copied from a camera to include the
>EXIF generated date into each filename.
>
>The filenames originally appear as follows::
>
>DSC00071.JPG
>DSC00075.JPG
>DSC00086.JPG
>DSC00093.JPG
>DSC00116.JPG
>DSC00133.JPG
>
>After running the script, they appear as:
>
>20190531_083305_DSC00071.jpg
>20190531_083429_DSC00075.jpg
>20190531_092253_DSC00086.jpg
>20190531_092643_DSC00093.jpg
>20190531_093211_DSC00116.jpg
>20190531_095200_DSC00133.jpg
>
>Nothing wrong with the procedure, it works great, but sometimes I run it
>twice on a batch of files by mistake, ending up with the following:
>
>20190531_083305_20190531_083305_DSC00071.jpg
>20190531_083429_20190531_083429_DSC00075.jpg
>20190531_092253_20190531_092253_DSC00086.jpg
>20190531_092643_20190531_092643_DSC00093.jpg
>20190531_093211_20190531_093211_DSC00116.jpg
>20190531_095200_20190531_095200_DSC00133.jpg
>
>(... plus thousands more.)
>
>Whenever it happens I erroneously prepend the date string and underscores to
>previously processed files, i.e. the 16 characters of each filename as
>above.
>

… and if by mistake, the script is run once more, there will be file
names containing a triple of identical date info…

>How can I remove the 16 characters in each filename, by renaming all files
>in the current working directory to retain only the last 28 last characters?
>
>But so I don't rename filenames that may still exist in the same directory
>which are still correctly named, the procedure should apply only on files
>which are exactly 44 characters long as the wrongly named files in this
>example are.
>

I suggest a slightly different solution:

For each filename, check, whether it starts with a sequence of at
least two identical patterns each consisting of 8 digits, one
underscore, 6 digits and one underscore. If that's the case,
strip all but one of these patterns from the start of the
filename.

(
for f in *.jpg
do
if sequence="$(
expr ' '"$f" : \
' \(\([[:digit:]]\{8,\}_[[:digit:]]\{6,\}_\)\2\{1,\}\)'
)"
then
date_info="${sequence%${sequence#????????_??????_}}" &&
renamed_f="${date_info}${f#${sequence}}" &&
mv -i -- "$f" "$renamed_f"
fi
done
)

Subject: Re: Cut first 16 characters of all filenames in directory?
From: Keith Thompson
Newsgroups: comp.unix.questions
Organization: None to speak of
Date: Sun, 7 Jul 2019 20:47 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!reader01.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: kst-u@mib.org (Keith Thompson)
Newsgroups: comp.unix.questions
Subject: Re: Cut first 16 characters of all filenames in directory?
Date: Sun, 07 Jul 2019 13:47:59 -0700
Organization: None to speak of
Lines: 17
Message-ID: <ln1rz1efls.fsf@kst-u.example.com>
References: <qfrrd8$4mr$1@solani.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="85be6611768214b4882e8b438a393482";
logging-data="28590"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19MZJvLerC6T8VX8FFJ2n8u"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux)
Cancel-Lock: sha1:Ris8Q6dH86zXIlvF6zS0D1VUbMA=
sha1:NwxTx7/2m9unH6ekvYCF+tIsrHU=
View all headers

Tuxedo <tuxedo@mailinator.net> writes:
> Hello,
>
> I use a script to rename image files copied from a camera to include the
> EXIF generated date into each filename.
[snip]

This was separately posted (not cross-posted) to comp.unix.shell and is
being discussed there.

If more than one newsgroup is relevant, please post a single article to
both groups so the discussion doesn't fragment.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

1

rocksolid light 0.9.8
clearnet tor