LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Excluding a directory with tar. (https://www.linuxquestions.org/questions/linux-newbie-8/excluding-a-directory-with-tar-4175573695/)

kb2tfa 03-01-2016 08:17 PM

Excluding a directory with tar.
 
Can I get a second set of eyes on this? I am new to tar. I want to backup the /var directory, but I don't want to backup the /var/tmp directory.

I've searched the internet high and low, and it's left me even more confused.

According to the manpage the option is --exclude=PATTERN

In a moment I will lay out what worked and didn't work. As for the manpage, does this mean I use an equals sign with pattern with no quotes, no equals sign pattern with quotes, or no equals sign no quotes, etc.

I tried this in my home directory and this worked. I created directory named dirtocompress with a few touch text files in it. I also created a directory dirtocompress/dirtoexclude with a few text files in it.

Here is what worked in that scenario: executed as regular user
Code:

tar czvpf test.tar.gz /dirtocompress --exclude dirtoexclude/*
Now I take that model, and apply it to my var directory situation, and it's a no go. Here is what I've tried in regards to the var/tmp exclusion: executed as root
Code:

tar czvpf /var/tmp/test.tar.gz /var --exclude var/tmp/*
tar czvpf /var/tmp/test.tar.gz /var --exclude 'var/tmp/*'

tar czvpf /var/tmp/test.tar.gz /var --exclude /var/tmp/*
tar czvpf /var/tmp/test.tar.gz /var --exclude '/var/tmp/*'


tar czvpf /var/tmp/test.tar.gz /var --exclude tmp/*
tar czvpf /var/tmp/test.tar.gz /var --exclude 'tmp/*'

tar czvpf /var/tmp/test.tar.gz /var --exclude '/tmp/*'
tar czvpf /var/tmp/test.tar.gz /var --exclude /tmp/*

These var ones copy the test.tar.gz to the backup which ends up in var/tmp/ I want it in var/tmp, but I don't want the script tarring up itself.
Code:

ls /var/tmp
test.tar.gz

Code:

tar ztvf /var/tmp/test.tar.gz | ls /var/tmp
test.tar.gz

I'm missing something, but I can't see it.

Sefyir 03-01-2016 08:26 PM

Quote:

I don't want to backup the /var/tmp directory.
Does this work?

Code:

tar czvpf /var/tmp/test.tar.gz /var --exclude=/var/tmp

kb2tfa 03-01-2016 08:50 PM

Quote:

Originally Posted by Sefyir (Post 5508786)
Does this work?

Code:

tar czvpf /var/tmp/test.tar.gz /var --exclude=/var/tmp

No. I tried
Code:

tar czvpf /var/tmp/test.tar.gz /var --exclude='var/tmp'
tar czvpf /var/tmp/test.tar.gz /var --exclude=/var/tmp
 tar czvpf /var/tmp/test.tar.gz /var --exclude='/var/tmp/*'
tar czvpf /var/tmp/test.tar.gz /var --exclude=/var/tmp/*


tar czvpf /var/tmp/test.tar.gz /var --exclude='/tmp'
tar czvpf /var/tmp/test.tar.gz /var --exclude='tmp'
tar czvpf /var/tmp/test.tar.gz /var --exclude='/tmp/*'
tar czvpf /var/tmp/test.tar.gz /var --exclude='tmp/*'

They result in
Code:

tar ztvf /var/tmp/test.tar.gz | ls /var/tmp/
test.tar.gz


kb2tfa 03-01-2016 08:53 PM

tried to others. no go
Code:

tar czvpf /var/tmp/test.tar.gz /var --exclude=tmp/*
tar czvpf /var/tmp/test.tar.gz /var --exclude=tmp


Doug G 03-01-2016 10:10 PM

As a guess, move the --exclude so it's before the target directory in your command line.

kb2tfa 03-02-2016 06:23 AM

Quote:

Originally Posted by Doug G (Post 5508820)
As a guess, move the --exclude so it's before the target directory in your command line.

no that didn't work either. I'm going to find another way to approach this.

hydrurga 03-02-2016 07:01 AM

Quote:

Originally Posted by Sefyir (Post 5508786)
Does this work?

Code:

tar czvpf /var/tmp/test.tar.gz /var --exclude=/var/tmp

For info, works fine for me (Linux Mint 17.3).

kb2tfa 03-02-2016 08:38 AM

1 Attachment(s)
I attached a screenshot. This is on my laptop Ubuntu 14 LTS. The previous was my desktop Linux Mint 17 Cinnamon. It is still tarring itself. If it is working correctly the
Code:

tar ztvf /var/tmp/test.tar.gz | ls /var/tmp/
should be empty.

hydrurga 03-02-2016 08:51 AM

Perhaps you might find it more useful to do a:

Code:

tar ztvf /var/tmp/test.tar.gz | grep "var/tmp"
rather than unsuccessfully trying to pipe tar output through an ls command.

rknichols 03-02-2016 08:54 AM

Quote:

Originally Posted by kb2tfa (Post 5508996)
Code:

tar ztvf /var/tmp/test.tar.gz | ls /var/tmp/

What on Earth do you think that command line is doing? The ls command does not read its standard input, so the output from the tar command goes nowhere and all you see is what you would get from just running "ls /var/tmp/". Perhaps you want
Code:

tar ztvf /var/tmp/test.tar.gz | grep var/tmp
Note that I've left off the leading and trailing "/" in the grep pattern since you want a match whether or not tar had stored those in the archive.

EDIT: There's an echo in here!

hydrurga 03-02-2016 08:59 AM

Quote:

Originally Posted by rknichols (Post 5509000)
EDIT: There's an echo in here!

:-P

kb2tfa 03-02-2016 09:01 AM

Quote:

Originally Posted by rknichols (Post 5509000)
What on Earth do you think that command line is doing? The ls command does not read its standard input, so the output from the tar command goes nowhere and all you see is what you would get from just running "ls /var/tmp/". Perhaps you want
Code:

tar ztvf /var/tmp/test.tar.gz | grep var/tmp
Note that I've left off the leading and trailing "/" in the grep pattern since you want a match whether or not tar had stored those in the archive.

EDIT: There's an echo in here!

thank you. that actually works. I cp the file to my home dir and opened with ark, and the var/tmp is not there. I guess the ls command is not the command to use for checking that.

hydrurga 03-02-2016 09:10 AM

Quote:

Originally Posted by kb2tfa (Post 5509002)
thank you. that actually works. I cp the file to my home dir and opened with ark, and the var/tmp is not there. I guess the ls command is not the command to use for checking that.

Great. Can you mark the thread as "Solved" please?

kb2tfa 03-02-2016 09:11 AM

Quote:

Originally Posted by hydrurga (Post 5509007)
Great. Can you mark the thread as "Solved" please?

absolutely. thanks again.


All times are GMT -5. The time now is 07:44 AM.