Cut first 16 characters of all filenames in directory?

9 views
Skip to first unread message

Tuxedo

unread,
Jul 7, 2019, 12:11:23 AM7/7/19
to
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

Luuk

unread,
Jul 7, 2019, 4:33:00 AM7/7/19
to
bash supports this:
$ a="abcdefghijklmnopqrstuvwxyz"
$ echo $a
abcdefghijklmnopqrtuvwyz
$ echo ${a:16}
qrtuvwyz

with this you could create a script to copy, or move, your files.

--
Luuk

Helmut Waitzmann

unread,
Jul 7, 2019, 4:43:32 PM7/7/19
to
Tuxedo <tux...@mailinator.net>:
… 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
)

Keith Thompson

unread,
Jul 7, 2019, 4:48:01 PM7/7/19
to
Tuxedo <tux...@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) ks...@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */
Reply all
Reply to author
Forward
0 new messages