Sponsored Content
Top Forums Shell Programming and Scripting mass renaming files with complex filenames Post 302495927 by VLaw on Friday 11th of February 2011 03:21:29 PM
Old 02-11-2011
mass renaming files with complex filenames

Hi,

I've got files with names like this :
[Various-PC3]_Some_Name_178_HD_[e2813be1].mp4
[TTB]_Some_Name_-_496_Vost_SD_(720x400_XviD_MP3).avi
Goffytofansub_Some name 483_HD.avi

And iam trying to rename it with a regular pattern. My gola is this :
Ep 178.mp4
Ep 496.avi
Ep 483.avi

I've tried using sed with substitution expressions :
Code:
 for f in `ls`
do
        OLD=$f
        NEW=$(echo $f | sed 's/.*\([0-9]\{3\}\).*\.\(.*\)/Ep \1.\2/1')
        echo -n "$OLD => $NEW (o/n) "
        read YES_NO
        [[ $YES_NO == "y" ]] && mv $OLD $NEW
done

the result is

Code:
Ep 813.mp4
Ep 400.avi
Ep 483.avi

One file correctly renamed :-/

This one :
Code:
 echo $f | sed 's/[^0-9]*\([0-9]\{3\}\).*\.\(.*\)/Ep \1.\2/1'

Went wrong with the first one
Code:
[Various-PC3Ep 178.mp4
Ep 496.avi
Ep 483.avi

Then I tried perl. But considering that I have very few knowledges in it, I spent many time on my script
Code:
open(LS, "ls -1 |");
foreach (<LS>)
{
        $old = $_;
        $new = s/(?![0-9]{3})*([0-9]{3}).*\.(.*)/Ep $1.$2/;
        print "$old => $new (o/n) ";
        chomp($yes_no = <STDIN>);
        next unless ( $yes_no == "y");
        system("mv $old $new");
} #foreach

for an unexpected result :
Code:
Ep 418.mp4
 => 1 (o/n) <- waiting here for input

I've also tried some alternatives of the beginning :
Code:
foreach(`ls`)

result
Code:
Ep 418.mp4
  => 1 (o/n) <- waiting here for input

This one :
Code:
foreach (system("ls -1"))

gave me
Code:
 Various-PC3]_Some_Name_178_HD_[e2813be1].mp4
[TTB]_Some_Name_-_496_Vost_SD_(720x400_XviD_MP3).avi
Goffytofansub_Some name 483_HD.avi
0 =>  (o/n) <- waiting here for input

Any idea on how to reach my goal ?

Thx in advance !

Last edited by VLaw; 02-11-2011 at 04:25 PM.. Reason: Details of 1st bash script
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Renaming of multiple filenames

Hi All, I need to rename the file names. I need to rename for example as follows file001 to flat1 file100 to flat100 Thanks Shash :confused: (7 Replies)
Discussion started by: shashi_kiran_v
7 Replies

2. UNIX for Dummies Questions & Answers

Renaming of multiple filenames

Hi All, I need to rename the multiple file names. I need to rename for example as follows bas100e1_jun05 to FLAT1 bas100e2_jun05 to FLAT2 bas100e18_jun05 to FLAT18 Please not that I can cut_jun05 from the filename. Madhan had helped with a similar kind of script. But this is a new... (4 Replies)
Discussion started by: shashi_kiran_v
4 Replies

3. AIX

VI questions : mass changes, mass delete and external insert

Is it possible in VI to do a global change but take the search patterns and the replacement patterns from an external file ? I have cases where I can have 100,200 or 300+ global changes to do. All the new records are inside a file and I must VI a work file to change all of them. Also, can... (1 Reply)
Discussion started by: Browser_ice
1 Replies

4. UNIX for Dummies Questions & Answers

Mass file renaming

Hi :) Is there any command I could use to rename a bunch of files resident of the same location to their original name plus a fixed text string of my own? Example: File1 File2 File3 Output: File1.txt File2.txt File3.txt This is easy using a "for" loop but what I want is a one-line... (5 Replies)
Discussion started by: Indalecio
5 Replies

5. Shell Programming and Scripting

Renaming Movies (or Flipping Portions of Filenames Using sed or awk)

Hey folks My problem is simple. For my first stash of movies, I used a naming convention of YEAR_MOVIE_NAME__QUALITY/ for each movie folder. For example, if I had a 1080p print of Minority Report, it would be 2002_Minority_Report__1080p/. The 2nd time around, I changed the naming convention... (4 Replies)
Discussion started by: ksk
4 Replies

6. UNIX for Dummies Questions & Answers

renaming filenames

I have 7 files with 7 different names coming into a specified folder on weekly basis, i need to pick a file one after another and load into oracle table using sql loader. I am using ksh to do this. So in the process if the file has error records and if sql loader fails to load into oracle tables,... (0 Replies)
Discussion started by: vpv0002
0 Replies

7. Shell Programming and Scripting

Complex renaming then moving files

I am a biologist and using an program on a computer cluster that generates a lot of data. The program creates a directory named ExperimentX (where X is a number) that contains files "out.pdb" and "log.txt". I would like to create a script that renames the out.pdb file to out_ExperimentX.pdb (or... (1 Reply)
Discussion started by: yaledocker
1 Replies

8. Shell Programming and Scripting

Renaming files & folder according to the similarities in filenames

hello does someone want to help me for this one ? i want to rename files & a folder according to the similarities in filenames for example : the file with the good name cglogo tougl1953 dgmel bogd 01 -- ttgductoog ggdté gollogtd.ext1the others files needed to be renamed cglogo... (5 Replies)
Discussion started by: mc2z674gj
5 Replies

9. Shell Programming and Scripting

Renaming Filenames by replacing a part

Hi, I have little experience on Shell scripts, I searched the forum but couldn't make out what I want. I want to rename a set of files to a new file name a_b_20100101 c_d_20100101 ....................... ...................... I want to rename the files to a_b_20140101... (5 Replies)
Discussion started by: JaisonJ
5 Replies

10. Shell Programming and Scripting

Renaming files with Spaces in Filenames

Entry level scripter. Any help appreciated. for file in *; do rename '4321_' '' $file ; done Doesn't work for files with spaces in between FOr eg 4321_1004.dat is renamed to 1004.dat but 4321_1004 2008.dat stays the same (1 Reply)
Discussion started by: davnavin
1 Replies
All times are GMT -4. The time now is 12:19 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy