LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-24-2007, 11:24 AM   #1
fw12
Member
 
Registered: Mar 2006
Distribution: Fedora core, Ubuntu
Posts: 175

Rep: Reputation: 31
how to use TAR command


I use

tar -zcvpf /backup/user/archive.tar.gz --directory /home/user --exclude=*.tar.* --exclude=*.rpm

to tar up files in a folder.

As you can see, I use --exclude=*.foo to exclude files that I don't want included.

Now, instead of --exclude, is there a way to "include" just a particular file type?

I read the manual for tar, but I didn't find anything.

For example, let's say I just want to archive *.php files in a folder that contains dozens of file types, how would I do that? Right now, my only option is to use --exclude over and over again until I've listed every single file type I don't want archived.

TIA.
 
Old 10-24-2007, 11:25 AM   #2
AlucardZero
Senior Member
 
Registered: May 2006
Location: USA
Distribution: Debian
Posts: 4,824

Rep: Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615Reputation: 615
Code:
tar -zcvpf /backup/user/archive.tar.gz --directory /home/user *.php
?
 
Old 10-24-2007, 11:32 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Why don't simply...?
Code:
cd /path/to/directory/containing/files/to/archive
tar -zcvpf /backup/user/archive.tar.gz *.php
Finer search criteria can be done using tar in conjunction with the find command.

Edit: sorry for repetition, I did not see reply by AlucardZero before posting!

Last edited by colucix; 10-24-2007 at 11:33 AM.
 
Old 10-24-2007, 11:43 AM   #4
fw12
Member
 
Registered: Mar 2006
Distribution: Fedora core, Ubuntu
Posts: 175

Original Poster
Rep: Reputation: 31
Very good. Thank you!
 
Old 10-24-2007, 11:43 AM   #5
The_JinJ
Member
 
Registered: Apr 2004
Location: Scotland
Distribution: Suse, OpenWRT
Posts: 299

Rep: Reputation: 30
Too late lol! Tried something different from above

find . -size -400 -print > small-files
tar -c -v -z -T small-files -f little.tgz

Last edited by The_JinJ; 10-24-2007 at 11:50 AM.
 
Old 10-24-2007, 12:15 PM   #6
fw12
Member
 
Registered: Mar 2006
Distribution: Fedora core, Ubuntu
Posts: 175

Original Poster
Rep: Reputation: 31
It didn't work.

I guess I did a poor job of explaining what I wanted.

The TAR command would normally tar up files and sub-folders within the directory you specify.

The following:

cd /path/to/directory/containing/files/to/archive
tar -zcvpf /backup/user/archive.tar.gz *.php

only tarred up *.php files within /path/to/directory/containing/files/to/archive.
It doesn't touch any *.php files inside sub-folders below /path/to/directory/containing/files/to/archive

So if I have /path/to/directory/containing/files/to/archive/subfolder/index.php, it doesn't get archived. But I want it to be included.

Last edited by fw12; 10-24-2007 at 12:17 PM.
 
Old 10-24-2007, 12:49 PM   #7
fw12
Member
 
Registered: Mar 2006
Distribution: Fedora core, Ubuntu
Posts: 175

Original Poster
Rep: Reputation: 31
TAR has the option --recursion, but it still did not work:

tar -zcvpf --recursion /backup/user/archive.tar.gz *.php
 
Old 10-24-2007, 01:19 PM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Uh... ok! In this case you can use find in conjunction with tar, as in
Code:
find /some/dir -name "*.php" -print0 | xargs -0 tar --no-recursion -zcvpf archive.tar.gz
this will archive all .php files under /some/dir mantaining the subdir structure. Just a note on your last post:
Quote:
tar -zcvpf --recursion /backup/user/archive.tar.gz *.php
the name of the archive must immediately follow the -f option: putting --recursion between -f and archive name can generate an error. Cheers!
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how can i decompress this tar.tar file? hmmm sounds new.. tar.tar.. help ;) kublador Linux - Software 14 10-25-2016 02:48 AM
BackUp & Restore with TAR (.tar / .tar.gz / .tar.bz2 / tar.Z) asgarcymed Linux - General 5 12-31-2006 02:53 AM
Need help with 'tar' command danishmr Linux - General 2 06-07-2004 09:34 AM
tar command rromeli Linux - General 3 02-19-2002 12:15 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:49 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