0

I have a file named

[  NarutoG] Naruto - 084 Roar, Chidori! Brother vs. Brother! [720p] [x265] [pseudo].mkv

I want to change it to

Naruto - 084 Roar, Chidori! Brother vs. Brother!.mkv

I have multiple files such as this, so I need a command to remove [ NarutoG], [720p], [x265], and [pseudo].

Can someone please give me the command to rename these files in Linux?

I saw this command:

rename 's/_200x200//' ./*_200x200*

... but I am a noob and therefore cannot make my own command.

Kusalananda
  • 333,661

2 Answers2

2

Using the perl rename utility.

Note: perl rename is also known as file-rename, perl-rename, or prename. Not to be confused with the rename utility from util-linux which has completely different and incompatible capabilities and command-line options.

rename -n 's/\s*\[\s*(NarutoG|720p|x265|pseudo)\s*\]\s*//ig' *.mkv

This will remove all instances of "NarutoG", "720p", "x265", and "pseudo" inside square brackets from the filenames, including any leading or trailing whitespace (both inside and outside the square brackets). You can add other words/strings inside the parentheses, just make sure they're separated by | (which is the regexp "alternation" character - this|that means "this OR that").

Square brackets have a special meaning inside a regular expression, so they need to be escaped with \, as \[ and \].

\s* means zero-or-more whitespace (tabs, spaces, newlines, etc) characters.

If you want it to remove all "word" characters (alphanumeric plus underscore plus some other punctuation and unicode characters - see man perlre and read about \w for details) inside square brackets, try this instead:

rename -n 's/\s*\[\s*\w+\s*\]\s*//ig' *.mkv

\w+ means one-or-more "word" characters.


Note that the -n option in both examples above makes them a dry run, so they will only show what they would do without actually renaming any files. Remove the -n, or replace it with -v for verbose output, when you've confirmed they do what you want.

cas
  • 78,579
0

The following will remove the [...] and also cleanup the beginning white spaces from the new filename, and also cleanup any whitespaces before the extension:

\ls|while read z;
do
echo mv "$z" "$(echo "$z"|sed -E 's/\[[a-zA-Z0-9\ ]+\]\s*?//g'|sed -E "s/\s+(\.\w{3})$/\1/g")"
done

you can remove the echo and rerun, to rename the files if they look correct

Make sure to create a backup of the files, before trying to rename them.

Ron
  • 111