7

I have a folder that contains many files named with the same structure:

file.number1.2010.720p.otherinfo.mp4
file.number2.1990.720p.otherinfo.mp4
....

I would like to remove all the sequence:

YEAR.720p.otherinfo

So name will be:

file.number1.mp4
file.number2.mp4
....

And finally I would like to remove all dots (.) except the one at the end (.mp4)

What commands should I use to create such script ?

Eliah Kagan
  • 117,780
Hamza
  • 121
  • Useful commands are grep, sed, and awk. What have you tried, and did it work, or fail? Have you searched for answers, with a search such as https://www.google.com/search?q=mass+rename+linux ? BTW, Linux 14.04 has reached end-of-life https://ubuntu.com/about/release-cycle and we are limited to provide support to current versions https://askubuntu.com/help/on-topic , such as 16.04 LTS, 18.04 LTS, 19.04, and alternate flavors https://ubuntu.com/download/flavours – K7AAY Aug 13 '19 at 18:16
  • 2
    "What commands should I use to create such script ?" script?! we got a "rename" command for that ;-) Have a look at man rename and man rename.ul. – Rinzwind Aug 13 '19 at 18:22
  • I use Krusader file manager with krename . menu is file-multirename . you can prefilter using menu view-custom before that . – pierrely Aug 17 '19 at 05:59

4 Answers4

6

This will do the job:

Firstly: Install rename by running the following command in the terminal:

sudo apt install rename

Secondly: In the terminal,cd to the directory containing your files.

Finally: Rename the files to your desired format by running the following command in the terminal:

rename 's/^(.+)\.(.+)\.(.+)\.(.+)\.(.+)\.mp4$/$1$2.mp4/' *

Done


Notice:

To see how the rename command will operate on your files but without really renaming them ( just print the output to the terminal ), you can add the option -n after it. Like so:

rename -n 's/^(.+)\.(.+)\.(.+)\.(.+)\.(.+)\.mp4$/$1$2.mp4/' *

Explanation - as requested by Hamza:

Part # 1:

's/ORIGINAL/NEW/'

Substitutes the ORIGINAL string with the NEW string.

To see how it works as simple as it gets:

  • Please run in the terminal rename -n 's/file.number1.2010.720p.otherinfo.mp4/NEW.mp4/' *
  • Assuming you have a file named file.number1.2010.720p.otherinfo.mp4 in the current directory.
  • The output would be rename(file.number1.2010.720p.otherinfo.mp4, NEW.mp4)

Part # 2:

^(.+)\.(.+)\.(.+)\.(.+)\.(.+)\.mp4$

Starts at the beginning of the string ^ and then matches one or more character/s ( any character ) (.+) before the dot \.

This group is put into a variable $1 and repeated four more times ( five in total ) each group is put into a variable ( $2, $3, $4, $5 ) until .mp4 represented as \.mp4 is reached.

Makes sure the string ends with .mp4 by using the $ symbol which matches the end of the string.

This part is, however, a bit flexible and will give you undesired results if the file naming is inconsistent and you have files with more than five parts separated by dots in their names like file.number1.2010.720p.otherinfo.extrainfo.mp4

If this is the case, please substitute this part with the more strict regular expression below. This way it will only operate on files with five parts separated by dots in their names only:

^([^.]+)\.([^.]+)\.([^.]+)\.([^.]+)\.([^.]+)\.mp4$

Part # 3:

$1$2.mp4

Defines the new file name as what is under the variable for the first group $1 ( in this case file ) + what is under the variable for the second group $2 ( in this case number(x) ) + .mp4

Part # 4:

*

Operates on all the files in the current directory.

Raffa
  • 32,237
2

You can use cut and bash string manipulation:

First approach: only with cut:

for I in .*mp4
do
    echo mv "$I" "$(echo $I | cut -d. -f 1,2,6)"
done 

Explanation:

  • for... will select all .mp4 files
  • echo $I | will convert the I variable into an input for the next program
  • cut -d. -f 1,2,6 will select fields 1, 2 and 6 where fields are separated by .

But this approach can be problematic since, the 6th field may be not the extension.


So you can have a second way:

for I in .*mp4
do
    echo mv "$I" "$(echo $I | cut -d. -f 1,2).mp4"
done 

With that command, you force the extension to be .mp4. It will work, but what if you have not only mp4 files with the same naming pattern?


In that case, you can use extension selection:

for I in *mp4 *avi
do
    echo mv "$I" "$(echo $I | cut -d. -f 1,2).${I##*.}"
done
  • ${I##*.} will allow you to select what is behind the last . in variable I

Note: to prevent bad behavior, I added echo in the commands to see what they will do before running them really.

Mathieu
  • 361
1

Select all files and press F2; then do what's shown in the image below. bulk rename

https://docs.xfce.org/xfce/thunar/4.12/bulk-renamer/start

0

I always use Thunar (filemanager) for this just open it, select the files you want to alter (cut/paste/adjust/replace characters) and then rightclick->rename

you get a sophisticated menu with lots of options and ways to change your file names and their extensions, and see their previews in the right panel.