Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWK, SED, REGEX to rename files

I'm only learning to use REGEX, AWK and SED. I currently have a group of files that I'd like to rename - they all sit in one directory.

The naming pattern is consistent, but I would like to re-arrange the filenames, here is the format:

01._HORRIBLE_HISTORIES_S2.mp4
02._HORRIBLE_HISTORIES_S2.mp4

I'd like to rename them to HORRIBLE_HISTORIES_s01e01.mp4 - where the e01 is gleaned from the first column. I know that I want to grab "01" from the first column, stuff it in a variable then paste it after the S2 in each filename, at the same time I want to remove it from the beginning of the filename along with the "._", additionally I want to change the "S2" to "s02".

If anyone would be so kind, could you help me write something using awk/sed and explain the procedure, that I might learn from it?

like image 338
user3043123 Avatar asked Jan 26 '14 04:01

user3043123


People also ask

How do I rename a file in awk?

awk itself is not a file renaming tool. However, with its powerful text processing functionalities, awk can be used to process filenames and turn them into “mv oldName newName” commands. We can pipe those “mv” commands to a shell to run them.

How do you change a file name?

Open File Explorer by going to My Computer, or by pressing Windows Key + E on your keyboard. Find the file you want to rename, select it and select Rename on the ribbon (or press F2 on your keyboard). Type the new name you want the file to have and press Enter.


1 Answers

using AWK. rename file with first and second and 4th part

ls | while read file; do newfile=`echo $file | awk -F . '{print $1 "." $2 "." $4}'`; echo $newfile;  mv $file $newfile; done;
like image 144
user1276325 Avatar answered Sep 20 '22 18:09

user1276325