Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Rename multiple files which have a variable suffix

+1
−0

I compressed some JPEGs with curtail it messed up the filenames. It was supposed to only add -min at the end but ended up adding a random string after the extension 😠:

prs@PC:/DOWNLOADS/Pictures$ find . -type f
./IMG_20230917_093726_2-min.jpg-U2XlUA   <<< To rename "[...]_2-min.jpg"
./IMG_20230917_093726_2.jpg              - Don't do anything
./IMG_20230917_093738_3-min.jpg-H39QsD   <<< To rename "[...]_3-min.jpg"
./IMG_20230917_093738_3.jpg              - Don't do anything
./IMG_20230917_094057_1-min.jpg-AbxJMt   <<< To rename "[...]_1-min.jpg"
./IMG_20230917_094057_1.jpg              - Don't do anything

How can I remove the random string for each -min file?

I already have a find command that gets the -min files while excluding the pictures I don't want to touch:

find . -name "*-min.jpg-*"
History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

3 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+1
−0
find . -type f -print0 \
| grep -z -- '-min.jpg-[[:alnum:]]*$' \
| while IFS= read -r -d '' f; do
    find "$f" -print0 \
    | sed -z 's/-min.jpg-[[:alnum:]]*$/-min.jpg/' \
    | xargs -0 mv "$f";
done;

Or, if you prefer a one-liner:

find . -type f -print0 | grep -z -- '-min.jpg-[[:alnum:]]*$' | while IFS= read -r -d '' f; do find "$f" -print0 | sed -z 's/-min.jpg-[[:alnum:]]*$/-min.jpg/' | xargs -0 mv "$f"; done;

The code above is hardened against malicious, broken, or otherwise non-portable file names. That makes it slightly more complex than necessary.

Never use whitespace in file names. They are non-portable. See POSIX: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_282.

For the hardened version, find(1)'s -print0 and xargs(1)'s -0 are common enough that don't need explanation (do an online search). grep(1)'s and sed(1)'s -z and read(1)'s -r are less common, but still common enough. read(1)'s -d '' is a trick explained here: https://stackoverflow.com/questions/9612090/how-to-loop-through-file-names-returned-by-find#comment98776168_9612232.

Here's a simpler version, which you can run if you know your file names are portable:

find . -type f \
| grep -- '-min.jpg-[[:alnum:]]*$' \
| while read f; do
    echo $f \
    | sed 's/-min.jpg-[[:alnum:]]*$/-min.jpg/' \
    | xargs mv $f;
done;

find . -type f |:

Find files

| grep -- '-min.jpg-[[:alnum:]]*$' |:

Filter those that have interesting file names. -- is necessary to avoid interpreting the pattern as an option to grep(1).

| while read f; do:

For each path name (each line), store the path name in $f, and run the nested commands on it.

echo $f |:

Echo the full path name.

| sed 's/-min.jpg-[[:alnum:]]*$/-min.jpg/' |:

Remove the part of the file name that we don't like.

| xargs mv $f;:

Move the file $f (this was the old path name), to the path resulting of the previous filter.

done;:

Have a nice sleep :)

History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

Why not `find . -type f -name '*-min.jpg-*' -print0` to avoid the `grep`? And there is no reason to a... (7 comments)
+3
−0

You can use rename for this. Many distributions package it; for example, it's apt-get install rename in Debian.

In normal usage, it is used to apply an arbitrary regular expression substitution expression to the name of each file named on the command line, allowing you to do complex renamings relatively easily. (It even supports using backreferences with $1 through $9 syntax for when you want to move parts of the file name around.) It also supports a verbose mode (-v) which I use below, or a dry-run mode (-n) for when you want to see what it will do.

Example:

$ cd $(mktemp -d)
$ touch IMG_20230917_093726_2-min.jpg-U2XlUA IMG_20230917_093726_2.jpg IMG_20230917_093738_3-min.jpg-H39QsD IMG_20230917_093738_3.jpg IMG_20230917_094057_1-min.jpg-AbxJMt IMG_20230917_094057_1.jpg
$ ls -1
IMG_20230917_093726_2.jpg
IMG_20230917_093726_2-min.jpg-U2XlUA
IMG_20230917_093738_3.jpg
IMG_20230917_093738_3-min.jpg-H39QsD
IMG_20230917_094057_1.jpg
IMG_20230917_094057_1-min.jpg-AbxJMt
$ rename -v 's,-......$,,g' *-min.jpg-*
IMG_20230917_093726_2-min.jpg-U2XlUA renamed as IMG_20230917_093726_2-min.jpg
IMG_20230917_093738_3-min.jpg-H39QsD renamed as IMG_20230917_093738_3-min.jpg
IMG_20230917_094057_1-min.jpg-AbxJMt renamed as IMG_20230917_094057_1-min.jpg
$ ls -1
IMG_20230917_093726_2.jpg
IMG_20230917_093726_2-min.jpg
IMG_20230917_093738_3.jpg
IMG_20230917_093738_3-min.jpg
IMG_20230917_094057_1.jpg
IMG_20230917_094057_1-min.jpg
$
History
Why does this post require moderator attention?
You might want to add some details to your flag.

2 comment threads

Be aware that there are 2 rename commands with some divergences in naming conventions. Fedora/Red Hat... (1 comment)
Thank you, @Canina 🙂 Yes, I am running Debian trixie "testing" but before posting this question I ... (2 comments)
+3
−0

An easy one-liner in POSIX-compatible shells:

for f in *-min.jpg-*; do mv -- "$f" "${f%-*}"; done

The only (mildly) tricky concept here is ${f%-*}, which expands to $f minus the shortest suffix matching the glob pattern -*.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

Works for me (1 comment)

Sign up to answer this question »