Extract .tar file to current directory using Script shell

I am completely new to the bash world, and now I am working on a script that will cycle through a large directory and retrieve any .tar files that it finds at this current location.

I am using the following script:

for a in in /home/davidwright/attachments/*/*.tar do echo "extracting $x" tar -xvf $x done 

Currently the file is being extracted fine, but it is being extracted to the location of my script. I need to extract it from the current .tar directory.

The solution is probably very simple, but I cannot figure it out for me. Thanks!

+4
source share
1 answer

You can try:

 -C, --directory DIR change to directory DIR 

and $(dirname "$x") for the file directory

 for x in in /home/davidwright/attachments/*/*.tar do echo "extracting $x" tar -xvf "$x" -C "$(dirname "$x")" done 
+4
source

Source: https://habr.com/ru/post/1485614/


All Articles