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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Renaming Movies (or Flipping Portions of Filenames Using sed or awk)
# 1  
Old 07-16-2010
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 to one that would be easier to read for humans and machines. It changed to MOVIE_NAME_YEAR[_U]/. This was done since when I ls the directory, this will give me movies by movie name, making it easier to scroll through. Also, quality was irrelevant for me know (in the directory name itself) so it went away. The third change was the addition of `_U'. This is a flag at the end of each movie directory name if the movie has not yet been watched. Thus, if I wanted to see list all movies in my stash that I haven't yet seen, then I would do `ls *_U'. So Minority Report would be Minority_Report_2002/. If I hadn't yet seen it, it would be Minority_Report_2002_U.

Now, I have these two different stashes of movies named under different conventions. I want to put them all together and name them according to convention 2 (i.e. MOVIE_NAME_YEAR[_U]).

In other words, I want to change the names of a set of directories from YEAR_MOVIE_NAME__QUALITY to MOVIE_NAME_YEAR. I'll manually add the `_U' flags later. I'm sure this is trivial to solve using sed or awk, but I don't have much experience in either.

Any thoughts?
# 2  
Old 07-16-2010
You can use something like this:

Code:
for movie_dir in [12][09][0-9][0-9]_*/; do
  year=${movie_dir%%_*} tmp=${movie_dir%__*}
  movie=${tmp#${year}_}
  [ -d "$movie"_"$year" ] && {
    printf 'directory %s already exists ...\n' "$movie"_"$year"
    continue
    }
  mv -- "$movie_dir" "$movie"_"$year"
done

# 3  
Old 07-16-2010
Code:
#!/bin/bash

ls . | while read OLD_NAME
do
  YEAR=$( echo $OLD_NAME | awk -F '_' '{print $1}' )
  CRAP=$( echo $OLD_NAME | awk -F '_' '{print $NF}' )
  NEW_NAME=$( echo $OLD_NAME | sed -e "s/$CRAP/$YEAR/" -e "s/$YEAR\_//" )
  mv -v $OLD_NAME $NEW_NAME
done

exit 0
#finis

Code:
[house@leonov] ls .
2001_Some_Film_whatever/ 2002_Some_Other_Film_nn/ 
[house@leonov] bash code.bash
2001_Some_Film_whatever -> Some_Film_2001
2002_Some_Other_Film_nn -> Some_Other_Film_2002

You may want to extend the loop with a read statement to add the "u" switch interactively while converting the file names one by one ...
# 4  
Old 07-17-2010
Get the new name directly from old folder name.

Code:
for movie_dir in [12][09][0-9][0-9]_*/; do
  new_name=$(echo $movie_dir |awk 'BEGIN{FS=OFS="_"}{print $2,$3,$1}')
  [ -d $new_name ] && {
    printf 'directory %s already exists ...\n' $new_name
    continue
    }
  mv "$movie_dir" "$new_name"
done



---------- Post updated at 07:12 PM ---------- Previous update was at 07:10 PM ----------

Quote:
Originally Posted by radoulov
You can use something like this:
mv -- "$movie_dir" "$movie"_"$year"
[/CODE]
What's the purpose to add "--" after mv, I never use like this way before.
# 5  
Old 07-17-2010
Quote:
Originally Posted by rdcwayx

[...]What's the purpose to add "--" after mv, I never use like this way before.
-- indicates end of options. It's there for extra protection.
Consider the extreme case when the movie name begins with a dash:
Code:
% mv 2010_-90_Celsius__1080p -90_Celsius_2010
mv: unknown option -- 9
Try `mv --help' for more information.
% mv -- 2010_-90_Celsius__1080p -90_Celsius_2010
%

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

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Match Fields between two files, print portions of each file together when matched in ([g]awk)'

I've written an awk script to compare two fields in two different files and then print portions of each file on the same line when matched. It works reasonably well, but every now and again, I notice some errors and cannot seem to figure out what the issue may be and am turning to you for help. ... (2 Replies)
Discussion started by: jvoot
2 Replies

2. UNIX for Beginners Questions & Answers

Match Strings between two files, print portions of each file together when matched ([g]awk)

I have two files and desire to use the strings from $1 of file 1 (file1.txt) as search criteria to find matches in $2 of file 2 (file2.txt). If matches are found I want to output the entire line of file 2 (file2.txt) followed by fields $2-$11 of file 1 (file1.txt). I can find the matches, I cannot... (7 Replies)
Discussion started by: jvoot
7 Replies

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

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

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

6. UNIX for Dummies Questions & Answers

renaming multiple files using sed or awk one liner

hi, I have a directory "test" under which there are 3 files a.txt,b.txt and c.txt. I need to rename those files to a.pl,b.pl and c.pl respectively. is it possible to achieve this in a sed or awk one liner? i have searched but many of them are scripts. I need to do this in a one liner. I... (2 Replies)
Discussion started by: pandeesh
2 Replies

7. Shell Programming and Scripting

mass renaming files with complex filenames

Hi, I've got files with names like this : _Some_Name_178_HD_.mp4 _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... (8 Replies)
Discussion started by: VLaw
8 Replies

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

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