Linuxxon
Linuxxon

Reputation: 411

Find files and execute command

I have a lot of rar archives structured in individual folders and would like to script unpacking them all.

I'm having trouble figuring out how it should be done and need some help.

#!/bin/bash
## For all inodes
for i in pwd; do
    ## If it's a directory
    if [ -d "$i" ] then
        cd $i

        ## Find ".rar" file
        for [f in *.rar]; do
            ./bin/unrar x "$f" # Run unrar command on filename
            cd ..
        done
    done
done

I am not familiar with bash scripting and I assume the code is wrong more than once. But I guess this should be the basic structure

Upvotes: 15

Views: 22087

Answers (2)

jherran
jherran

Reputation: 3367

You don't need a script.

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

Additionally, you could pass the results of find to unrar command.

find . -name "*.rar" | xargs unrar x

Upvotes: 10

hek2mgl
hek2mgl

Reputation: 158220

You can use the find command:

find -name '*.rar' -exec unrar x {} \;

find offers the option exec which will execute that command on every file that was found.

Upvotes: 26

Related Questions