LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How do I untar a tar.gz file to multiple directories? (https://www.linuxquestions.org/questions/linux-newbie-8/how-do-i-untar-a-tar-gz-file-to-multiple-directories-4175516355/)

draziw 08-25-2014 07:57 PM

How do I untar a tar.gz file to multiple directories?
 
Hey everyone,

I am trying to find a way to untar a file.tar.gz in my home/noob directory into 15 different directories.

where the tar.gz file is located: /home/noob
where the 15 directories are located: /home/noob/Staging/ 1-15 folders

Without having to extract the tarball individually, is there a simple way in one or two commands that can take the tarball and extract it to all 15 folders ?

Please and thanks!

jdkaye 08-25-2014 10:57 PM

I'm not sure I follow what your are trying to do and why. You could try something like this:
Code:

echo dir1 dir2 dir3 | xargs -n 1 cp file1
This will copy file1, the one you extracted from the tarball, to dir1, dir2, and dir3. xargs will call cp 3 times to do this, see the man page for xargs for details.
jdk

draziw 08-25-2014 11:35 PM

Thank you.

I was trying to use find, xargs, and tar ... I am going to try it your way.

I am just trying to automate some random tasks to be honest. I'm just trying to learn how to use different common commands and how to implement them together.

jdkaye 08-26-2014 12:43 AM

That's great. Let us know how you make out. Be sure to post your solution to help others with a similar problem.
jdk

initramfs 08-27-2014 02:56 PM

Code:

find /home/noob/Staging/ -maxdepth 1 -type d -print0 | while read -d $'\0' dir; do echo tar -zxvf /home/noob/file.tar.gz -C "$dir"; done
This will scam all directories in /home/noob/Staging/ and will untar the tar/gz file /home/noob/file.tar.gz into the top level of each directory in /home/noob/Staging/.

PS: The echo in red will give you a preview of of what the code will do without doing anything. Look at the output carefully and see if things look right.

If it looks OK, run the code again without the echo part.

suicidaleggroll 08-27-2014 02:59 PM

FYI - initramfs's post can also be simplified by replacing the find, print, while, read with a simple for loop and an if to check for directories:
Code:

for dir in /home/noob/Staging/*; do if [[ -d $dir ]]; then echo tar -zxvf /home/noob/file.tar.gz -C "$dir"; fi; done

initramfs 08-27-2014 03:04 PM

Quote:

Originally Posted by suicidaleggroll (Post 5228271)
FYI - initramfs's post can also be simplified by replacing the find, print, while, read with a simple for loop and an if to check for directories:
Code:

for dir in /home/noob/Staging/*; do if [[ -d $dir ]]; then echo tar -zxvf /home/noob/file.tar.gz -C "$dir"; fi; done

That's what's cool with linux, there will always be more ways to do a task :)

szboardstretcher 08-27-2014 03:13 PM

Quote:

Originally Posted by initramfs (Post 5228274)
That's what's cool with linux, there will always be more ways to do a task :)

This is true. And usually it goes from complex, to simple.. such as this. Say we have a file called textfile and we want to display it from bottom to top, or as I would say, backwards. In bash we might start with this:

Code:

reverse () {   
local line   
if IFS= read -r line   
 then       
  reverse         
  printf '%s\n' "$line"   
fi
}

Then we might use awk to make that smaller:

Code:

awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] }' textfile
Then we might use sed, to make it smaller:

Code:

sed '1!G;h;$!d' ${inputfile}
And eventually a unix-beard would jump in and say:

Code:

tac textfile
Because nearly everything that can be done to a file or string, has already been done and compiled into a program.

initramfs 08-27-2014 03:23 PM

Quote:

Originally Posted by szboardstretcher (Post 5228280)
This is true. And usually it goes from complex, to simple..

I totally agree. The simpler and the least code, the better :)

initramfs 08-29-2014 02:13 PM

No feedback from the OP, oh well... :/

draziw 04-15-2015 04:45 PM

Thanks for the feedback, everyone
 
Hey everyone.

Sorry for the late reply. At the time when I posted this, I was actually preparing for a job interview (yay!) that I actually got hired from, so the past 8 months has been a lot of hit-the-ground-running type of scenarios. I wanted to say thank you for all the help and contributions. I wish I still had the virtual machine I was using at the time so I could post the script and show it to you all, but it definitely used a lot of the methods you all mentioned on this post.

Thanks again!

John VV 04-15-2015 05:02 PM

on a desktop install of any major OS
r-click the tarbal and select "extract here" from the menu

terminal
tar -x tarbal.tar.gz


or the ( tarbal.tgz )

jpollard 04-16-2015 02:58 PM

An old solution (and a bit tacky, but I was impatient at the time):
Code:

...
for i in <list of directories> ; do
  tar -C $i -xf <tarfile> . &
done
wait

This is tacky in that it spawns a new process to handle each directory. It also does all of them in parallel. There are also several ways to the tar command itself. I think the one I originally used was
Code:

(cd $i; tar -xf <tarfile> . )&
The nice thing about the "wait" command was that without options or parameters, it waits for all subrocesses to finish before returning.

The other thing to note - don't do this for LOTS of diretories as it can tie up a server with large tarfiles. (I made that mistake from a client of a NFS server, and tied it up for about 15 minutes. Long enough for people to start running around to find out what was hanging...)


All times are GMT -5. The time now is 05:35 PM.