Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
8
rated 0 times [  8] [ 0]  / answers: 1 / hits: 739  / 2 Years ago, mon, may 30, 2022, 9:30:35

I have tried to extract multiple files from the directory where they're located with:


for i in $(find . -name '*.tgz'); do  # search the tgz files and extract them
tar -xvzf $i
done

I expected that the command would extract these files within the directory they were found in, however they are extracted outside their directory. How can I make sure that the .tgz files are extracted in the directory where they are located?


More From » bash

 Answers
0

You should avoid constructions like for i in $(find . -name '*.tgz'); do ... - see for example Why is looping over find's output bad practice? - instead, use -exec or -execdir to run the command directly on found files whenever possible.


For your application, -execdir will do exactly what you want i.e. execute tar relative to each .tgz file's containing directory:


find . -name '*.tgz' -execdir tar -xvf {} ;

See also Understanding the -exec option of find.


[#165] Tuesday, May 31, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tionpromoti

Total Points: 14
Total Questions: 112
Total Answers: 113

Location: Tonga
Member since Tue, Nov 30, 2021
3 Years ago
tionpromoti questions
Fri, Apr 15, 22, 03:58, 2 Years ago
Tue, Jun 1, 21, 23:05, 3 Years ago
Mon, Jul 5, 21, 06:11, 3 Years ago
;