Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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...
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.
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.
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:
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.
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 $( ).
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
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.