Recursively search for RAR files and extract them, once extracted, remove them

I would like to recursively search a folder, extract the RAR file, and remove .nfo, .svf and the RAR files (*.rar and *.r1, *.r2, etc).

I found a command to recursively extract, but not to remove:

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

1 Answer

Tack another -exec predicate (with rm inside that), which would only be run if the first one succeeds:

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

-exec rm {} \; will only be run to remove the .rar file if the unrar-ing succeeds (-exec unrar e {} \;) i.e. unrar returns with exit status 0.


You can also do this using bash, using globstar option to recursively match glob pattern (*.rar), and rm each file if unrar-ing is successful:

shopt -s globstar
for f in **/*.rar; do unrar e "$f" && rm "$f"
done
4

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like