Fedora Linux Support Community & Resources Center
  #1  
Old 22nd April 2007, 02:46 PM
majdi Offline
Registered User
 
Join Date: Feb 2007
Posts: 160
tar a directory excluding a subdirectory

Hi,
I'm trying to tar a directory, but in the same command I want to exclude a subdirectory within the directory being tared. I tried using the following command;

Command used;
tar -cvf test.tar directory_name --exclude “/directory_name/sub-directory”

The command successfully tared the directory, but did not exclude the sub-directory. Cant see why this is happening? Can anyone help?

Current working directory;
/home/user/dump

Directory to be tared;
/home/user/dump/directory_name

System used;
FC6

Regards,

Majdi

Last edited by majdi; 23rd April 2007 at 06:42 AM.
Reply With Quote
  #2  
Old 22nd April 2007, 02:52 PM
leigh123linux's Avatar
leigh123linux Offline
Retired Administrator
 
Join Date: Oct 2006
Posts: 21,509
there should be a = sign

tar -cvf test.tar directory_name --exclude=/directory_name/sub-directory
__________________
My Hardware
- CPU: AMD Phenom II X6 Hex Core 1055T 95W Edition @3.5Ghz
- Motherboard: Gigabyte GA-880GM-UD2H
- Cooler: Corsair H50 CPU Cooler
- RAM: Corsair Dominator 8GB (4x2GB) DDR3 1600MHz
- Graphics: Gigabyte GeForce GTS 450 OC 1024MB GDDR5
Reply With Quote
  #3  
Old 22nd April 2007, 04:16 PM
marko's Avatar
marko Online
Registered User
 
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 5,488
Another point, that last argument for the excluded directory has to have
exactly the same naming format as the directory you're tarring even if
it really means the same thing, i.e. you'd agree that if you're
in /etc directory that "/etc/yum.repos.d" and "yum.repos.d" are
really the same directory but tar isn't that smart you have to
tell it the same formatting since it just compares the strings
in a dumb manner.

In the above if I read it literally:

Code:
tar -cvf test.tar directory_name --exclude=/directory_name/sub-directory
has no a leading slash on "directory_name" then
you're doing a relative path, so your --exclude= value has to
also be relative so don' t use the "/" after the "="

Code:
tar -cvf test.tar directory_name --exclude=directory_name/sub-directory
I tested this by tarring a file to my home directory while occuping
the /etc directory and tarring yum.repos.d

Works incorrectly with "/" after =
Code:
$> pwd
/etc
$> tar cvf /home/mos/foo.tar yum.repos.d --exclude=/etc/yum.repos.d/unused
yum.repos.d/
yum.repos.d/fedora-extras.repo
yum.repos.d/dries.repo
yum.repos.d/macromedia-i386.repo
yum.repos.d/freshrpms.repo
yum.repos.d/fedora-core.repo
yum.repos.d/unused/
yum.repos.d/unused/livna-testing.repo
yum.repos.d/unused/fedora-extras-development.repo
yum.repos.d/unused/fedora-development.repo
yum.repos.d/unused/fedora-updates-testing.repo
yum.repos.d/unused/livna-devel.repo
yum.repos.d/unused/fedora-legacy.repo
yum.repos.d/fedora-updates.repo
yum.repos.d/livna.repo
Works correctly
Code:
$> tar cvf /home/mos/foo.tar yum.repos.d --exclude=yum.repos.d/unused
yum.repos.d/
yum.repos.d/fedora-extras.repo
yum.repos.d/dries.repo
yum.repos.d/macromedia-i386.repo
yum.repos.d/freshrpms.repo
yum.repos.d/fedora-core.repo
yum.repos.d/fedora-updates.repo
yum.repos.d/livna.repo
Mark
Reply With Quote
  #4  
Old 23rd April 2007, 08:14 AM
majdi Offline
Registered User
 
Join Date: Feb 2007
Posts: 160
Thanks guys,

I tried Mark's code and it worked fine.

Code:
tar -cvf test.tar directory_name --exclude=directory_name/sub-directory
I tried the same code only this time using the absolute path, it worked but it tared the directory with the same directory structure as the absolute path. Why is this happening? And how do I avoid this?

Code:
tar -cvf test.tar /home/user/dump/directory_name --exclude=/home/user/dump/directory_name/sub-directory
I have another question on the tar issue; Is there a way you can tar only the files in a directory so that when you extract that tar file it extracts those file and places them in the current working directory. This avoids having to move the files out of the tared directory and into the current working directory.

Thanks again.

Majdi

Last edited by majdi; 24th April 2007 at 07:15 AM.
Reply With Quote
  #5  
Old 23rd April 2007, 12:28 PM
jbannon Offline
Registered User
 
Join Date: Dec 2005
Posts: 909
Quote:
I have another question on the tar issue; Is there a way you can tar only the files in a directory so that when you extract that tar file it extracts those file and places them in the current working directory. This avoids having to move the files out of the tared directory and into the current working directory.
Change to the source directory first then run tar.
__________________
Best regards,
Jim Bannon
(Registered Linux User #405603 :) )
Reply With Quote
  #6  
Old 23rd April 2007, 02:29 PM
marko's Avatar
marko Online
Registered User
 
Join Date: Jun 2004
Location: Laurel, MD USA
Posts: 5,488
Quote:
I have another question on the tar issue; Is there a way you can tar only the files in a directory so that when you extract that tar file it extracts those file and places them in the current working directory.
you can do this ("O" option extracts to standard out, then use ">" to redirect to a local file
of the same name )

tar xOf test.tar dir1/dir2/filename.txt > filename.txt

sometimes this is handy if the tar file has the files in various staggered dirs but
you just want to drop them in one directory.

Mark
Reply With Quote
  #7  
Old 24th April 2007, 07:25 AM
majdi Offline
Registered User
 
Join Date: Feb 2007
Posts: 160
Thanks again guys,

I tried this in the source directory and it worked fine. With this code, I don't have to move the files out of the tared directory and into the working directory after extracting, all I have to do now is run extract.

Code:
[source-directory]# tar -cvf test.tar *
Regards,

Majdi
Reply With Quote
Reply

Tags
directory, excluding, subdirectory, tar

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Moving everything in a directory to a subdirectory daviddoria Using Fedora 1 19th November 2008 11:18 PM
creating a subdirectory mat.hunt Using Fedora 5 11th April 2007 03:16 PM
creating a subdirectory mat.hunt Using Fedora 1 11th April 2007 12:42 PM
mv and wildcards: cannot move `A' to a subdirectory of itself brianafischer Using Fedora 7 16th March 2006 06:14 PM


Current GMT-time: 03:06 (Wednesday, 19-06-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat