ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I have a drive with multiple directories on, all of them containing rar files.
I want to batch-decompress them all into the directory the archives are in now. So I want something that finds a rar file, say /home/xx/movie1/file.rar
then do a "unrar x /home/xx/movie1/file.rar /home/xx/movie1/"
Unfortunately I'm not a programmer so I have only thought this through. Can somebody finish this for me?
find -name *.rar (should this be something that allows all cases (i.e. *.Rar *.RAR)) | unrar x "input from find" "input directory"
Hmmm... sorry urka58 but I think it does not work in the way the OP was looking for, e.g. it does not extract files in the directory where the archives reside. Keep it simple and do something like this
Code:
for archive in $(find . -iname "*.rar")
do
path_to_extract=$(dirname $archive)
unrar e $archive $path_to_extract > /dev/null
done
the test -iname (of the command find) matches entries in a case-insensitive way. The command dirname extract the path from the filename (actually it strips non-directory suffix from the filename). Finally unrar the archive specifying path_to_extract as last argument.
As urka58 correctly suggested, you may encounter problem if file names contain blank spaces. In this case a little workaround is needed.
ghostdog74, you sure? On my system "for archive in *.rar" does not recurses subdirectories. I'd rather do something a little more tricky like this
Code:
#!/bin/bash
archives=$(find . -iname "*.rar" -printf "%p@")
IFS="@"
for archive in $archives
do
path_to_extract=$(dirname $archive)
unrar e $archive $path_to_extract > /dev/null
done
@quackyo: here the trick is to use an Input Field Separator different from the default one (any of blank space, tab or newline). I choosed "@" assuming you don't have any file or directory names containing "@". In this way, names containing one or more blank spaces are not splitted inside the for loop.
ghostdog74, you sure? On my system "for archive in *.rar" does not recurses subdirectories. I'd rather do something a little more tricky like this
Code:
#!/bin/bash
archives=$(find . -iname "*.rar" -printf "%p@")
IFS="@"
for archive in $archives
do
path_to_extract=$(dirname $archive)
unrar e $archive $path_to_extract > /dev/null
done
no, it does not recurse directories, my bad for misinterpreting the requirement. however that's trivial to do.
Quote:
@quackyo: here the trick is to use an Input Field Separator different from the default one (any of blank space, tab or newline). I choosed "@" assuming you don't have any file or directory names containing "@". In this way, names containing one or more blank spaces are not splitted inside the for loop.
one way to go around that is include double quotes, at least it works for me eg
Code:
for files in "$(find . -iname '*.rar')"; do echo "$files"; done
for files in "$(find . -iname '*.rar')"; do echo "$files"; done
Yes, it works for me too. But the loop will run once and the unrar command will fail, since all the arguments will be on the same (unique) command line, even if filenames are separated by a newline. E.g. the following lines will stay all together as one block
Code:
/path/to/file_one.rar
/path/to/subdir/file with spaces.rar
/path/to/subdir/file_two.rar
This is actually one of the reasons why all the *nix guides advise against using blank spaces in filenames! Ciao
yes, you're right, I did not consider the output dir, but on the solution you proposed the imput dir is missing (@least not specified).
find . searches in $PWD which not necessarely is /path/to/rar_files..
A cd /path/to/rar_files can easily fix it
I am trying to use the info here to allow for white spaces in a multiple rar extract. I would want to manually state the output directory (I've called the file multiple-extract).
Code:
for archive in $(find . -iname "*.rar")
do
unrar e "$archive" "$1"/
done
I have even tried using single quotes against $archive. so the above works with rar files with no white spaces. I can do one of the below for it to work:
Code:
multiple-extract .
multiple-extract temp\ dir
multiple-extract "temp dir"
When I try with white spaces I get
Code:
[root@localhost test]# ls
evo 1.rar opera 1.rar
[root@localhost test]# multiple-extract .
UNRAR 3.71 beta 1 freeware Copyright (c) 1993-2007 Alexander Roshal
Cannot open $archive.rar
No such file or directory
No files to extract
UNRAR 3.71 beta 1 freeware Copyright (c) 1993-2007 Alexander Roshal
Cannot open $archive.rar
No such file or directory
No files to extract
UNRAR 3.71 beta 1 freeware Copyright (c) 1993-2007 Alexander Roshal
Cannot open $archive.rar
No such file or directory
No files to extract
UNRAR 3.71 beta 1 freeware Copyright (c) 1993-2007 Alexander Roshal
Cannot open $archive.rar
No such file or directory
No files to extract
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.