LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 03-01-2016, 08:17 PM   #1
kb2tfa
Member
 
Registered: Oct 2006
Location: New Jersey
Distribution: CentOS
Posts: 175

Rep: Reputation: 18
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.
 
Old 03-01-2016, 08:26 PM   #2
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
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
 
Old 03-01-2016, 08:50 PM   #3
kb2tfa
Member
 
Registered: Oct 2006
Location: New Jersey
Distribution: CentOS
Posts: 175

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by Sefyir View Post
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
 
Old 03-01-2016, 08:53 PM   #4
kb2tfa
Member
 
Registered: Oct 2006
Location: New Jersey
Distribution: CentOS
Posts: 175

Original Poster
Rep: Reputation: 18
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
 
Old 03-01-2016, 10:10 PM   #5
Doug G
Member
 
Registered: Jul 2013
Posts: 749

Rep: Reputation: Disabled
As a guess, move the --exclude so it's before the target directory in your command line.
 
Old 03-02-2016, 06:23 AM   #6
kb2tfa
Member
 
Registered: Oct 2006
Location: New Jersey
Distribution: CentOS
Posts: 175

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by Doug G View Post
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.
 
Old 03-02-2016, 07:01 AM   #7
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by Sefyir View Post
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).
 
Old 03-02-2016, 08:38 AM   #8
kb2tfa
Member
 
Registered: Oct 2006
Location: New Jersey
Distribution: CentOS
Posts: 175

Original Poster
Rep: Reputation: 18
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.
Attached Thumbnails
Click image for larger version

Name:	tar.png
Views:	40
Size:	143.0 KB
ID:	21020  
 
Old 03-02-2016, 08:51 AM   #9
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
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.
 
Old 03-02-2016, 08:54 AM   #10
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,770

Rep: Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210
Quote:
Originally Posted by kb2tfa View Post
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!
 
Old 03-02-2016, 08:59 AM   #11
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by rknichols View Post
EDIT: There's an echo in here!
:-P
 
Old 03-02-2016, 09:01 AM   #12
kb2tfa
Member
 
Registered: Oct 2006
Location: New Jersey
Distribution: CentOS
Posts: 175

Original Poster
Rep: Reputation: 18
Thumbs up

Quote:
Originally Posted by rknichols View Post
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.
 
Old 03-02-2016, 09:10 AM   #13
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by kb2tfa View Post
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?
 
Old 03-02-2016, 09:11 AM   #14
kb2tfa
Member
 
Registered: Oct 2006
Location: New Jersey
Distribution: CentOS
Posts: 175

Original Poster
Rep: Reputation: 18
Quote:
Originally Posted by hydrurga View Post
Great. Can you mark the thread as "Solved" please?
absolutely. thanks again.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Excluding executables in a sub-directory from tar backups sorvad Linux - General 5 07-29-2010 06:04 AM
[SOLVED] tar excluding some files infraquest Linux - Newbie 7 06-11-2010 06:35 AM
Excluding directories from tar? LocoMojo Linux - Software 6 01-24-2006 01:01 AM
tar, excluding directories recursively towlie Linux - Newbie 3 06-17-2004 12:32 PM
tar and excluding files murshed Linux - Newbie 7 03-15-2003 02:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:51 AM.

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