Reputation: 4745
I have a lot of files named like activity_unpublish_39x39.png
, abc_29x29.png
and etc.
I want to convert the name to activity_unpublish.png
(remove _39x39
)
and abc.png
(remove _29x29
).
Could anyone tell me how I can achieve that?
It would be better working on Mac OS X.
Upvotes: 2
Views: 607
Reputation: 5631
The following small shell script should work on Linux and also on Mac OS. Note that it's working in the current folder, further you have to change pat
and suf
to your needs (here suf="\.png"
and pat="_[0-9]+x[0-9]+$suf"
to work with your given example).
It uses sed
with -E
which is undocumented in the manpage. It's the option to go in Mac OS which is known as -r
in Linux. In Linux it is also existent but as said not documented:
#!/bin/sh
suf="\.png"
pat="_[0-9]+x[0-9]+$suf"
for f in *; do
if [[ $f =~ $pat ]]; then
newName=$(echo "$f" | sed -E "s/$pat/$suf/g")
mv "$f" "$newName"
fi
done
Upvotes: 3
Reputation: 4745
I got the answer from my kind colleague. Use this shell script.
#!/bin/sh
for file in *_[0-9]*x[0-9]*.png
do
mv $file $(echo $file | sed 's/_[0-9]*x[0-9]*//')
done
Upvotes: 1