7

I have a directory containing thousands of files with names t_00xx_000xxx.png and 00xx_000xxx.png. I want to change the names of the files which starts with t_, like t_00xx_000xxx.png to 00xx_000xxx_t.png

So take the prefix and put it as a postfix for some of the files. Can this be done in only one command?

I am running on SUSE SLES12 SP2.

5
  • 2
    OP has no prename and no admin rights to install anything. Commented Aug 1, 2018 at 14:08
  • are all files in one folder? Commented Aug 1, 2018 at 14:12
  • I have something in mind, correct me if I am wrong. If there is a way to loop on all files t_*, I can use mv command, something like mv t_* *_t where * is a single filename within the loop Commented Aug 1, 2018 at 14:12
  • @RoVo yes they are Commented Aug 1, 2018 at 14:12
  • @Mostafa mv doesn't work like that. It's going to take *_t as a directory name then give you an error when it doesn't exist. Commented Aug 2, 2018 at 1:55

4 Answers 4

7

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.

5

In sh syntax:

for f in t_*.*; do
  dest=${f#t_}
  dest=${dest%.*}_t.${dest##*.}
  echo mv -i -- "$f" "$dest"
done
5

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
4

This will work if we assume everything up to the first underscore to be the prefix.

for f in *.png; do
    new=$(echo "$f" | sed -r 's/^([^_]*)_(.*)\.(.*)$/\2_\1.\3/');
    echo "Renaming: $f => $new";
    #mv $f $new
done

Remove the # in front of mv if you're happy with the output.


With prename it would be a little easier:

prename -n 's/^([^_]*)_(.*)\.(.*)$/$2_$1.$3/'

If t_ is always the prefix, change to this pattern:

for f in t_*.png; do
    new=$(echo "$f" | sed -r 's/^t_(.*)\.(.*)$/\1_t.\2/');
    echo "Renaming: $f => $new";
    #mv $f $new
done
10
  • ` Renaming: 0020_000834.png => 000834_0020.png Renaming: 0020_000835.png => 000835_0020.png Renaming: 0020_000836.png => 000836_0020.png Renaming: 00xx_000xxx_t.png => 000xxx_t_00xx.png Renaming: t_0000_000000.png => 0000_000000_t.png Renaming: t_0000_000001.png => 0000_000001_t.png Renaming: t_0000_000002.png => 0000_000002_t.png ` Commented Aug 1, 2018 at 14:16
  • so for the t files it worked, for the others not Commented Aug 1, 2018 at 14:16
  • oh yes it worked well when my assumptions would have been correct... it seems they were wrong. Is always just t_ the prefix ? Commented Aug 1, 2018 at 14:18
  • the folder contains files with t_ and files with not, so I need to perform this operation on only files with t_ ... I would say that I replace this for f in *.png; do with for f in t_*.png; do, right ? or what do you think ? Commented Aug 1, 2018 at 14:20
  • that would work, yes! Commented Aug 1, 2018 at 14:21

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.