[SOLVED] Tar-ring files from other directories and current directory
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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:
$ 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.
@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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.