Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find files and execute command

Tags:

linux

bash

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

like image 652
Linuxxon Avatar asked Nov 13 '14 14:11

Linuxxon


People also ask

What is find exec command in Linux?

Find exec causes find command to execute the given task once per file is matched. It will place the name of the file wherever we put the {} placeholder. It is mainly used to combine with other commands to execute certain tasks.

How do I run multiple commands in find exec?

Find exec multiple commands syntaxThe -exec flag to find causes find to execute the given command once per file matched, and it will place the name of the file wherever you put the {} placeholder. The command must end with a semicolon, which has to be escaped from the shell, either as \; or as " ; ".


1 Answers

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.

like image 179
hek2mgl Avatar answered Oct 03 '22 00:10

hek2mgl