Rename multiple files which have a variable suffix
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-*"
3 answers
The following users marked this post as Works for me:
User | Comment | Date |
---|---|---|
Pr. Sunflowerâ |
Thread: Works for me Yessssss! Thank you! đ
|
Oct 14, 2023 at 12:01 |
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 -*
.
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
$
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 :)
0 comment threads