Change some file names (prefix to postfix)
Given the filename pattern t_00xx_000xxx.png
where the x's could be any single character, the following loop uses the ?
globbing character substitute for the variable characters. The loop picks up only files that start with t_
and that end in .png
. It uses parameter expansion to strip off the leading t_
, then extracts the desired substring in order to move the _t
into the desired position.
for f in t_00??_000???.png
do
echo mv -- "$f" "${f:2:11}_t.png"
done
For some made-up filenames, the sample output is:
mv t_0011_000111.png 0011_000111_t.png
mv t_0012_000345.png 0012_000345_t.png
mv t_00xx_000xxx.png 00xx_000xxx_t.png
Remove the echo
portion if the results look correct.
In sh
syntax:
for f in t_*.*; do
dest=${f#t_}
dest=${dest%.*}_t.${dest##*.}
echo mv -i -- "$f" "$dest"
done
If SuSE SLES12 has mcp/mmv command in its repository, then it would be simple use of mmv
:
mmv 't_*.png' '#1_t.png'
Or, if the number of characters and specific numbers was issue, you could be more specific like:
mmv 't_00??_000???.png' '00#1#2_000#3#4#5_t.png'
First argument to mmv is source, with standard wildcards *
and ?
. The second argument is destination, in which #1 is replaced with content which first wildcard matched, #2 with content which second wildcard matched etc.
Example:
% ls -1
t_0011_000037.png
t_0011_000038.png
t_0011_000039.png
t_0022_000001.png
t_0022_000002.png
% mmv 't_*.png' '#1_t.png'
% ls -1
0011_000037_t.png
0011_000038_t.png
0011_000039_t.png
0022_000001_t.png
0022_000002_t.png