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.otherinfoSo 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 ?
34 Answers
This will do the job:
Firstly: Install rename by running the following command in the terminal:
sudo apt install renameSecondly: 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.mp4in 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.mp4Defines 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.
1You 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.mp4filesecho $I |will convert theIvariable into an input for the next programcut -d. -f 1,2,6will 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 variableI
Note: to prevent bad behavior, I added echo in the commands to see what they will do before running them really.
Select all files and press F2; then do what's shown in the image below.
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.