LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-14-2008, 11:47 AM   #1
Vasto
LQ Newbie
 
Registered: Oct 2008
Posts: 10

Rep: Reputation: 0
Unrar and Rename to Parent Folder Recursively


I have a hard drive that has a bunch of folders that contain split rar archives. I'm looking to unrar them recursively and rename the resulting file to the name of the parent folder.

The command for unrar is:
Code:
unrar x -r *.rar
But I don't know how to combine that with rename, and also how to make rename use the parent folder as the new name.

Any help would be appreciated.

Thank you,
Vasto
 
Old 10-14-2008, 03:50 PM   #2
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,036

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
Quote:
Originally Posted by Vasto View Post
I have a hard drive that has a bunch of folders that contain split rar archives. I'm looking to unrar them recursively and rename the resulting file to the name of the parent folder.

The command for unrar is:
Code:
unrar x -r *.rar
But I don't know how to combine that with rename, and also how to make rename use the parent folder as the new name.

Any help would be appreciated.

Thank you,
Vasto
I am not sure of your purpose. However, a brief note.

If you are in a given directory, it's name (full name, from root) is in the var $PWD. So, in bash, you can use ${PWD##*\/} to extract the name of the parent dir (cutting from the beginning to the last '/'). So, "${PWD##*\/}.rar" would probably be the name of the file you want to create.

In other shells you might need another syntax, or use sed to edit the path. There might be some command that can do that in a simpler, way, I don't know...
 
Old 10-14-2008, 05:00 PM   #3
Vasto
LQ Newbie
 
Registered: Oct 2008
Posts: 10

Original Poster
Rep: Reputation: 0
Ok, so inorder to rename the extracted files I would want to do something like this?

Code:
unrar x -r *.rar | mv *.avi ./${PWD##*\/}.avi
EDIT: I just tried that and I received this as an error:
Code:
mv: cannot stat `*.avi': No such file or directory

Last edited by Vasto; 10-14-2008 at 05:05 PM.
 
Old 10-14-2008, 05:26 PM   #4
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,036

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
Quote:
Originally Posted by Vasto View Post
Ok, so inorder to rename the extracted files I would want to do something like this?

Code:
unrar x -r *.rar | mv *.avi ./${PWD##*\/}.avi
EDIT: I just tried that and I received this as an error:
Code:
mv: cannot stat `*.avi': No such file or directory
The first things is that the pipe there is not going to do anything.

The sedond thing is that, to be able to help any further, I would need at least one example of:

1.- the original rar files, names
2.- the resulting files, when you unrar them
3.- what do you intend to do with these resulting files


I think that on each dir, you have many rar files. After uncompressing all of them, you get many avi files. I think that's correct. Let me know if it's not.

But, I have no clue what do you intend to do with those avi files.
 
Old 10-14-2008, 06:07 PM   #5
jay73
Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 130Reputation: 130
If those are split files and they are numbered (e.g. part1.rar, part2.rar etc.), there is a good file that each of them will be extracted mulitple times as extracting the first part often results in the otter parts being extracted and joined too. You should really use this then:
for file in *1.rar; do unrar e $file; done
However, some split files do extract as individual parts so you may need to check carefully before you throw anything away.

Last edited by jay73; 10-14-2008 at 06:08 PM.
 
Old 10-14-2008, 10:39 PM   #6
Vasto
LQ Newbie
 
Registered: Oct 2008
Posts: 10

Original Poster
Rep: Reputation: 0
Name of directory where the movies are stored (Media is the name of my hard drive):
Code:
/media/Media/Movies
Then inside the folder Movies, I have a whole bunch of movies inside directories.
Code:
/media/Media/Movies/The_Little_Shop_of_Horrors/
/media/Media/Movies/Detour/
/media/Media/Movies/House_On_Haunted_Hill/
/media/Media/Movies/The_Last_Man_On_Earth/
etc....
Inside all the directories there are split rar's. For example, inside "/media/Media/Movies/The_Little_Shop_of_Horrors/":
Code:
lsoh_XVID_1960_Comedy.r01
lsoh_XVID_1960_Comedy.r02
lsoh_XVID_1960_Comedy.r03
lsoh_XVID_1960_Comedy.r04
...
lsoh_XVID_1960_Comedy.r45
lsoh_XVID_1960_Comedy.rar
Inside the arhives is the file:
Code:
lsoh_XVID_1960_Comedy.avi
So what I want to do is extract lsoh_XVID_1960_Comedy.rar and rename the resulting file (lsoh_XVID_1960_Comedy.avi) to the name of the parent folder. Resulting in:
Code:
The_Little_Shop_of_Horrors.avi
 
Old 10-15-2008, 04:23 AM   #7
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,036

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
Quote:
Originally Posted by Vasto View Post
So what I want to do is extract lsoh_XVID_1960_Comedy.rar and rename the resulting file (lsoh_XVID_1960_Comedy.avi) to the name of the parent folder. Resulting in:
Code:
The_Little_Shop_of_Horrors.avi
Nice. If there's one and only one .rar file and one and only one resulting avi file, then it's easy.

Code:
find . -type d | while read dir
do
  cd "$dir"
  find . -name \*.rar -exec unrar x '{}' \;
  find . -name \*.avi -exec mv '{}' "${PWD##*\/}.avi" \;
  cd ..
done
Completely untested, so if I were you I would do some controlled tests before launching this blindly. As said, this will only work if there's only one rar file and one resulting avi file on each directory (i.e. it will not work if the file extension is .mpg). That's the basic idea.

If it works you can use a similar find command to delete all the rar .r?? files.
 
Old 10-15-2008, 05:09 AM   #8
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Just use unrar on the .rar file. You don't join them. It will be reassembled from the parts. If the basename of the .rar file is the name matches the name of the rar file, then use that. I think that the -r option is for creating an rar archive. If you use "x" instead of "e" then you will extract any subdirectories in the rar archive.

Code:
for rarfile in *.rar; do
  dirname=$(basename "$rarfile" .avi)
  mkdir "$dirname"
  unrar x *.rar $(basename "$rarfile" .rar)
If you read "unrar --help" you will see that there is an optional last argument with the directory to extract to.

You can also extract the name of the .avi file from the listing:
Code:
rar l test.rar

RAR 3.71   Copyright (c) 1993-2007 Alexander Roshal   20 Sep 2007
Shareware version         Type RAR -? for help

This is a test comment for a test run of rar.


Archive test.rar

Comment: This is a test comment for a test run of rar.


 Name             Size   Packed Ratio  Date   Time     Attr      CRC   Meth Ver
-------------------------------------------------------------------------------
 listing          1357      377  27% 31-07-08 03:42 -rw-r--r-- 34FA3260 m3b 2.9
 listing-demo.html     2476      674  27% 31-07-08 03:45 -rw-r--r-- 33F82C04 m3b 2.9
 lsmod.hpmedia     4185     1421  33% 01-08-08 11:29 -rw-r--r-- 4D79C7F9 m3b 2.9
 users               0        8   0% 22-09-08 21:14 -rw-r--r-- 00000000 m3b 2.9
 dlist             688      173  25% 31-08-08 09:16 -rw-r--r-- 7220559E m3b 2.9
-------------------------------------------------------------------------------
    5             8706     2653  30%

jschiwal@qosmio:~> rar l test.rar | awk '/html/{print $1}'
listing-demo.html
jschiwal@qosmio:~> name=$(basename $(rar l test.rar | awk '/html/{print $1}') .html)
jschiwal@qosmio:~> echo $name
listing-demo
note the advantage of using $(...) instead of backticks. You can have one $(...) expression embedded in another, and test the inner part first, and then hit the up arrow and surround the successful command with $( ).

Last edited by jschiwal; 10-15-2008 at 05:13 AM.
 
Old 10-15-2008, 09:41 AM   #9
Vasto
LQ Newbie
 
Registered: Oct 2008
Posts: 10

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by i92guboj View Post
Code:
find . -type d | while read dir
do
  cd "$dir"
  find . -name \*.rar -exec unrar x '{}' \;
  find . -name \*.avi -exec mv '{}' "${PWD##*\/}.avi" \;
  cd ..
done
Quote:
Originally Posted by jschiwal View Post
Code:
for rarfile in *.rar; do
  dirname=$(basename "$rarfile" .avi)
  mkdir "$dirname"
  unrar x *.rar $(basename "$rarfile" .rar)
I just re-read the unrar help file, and -r is for Recurse subdirectories, e extracts to current directory, and x extracts to the directory the rar files are in.

But I have a question, are the two above codes Bash scripts? If so I never worked with them before, so is there a website that is recommended for me to read beyond the typical google search result?
 
Old 10-16-2008, 06:36 AM   #10
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,036

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
Yes.

http://tldp.org/LDP/abs/html/

Despite it's name, it's suitable for beginners as well.
 
  


Reply

Tags
folder, mv, parent, rename, unrar


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to search through all files in a folder, recursively... MJBoa Linux - Software 13 09-14-2008 11:25 PM
automatically inherit parent folder permissions when copying files teixeira Linux - Newbie 3 07-08-2008 12:21 PM
FTP need to transfer entire folder recursively rolster Linux - Newbie 6 02-02-2006 08:54 AM
How to recursively rename files using their directory name pattern ceg4048 Linux - General 2 09-28-2005 01:16 PM
Moving certain files recursively preservice folder structure DigiTalk Linux - Newbie 11 09-08-2005 08:04 AM


All times are GMT -5. The time now is 02:16 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration