LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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-06-2015, 06:32 AM   #1
technizia
LQ Newbie
 
Registered: Aug 2014
Posts: 6

Rep: Reputation: Disabled
Tar-ring files from other directories and current directory


Hi,

Let's say, my current directory is /home. I have a file1 in my /home directory. I have another directory (say dir2) which has a lot of files. I want to tar selective 2 files (say file2, file3) from the directory dir2 and file1 from my /home into a tar.gz file.

Resultant tar needed : /home/1.tar.gz
Files to be tar'd :
1) /path/to/dir2/file2
2) /path/to/dir2/file3
3) /home/file1 (current directory)

I've tried -C option (because I want only the files in the resultant tar and not the directory structures) to tar (1) and (2) in this manner :
"tar -zcvf 1.tar.gz -C /path/to/dir2 file2 -C /path/to/dir2 file3

I'm not sure how to include the current directory's file1 in the above tar command.

Please help.

PS : The files need to be tar'd in the given order.

-T

Last edited by technizia; 08-06-2015 at 06:57 AM.
 
Old 08-06-2015, 07:20 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Do not believe you need the -C nor the -z directives. Just use the literal pathnames for each file. And tar detects the target filename and appropriately does the zip operation on its own:
Code:
$ tar -cvf new.tar.gz sub-dir1/a.txt sub-dir2/b.txt c.txt
sub-dir1/a.txt
sub-dir2/b.txt
c.txt
$ ls -l
$ new.tar.gz
-rw-r--r-- 1 myuser mygroup 10240 Aug 6 08:12 new.tar.gz
$ tar -tvf new.tar.gz
-rw-r--r-- myuser/mygroup     2 2015-05-07 07:43 sub-dir1/a.txt
-rwxr-xr-x myuser/mygroup     2 2014-09-26 14:40 sub-dir2/b.txt
-rw-r--r-- myuser/mygroup   219 2014-08-13 09:08 c.txt
Note you can also do something like this:
Code:
$ tar -cvf new.tar.gz sub-dir1/a.txt sub-dir2/b.txt
sub-dir1/a.txt
sub-dir2/b.txt
$ tar -rvf new.tar.gz c.txt
c.txt
$
The result is the same as above.

Perhaps it is easy if you remember that tar is Tape ARchive and all it is doing is creating an archive file, or a concatenation of all the files you add to that archive.

Last edited by rtmistler; 08-06-2015 at 07:21 AM.
 
Old 08-06-2015, 07:25 AM   #3
technizia
LQ Newbie
 
Registered: Aug 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
@rtmistler If you create the tar in this format - tar -cvf new.tar.gz sub-dir1/a.txt sub-dir2/b.txt c.txt, when you untar it is going to create the directory structure as is (sub-dir1/a.txt). But I want only the files to be tar'd i.e, only a.txt, b.txt, c.txt should be extracted when I untar and not with the directory structure. This is why I use -C to change directory and pick only the files.
 
Old 08-06-2015, 07:29 AM   #4
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
You don't need the switch -C. You can simply add the files by giving the pathnames to tar. Like this:
Code:
tar -cvf tarball.tar dir1/foo/file1 dir2/bar/file2 file_current_dir 
a dir1/foo/file1
a dir2/bar/file2
a file_current_dir
Code:
tar -tvf tarball.tar
-rw-r--r--  0 HMW HMW       0  6 Aug 14:22 dir1/foo/file1
-rw-r--r--  0 HMW HMW       0  6 Aug 14:22 dir2/bar/file2
-rw-r--r--  0 HMW HMW       0  6 Aug 14:23 file_current_dir
Best regards,
HMW

Edit: rtmistler already answered. Ignore.

Last edited by HMW; 08-06-2015 at 07:30 AM.
 
Old 08-06-2015, 07:31 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Normally I prefer to retain the directory structure. But given your desire, then you'd use the fully qualified pathnames:
Code:
tar -cvf new.tar.gz -C /home/myuser/sub-dir1 a.txt -C /home/myuser/sub-dir2 b.txt -C /home/myuser c.txt
 
1 members found this post helpful.
  


Reply



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
tar archive - How is a single directory extracted without the parent directories? Siopa Linux - Software 12 01-22-2014 03:49 AM
tar all files and folders in the current directory riahc3 Linux - Newbie 18 12-13-2013 03:06 AM
move all files in current directory into a subdirectory in the current directory jakykong Linux - Newbie 8 07-16-2013 11:46 PM
extracting .tar.gz files in current directory... mitesh.ever Linux - Newbie 4 12-09-2008 06:03 AM
How do I tar files from different directories glenn69 Linux - General 1 11-12-2007 04:20 PM

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

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

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
Open Source Consulting | Domain Registration