mass renaming files with complex filenames


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting mass renaming files with complex filenames
# 1  
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
# 2  
Old 02-11-2011
You want more of an assembly line, not command calls for one file:
Code:
 
export zct=$start_no
find $top-dir -type f -name '*[a-zA-Z].[am][vp][i3]' | sed '
  /avi$/!{
    /mp3$/!d
   }
 /avi$/{
    /MP3/!d
   }
  s/.*/mv "&" $(( zct += 1 )).mp3/
 ' >/tmp/mvall.sh

Review before executing! You might take all the files for one dir and do them at once in the background (parallel), just wrap lines for each dir in parens with amp: (...)& More than one process renaming in the same dir makes for file contention.
# 3  
Old 02-11-2011
EDIT: DGPickett beat me to it.

I don't know the answer to your question/problem, but in my experience, using 'ls' with a 'for' statement is just bad practice and even more so with spaces in your filenames. I would direct you to use the 'find' command.
# 4  
Old 02-11-2011
Try replacing your sed by:
Code:
sed -e 's/\([0-9]\{3\}\).*\(\..*\)/\1\2/' -e 's/.*\([0-9]\{3\}\)/Ep \1/'

This User Gave Thanks to Shell_Life For This Post:
# 5  
Old 02-11-2011
Oh, extracted numbers! Sure. I never use -e on sed, just pile the lines on the command line, but with pretty spacing, sed on sed lines and shell on shell lines. You, too, deserve readable code and a lower error rate!

Still a lot of mv fork() and exec(), so PERL might have an advantage, assuming it knows how to rewrite directories internally. In C or PERL, you link() the file to the new name and unlink() it from the old name.

BTW, directories never shrink, so making new directories is not a bad idea. Big directories can waste a lot of time.
# 6  
Old 02-11-2011
I strongly advise that you hide these filenames from Shell and avoid trying to put them into Shell environment variables. Try to work with external commands only.

Use unix "find" to generate a list of filenames into a file.

Then create a "sed" file containing your "sed" commands. Because Shell will not see them we only need to escape characters where "sed" requires it (which it will with some of these filenames!).

Run with the outline syntax:
Code:
sed -f sed_scriptfile filename

Throughout this exercise we must avoid Shell seeing the filenames.
# 7  
Old 02-12-2011
Quote:
Originally Posted by methyl
Throughout this exercise we must avoid Shell seeing the filenames.

Quite the contrary.

You want the shell to see the filenames, not the individual words making up those names.

The correct way is:
Code:
for file in *
do
  : whatever
done

This User Gave Thanks to cfajohnson For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question