13

I am trying to extract many rar files at once, but with no successful. I am trying in the order:

>ls *.rar|xargs unrar x
>ls *.rar|xargs unrar e
>unrar e -r *.rar
>for f in *.rar;do unrar e “$f”;done

no ones works. Rar answers every times saying that there are not file to exctract.

>Extracting from damned_file.rar

 No files to extract

If I try to extract the file one by one, then all works fine

>unrar e damned_file.rar
 extracting damned_file.rar                        
 extracting dmaned_file.txt                            OK
 All OK
>

My version of rar is

UNRAR 4.10 freeware      Copyright (c) 1993-2012 Alexander Roshal

What I am doing wrong?

PS: The command

find . -name "*.rar" -exec unrar e {} \;

works fine, but the question remain the same. Why the previous commands fails?

4 Answers 4

15

Here is my go-to for loop:

for file in *.rar; do unrar e "$file"; done
1

xargs puts arguments behind the command provided to it up until the maximum command length for your current shell, so the command would be:

xargs unrar e damned_file.rar another_damned_file.rar yadf.rar

However, unrar only takes a single rar file as argument. The find command you specified runs unrar for every single file it finds, so the command is unrar e damned_file.rar, unrar e another_damned_file.rar.

2
  • 1
    so why for loop won't work?
    – emanuele
    Commented Apr 10, 2014 at 16:40
  • 1
    @mtak, isn't the option -n 1 solving that problem? ls *.rar | xargs -n 1 -t unrar l shows that the arguments are distributed correctly. Commented May 16, 2022 at 20:12
1

The trick is using the xargs option -n 1

ls *.rar | xargs -n 1 unrar x

-n 1 uses at most 1 argument per command line (see xargs man page)

Notice, though, that parsing the output of ls can easily lead to errors. A for cycle is always a much safer option.

You can read about this in Why you shouldn't parse the output of ls(1).

0

On CentOS 8, need use unar ,not unrar

find . -name "*.rar" -exec unar e {} \;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .