Operating System - HP-UX
1819977 Members
3242 Online
109607 Solutions
New Discussion юеВ

Re: Exclude tar a directory

 
addendum
Advisor

Exclude tar a directory

If use tar file from a directory , how to exclude a sub-directory in this directory ?

ll
drw-r--r-- 1 root root 4096 Oct 12 11:58 b
drw-r--r-- 1 root root 4096 Oct 12 10:54 c
drwxr-xr-x 2 root root 4096 Oct 12 11:57 d
drw-r--r-- 1 root root 4096 Oct 12 10:54 d
eg . I want to tar all files under the above directory but exclude the sub-directory "c" ( not exclude file ) , could advise how to do it ? thx
10 REPLIES 10
GGA
Trusted Contributor

Re: Exclude tar a directory

hello

u can use the -x option copy file or all files from a directory

regrads gga
Mugilvannan
Valued Contributor

Re: Exclude tar a directory

`-S'
`--separate-dirs'
Report the size of each directory separately, not including the
sizes of subdirectories.

`-x'
`--one-file-system'
Skip directories that are on different filesystems from the one
that the argument being processed is on.

`--exclude=PAT'
When recursing, skip subdirectories or files matching PAT. For
example, `du --exclude='*.o'' excludes files whose names end in
`.o'.

`-X FILE'
`--exclude-from=FILE'
Like `--exclude', except take the patterns to exclude from FILE,
one per line. If FILE is `-', take the patterns from standard
input.
If U need a helping hand, U will find one at the end of your arm
addendum
Advisor

Re: Exclude tar a directory

thx all replies,

from your suggestions , it seems no parameter can exclude the directory to tar , isn't it ? thx
Sandman!
Honored Contributor

Re: Exclude tar a directory

You can supply the directories to be tar'red up using command substitution. Assuming your curent directory is the one that contains the above sub-directories then:

# tar cvf /tmp/tarfile $(ll -tr | awk '$1~/^d/ && $NF!="c" {print $NF}')
Stephen Keane
Honored Contributor

Re: Exclude tar a directory

You could use pax to create the archive. you should be able to read/extract it using tar. pax will allow you to pipe the file list into it.

find ... | pax -wf archive_file

You can customize the find/pattern command to only list the files you want?
Peter Nikitka
Honored Contributor

Re: Exclude tar a directory

Hi,

you can use KSH filename generation to let the shell do this for you - just have no HPUX at hand to check, whether the posix shell has this capability as well:
(in ksh)
!(name) will match anything but 'name',
so
tar cf xxx.tar !(c)
will not archive 'c'.

mfG nik

The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Kent Ostby
Honored Contributor

Re: Exclude tar a directory

Put the directory names into a file called useme.

Let's assume that you're dir name that you're excluding isn't "c" , but "mydir":

ll | awk '{print $NF}' | grep -v mydir > useme

verify the list of directories and files:

cat useme

create tarball with just those files:

tar -cvf my.tar `cat useme`

"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Peter Nikitka
Honored Contributor

Re: Exclude tar a directory

Hi Kent,

remember, that a inverse-match (via grep -v mydir) has the bad effect of eliminating entries like
savemydir
as well.
More, the filename generation pattern can easily extended to more than one exception entry like
!(c|d)
which leads to multiple
grep -v one | grep -v other ..
in your solution.
Possible would be
ls | awk '{$1 ~ "mydir";next};{print}' to get the list.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Peter Nikitka
Honored Contributor

Re: Exclude tar a directory

Sorry,
I wanted to state

ls | awk '{$1 == "mydir";next};{print}' to get the list.
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Kent Ostby
Honored Contributor

Re: Exclude tar a directory

Peter -- you're correct ... it's a point I made somewhere else ...

Taking your comments along with what I wrote earlier:

Put the directory names into a file called useme.

Let's assume that you're dir name that you're excluding isn't "c" , but "mydir":

ls | awk '{$1 == "mydir";next};{print}' > useme

verify the list of directories and files:

cat useme

create tarball with just those files:

tar -cvf my.tar `cat useme`
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"