0

I have multiple files that have the following format: (note that file corresponds to each file name which is not common).

File1_S20.tab
File2_S25.tab
File3_S40.tab
etc.

I want to rename them all so they become:

File1
File2
File3
etc.

Basically removing the _S$$.tab part from all the files.

For renaming files, I usually use the rename command as follows: rename # somethingelse *.tab (replace # by somethingelse).

But the only trouble I have is that each file has a different number after the S.

tenten
  • 13

1 Answers1

3

That sounds like you have the util-linux version of rename, which doesn't bend to that easily. There's also a Perl-based tool called rename. (see: What's the difference between 'rename' and 'mv'?)

With the Perl rename, if you can get it, that's easy (or add -n to see what it would do):

rename 's/_S\d+\.tab//' File*.tab

(It's relatively simple to implement the rename loop in Perl, but doing it right and safe takes a few lines.)

But you can do it with a loop in the shell:

for f in File*.tab; do
    mv -n -- "$f" "${f%%_S[0-9][0-9].tab}"
done

(Or "${f%%_S*.tab}" to match anything in place of the two digits, or use shopt -s extglob in Bash, and then "${f%%_S+([0-9]).tab}" to match any number of digits there.)

ilkkachu
  • 138,973