Change part of filenames in a bulk way

Hallo!

I have generated lots of data file which all having this format:

sp*t1overt2*.txt

Now I want to change them in this way:

sp*t2overt1*.txt

The rest of the file names stay unchanged. I know this is kind of routine action in sed or awk, but dont know how! I tried this command:

ls sp* | sed 's/\(t1)su(t2)/mv & \2\1/'

and also this:

ls sp* | sed 's/\(t1).\(t2)/mv & \2\1/'

but none worked and I get this error:

sed: -e expression #1, char 24: Unmatched ( or \(

Can someone tell me how to fix? I really need to change those file names.

Cheers,
Renzo

ls sp* | sed '
  s/\(.*\)t1overt2\(.*\)/mv & \1t2overt1\2/
 ' | sh
1 Like

yet another way...

ls sp*t1overt2*.txt | sed 's/\(.*\)\(t1\)\(.*\)\(t2\)\(.*\)/\1\4\3\2\5/' | xargs mv sp*t1overt2*.txt

No, don't think xargs can apply. Only if t1overt2 is substring of a directory can you move more than one per mv, and then you must make it one per directory.

I copied and pasted incorrectly...but you can apply xargs to a wildcard filename as follows...

ls sp*t1overt2*.txt | sed 'p;s/\(.*\)\(t1\)\(.*\)\(t2\)\(.*\)/\1\4\3\2\5/' | xargs -n 2 mv

Well, it is just doing one file at a time, then, so xargs is not adding any value over that of a shell.

Thank you so much! That was all I wanted :slight_smile:
cheers,
R