Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use GNU parallel with find -exec?

I want to unzip multiple files,

Using this answer, I found the following command.

find -name '*.zip' -exec sh -c 'unzip -d "${1%.*}" "$1"' _ {} \;

How do I use GNU Parallel with the above command to unzip multiple files?


Edit 1: As per questions by user Mark Setchell

Where are the files ?

All the zip files are generally in a single directory.

But, as per my assumption, the command finds all the files even if recursively/non-recursively according to the depth given in find command.

How are the files named?

abcd_sdfa_fasfasd_dasd14.zip

how do you normally unzip a single one?

unzip abcd_sdfa_fasfasd_dasd14.zip -d abcd_sdfa_fasfasd_dasd14

like image 885
Tarun Maganti Avatar asked Dec 31 '22 09:12

Tarun Maganti


1 Answers

You can first use find with the -print0 option to NULL delimit files and then read back in GNU parallel with the NULL delimiter and apply the unzip

find . -type f -name '*.zip' -print0 | parallel -0 unzip -d {/.} {}

The part {/.} applies string substitution to get the basename of the file and removes the part preceding the . as seen from the GNU parallel documentation - See 7. Get basename, and remove last ({.}) or any ({:}) extension You can further set the number of parallel jobs that can be run with the -j flag. e.g. -j8, -j64

like image 133
Inian Avatar answered Jan 21 '23 17:01

Inian