Find files and execute command

I have many rar archives structured in separate folders and would like the script to unzip them all.

I am having trouble figuring out how this should be done and you need help.

#!/bin/bash ## For all inodes for i in pwd; do ## If it 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 the bash script, and I assume the code is wrong more than once. But I guess this should be the main structure

+8
linux bash
source share
2 answers

You can use the find :

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

find offers an exec option that will execute this command for each file found.

+12
source share

You do not need a script.

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

Alternatively, you can pass the search results to the unrar command.

 find . -name "*.rar" | xargs unrar x 
+5
source share

All Articles