LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-25-2014, 07:57 PM   #1
draziw
LQ Newbie
 
Registered: Aug 2014
Posts: 3

Rep: Reputation: Disabled
Question 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!
 
Old 08-25-2014, 10:57 PM   #2
jdkaye
LQ Guru
 
Registered: Dec 2008
Location: Westgate-on-Sea, Kent, UK
Distribution: Debian Testing Amd64
Posts: 5,464

Rep: Reputation: Disabled
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
 
Old 08-25-2014, 11:35 PM   #3
draziw
LQ Newbie
 
Registered: Aug 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
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.
 
Old 08-26-2014, 12:43 AM   #4
jdkaye
LQ Guru
 
Registered: Dec 2008
Location: Westgate-on-Sea, Kent, UK
Distribution: Debian Testing Amd64
Posts: 5,464

Rep: Reputation: Disabled
That's great. Let us know how you make out. Be sure to post your solution to help others with a similar problem.
jdk
 
Old 08-27-2014, 02:56 PM   #5
initramfs
LQ Newbie
 
Registered: Aug 2014
Posts: 27

Rep: Reputation: Disabled
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.

Last edited by initramfs; 08-27-2014 at 03:10 PM.
 
Old 08-27-2014, 02:59 PM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,520

Rep: Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106
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
 
Old 08-27-2014, 03:04 PM   #7
initramfs
LQ Newbie
 
Registered: Aug 2014
Posts: 27

Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
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
 
Old 08-27-2014, 03:13 PM   #8
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,208

Rep: Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611
Talking

Quote:
Originally Posted by initramfs View Post
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.
 
Old 08-27-2014, 03:23 PM   #9
initramfs
LQ Newbie
 
Registered: Aug 2014
Posts: 27

Rep: Reputation: Disabled
Quote:
Originally Posted by szboardstretcher View Post
This is true. And usually it goes from complex, to simple..
I totally agree. The simpler and the least code, the better
 
Old 08-29-2014, 02:13 PM   #10
initramfs
LQ Newbie
 
Registered: Aug 2014
Posts: 27

Rep: Reputation: Disabled
No feedback from the OP, oh well... :/
 
Old 04-15-2015, 04:45 PM   #11
draziw
LQ Newbie
 
Registered: Aug 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
Smile 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!
 
Old 04-15-2015, 05:02 PM   #12
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,169

Rep: Reputation: 2493Reputation: 2493Reputation: 2493Reputation: 2493Reputation: 2493Reputation: 2493Reputation: 2493Reputation: 2493Reputation: 2493Reputation: 2493Reputation: 2493
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 )
 
Old 04-16-2015, 02:58 PM   #13
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,705

Rep: Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270
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...)

Last edited by jpollard; 04-16-2015 at 02:59 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
massive tar file, can't untar dtra Linux - Software 3 09-08-2011 04:32 AM
[SOLVED] tar multiple directories into one file davedpss Linux - Newbie 8 05-16-2011 02:21 PM
[SOLVED] command for tar'ing multiple sub-directories into a single tar file blainemiller Linux - Server 3 04-21-2011 02:05 PM
'Read-only file system' when untar a tar.bz2 file yinglcs Linux - General 1 02-18-2006 09:12 PM
Getting problem when untar the .tar.gz file? ramakumard77 Linux - General 2 11-29-2004 11:15 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration