LinuxQuestions.org
Review your favorite Linux distribution.
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 11-16-2014, 08:29 PM   #1
ASTRAPI
Member
 
Registered: Feb 2007
Posts: 210

Rep: Reputation: 16
Question Exclude a folder on tar


Hi
I want to backup the contents of the folder:
Code:
/home/username/public_html/
Inside public_html is a folder named uploads that i want to exclude from tar (completely exclude this folder and all files in it and all subfolders in it)
So i cd here:
Code:
cd /home/username
Then i found from Google many recommendations like:
Code:
tar -cpvzf /home/username/sitefiles.tgz  --exclude=/home/username/public_html/uploads public_html
or
Code:
tar -cpvzf /home/username/sitefiles.tgz public_html --exclude=/home/username/public_html/uploads
or
Code:
tar --exclude=/home/username/public_html/uploads -cpvzf /home/username/sitefiles.tgz public_html
or
Code:
tar -cpvzf /home/username/sitefiles.tgz public_html --exclude=./home/username/public_html/uploads
or
Code:
tar -cpvzf /home/username/sitefiles.tgz public_html --exclude="/home/username/public_html/uploads"
No way to get this folder excluded as i can see from ssh when i run the commands....
Any ideas?

Tested on Centos and Ubuntu 64bit servers
 
Old 11-17-2014, 12:05 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
You do not have ssh listed anywhere in the commands above. What part is this playing in the issuing of the command?

As far as the tar command goes, you need to remember the man page advises it is a pattern and once in your home directory, there will not be a full path to your directory available.
Try:
Code:
tar -cpvzf /home/username/sitefiles.tgz  --exclude=uploads public_html
 
Old 11-17-2014, 12:27 AM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,079
Blog Entries: 1

Rep: Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147
The key idea from your OP is...

Quote:
Originally Posted by ASTRAPI View Post
Inside public_html is a folder named uploads that i want to exclude from tar (completely exclude this folder and all files in it and all subfolders in it)
And the key idea from man tar is...

Quote:
--exclude FILE
exclude file FILE
Hint: Emphasis on FILE is in man tar (i.e. NOT directories).

It also happens that the path to the files to exclude are matched as character strings, not as absolute filesystem paths, so they must be expressed in the same context as the included files. Because you are including public_html as a relative path, the exclude path must also be relative.

So this will not work...

Code:
tar -cpvzf /home/username/sitefiles.tgz public_html --exclude=/home/username/public_html/uploads
Nor this...

Code:
tar -cpvzf /home/username/sitefiles.tgz public_html --exclude=./home/username/public_html/uploads
(Which shows that you probably need to study up on GNU/Linux, Unix file path expresions in general).

Nor this...

Code:
tar -cpvzf /home/username/sitefiles.tgz public_html --exclude=public_html/uploads

Nor this...

tar -cpvzf /home/username/sitefiles.tgz public_html --exclude=public_html/uploads/
But something like this should because paths are relative and it lists all FILEs (wildcard expansion is done by the shell)

Code:
tar -cpvzf /home/username/sitefiles.tgz public_html --exclude=public_html/uploads/*
 
1 members found this post helpful.
Old 11-17-2014, 09:11 AM   #4
ASTRAPI
Member
 
Registered: Feb 2007
Posts: 210

Original Poster
Rep: Reputation: 16
This one works for me:

Code:
tar -cpvzf /home/username/sitefiles.tgz public_html --exclude=public_html/uploads/*
Thanks !
 
1 members found this post helpful.
Old 11-17-2014, 09:37 AM   #5
ASTRAPI
Member
 
Registered: Feb 2007
Posts: 210

Original Poster
Rep: Reputation: 16
How can i do the opposite now?

Exclude all fies and folders and tar only the /public_html/uploads files and subfolders there?

Thanks
 
Old 11-17-2014, 09:44 AM   #6
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,184

Rep: Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584
Just explicitly tell it what to tar

Code:
tar -cpvzf /home/username/sitefiles.tgz public_html/uploads
If you want to add those files to an existing archive,. you can just add '-r' or '--append' (same thing) to your options list:

Code:
tar -cpvzrf /home/username/sitefiles.tgz public_html/uploads

Last edited by szboardstretcher; 11-17-2014 at 09:46 AM.
 
Old 11-17-2014, 10:54 AM   #7
ASTRAPI
Member
 
Registered: Feb 2007
Posts: 210

Original Poster
Rep: Reputation: 16
Thanks !!!
 
  


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
[SOLVED] exclude nested folder from tar threezerous Linux - Newbie 4 11-01-2014 02:20 PM
TAR Exclude everything in folder except this folder rationalthinker1 Linux - Newbie 5 04-30-2012 08:20 PM
[SOLVED] tar with --exclude and/or --exclude-from vbekker Linux - Newbie 6 12-09-2010 02:51 AM
TAR: Unpack file, exclude a folder Smeerbalg Linux - Software 1 01-29-2006 07:59 AM
tar --exclude --exclude-from cefn Linux - Software 4 10-11-2005 07:31 PM


All times are GMT -5. The time now is 02:24 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration