2

I have a bunch of files named:

Brooklyn Nine-Nine S01 E01 [H264].mkv
Brooklyn Nine-Nine S01 E02 [H264].mkv
Brooklyn Nine-Nine S01 E03 [H264].mkv
Brooklyn Nine-Nine S01 E04 [H264].mkv
Brooklyn Nine-Nine S01 E05 [H264].mkv
Brooklyn Nine-Nine S01 E06 [H264].mkv
Brooklyn Nine-Nine S01 E07 [H264].mkv
Brooklyn Nine-Nine S01 E08 [H264].mkv
...

I want to rename them so that the space Between S01 and E08 is removed. example

Brooklyn Nine-Nine S01E08 [H264].mkv

I already found a command to remove all spaces:

IFS="\n"
for file in *.mkv;
do
    mv "$file" "${file/[[:space:]]}"
done

but I only want to remove space between Sxx and Exx.

Siva
  • 9,077
Stiefel
  • 31

2 Answers2

1

The easiest way is to use the rename tool, which lets you do a simple search-and-replace in many filenames:

rename [options] <expression> <replacement> <file>...

Something like this should do:

rename " E0" E0 Brooklyn*.mkv

Note that if you're using a Debian-like distribution, your rename command probably calls a Perl script with a different input syntax instead of the usual utility from util-linux. In that case, use rename.ul to call the right tool. Why is the rename utility on Debian/Ubuntu different than the one on other distributions, like CentOS?

TooTea
  • 2,388
  • 11
  • 15
0

The for loop can be edited as follows:

for file in *.mkv;
do
   mv "$file" "${file//S01 E0/S01E0}"
done

or using the rename command:

rename 's/S01 E0/S01E0/' *.mkv
GAD3R
  • 66,769