LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Unrar and Rename to Parent Folder Recursively (https://www.linuxquestions.org/questions/linux-newbie-8/unrar-and-rename-to-parent-folder-recursively-676297/)

Vasto 10-14-2008 11:47 AM

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

i92guboj 10-14-2008 03:50 PM

Quote:

Originally Posted by Vasto (Post 3309872)
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...

Vasto 10-14-2008 05:00 PM

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

i92guboj 10-14-2008 05:26 PM

Quote:

Originally Posted by Vasto (Post 3310145)
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.

jay73 10-14-2008 06:07 PM

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.

Vasto 10-14-2008 10:39 PM

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

i92guboj 10-15-2008 04:23 AM

Quote:

Originally Posted by Vasto (Post 3310382)
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.

jschiwal 10-15-2008 05:09 AM

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 $( ).

Vasto 10-15-2008 09:41 AM

Quote:

Originally Posted by i92guboj (Post 3310587)
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 (Post 3310606)
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?

i92guboj 10-16-2008 06:36 AM

Yes.

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

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


All times are GMT -5. The time now is 01:18 AM.