LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-01-2007, 08:14 AM   #1
lrt
Member
 
Registered: Sep 2007
Location: Chattanooga, TN
Distribution: Ubuntu
Posts: 84

Rep: Reputation: 15
tar does not skip empty directories


i'm using tar to create an archive from a mounted share. the archive should only backup files that are newer than "time"

tar --create --gzip --file=/path/to/gzip/file --newer=/path/to/timestamp/file /path/to/files/needing/backup/

this works great because only the files that are newer than the timestamp file are backed up. HOWEVER, since tar searches through all the directories in /path/to/files/needing/backup/ it also backs up empty directories. the files older than --newer get skipped, but that the directories that contain those files get backed up, therefore creating empty directories.

so my basic question is, how do i tell tar not to archive empty directories?

lrt
 
Old 10-01-2007, 09:18 AM   #2
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,884

Rep: Reputation: 351Reputation: 351Reputation: 351Reputation: 351
Tar doesn't provide an obvious means of skipping empty directories.

But, you could embed your tar script in a shell script. Let the shell detect empty directories in the search path and skip to the next until a directory with files is found, then apply the tar script to the files in the directory.
 
Old 10-01-2007, 09:51 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
A method can be using find in conjunction with the tar command. First create a file with a timestamp to compare
Code:
touch -t 200710011500 /path/to/reftime
this will create an empty file (named reftime) with timestamp 01-Oct-2007 15:00. Then suppose you want to look for file newer than the above timestamp, and tar/gzip them
Code:
find /path/to/source/dir -cnewer /path/to/reftime -print0 | xargs -0 --no-recursion -zcvf /path/to/archive.tar.gz
Here I used the short form for tar options. The -cnewer option to the find command looks for files whose canghe time is more recent than the one of /path/to/reftime file. However this will include empty directories if (and only if) a directory has a timestamp newer than reftime and the files inside it are older, but in general this is not a common situation. I hope this helps a little.
 
1 members found this post helpful.
Old 10-01-2007, 10:18 AM   #4
lrt
Member
 
Registered: Sep 2007
Location: Chattanooga, TN
Distribution: Ubuntu
Posts: 84

Original Poster
Rep: Reputation: 15
exactly what i was looking for. thank you colucix! (cool screename by the way).

lrt
 
Old 10-01-2007, 11:43 AM   #5
lrt
Member
 
Registered: Sep 2007
Location: Chattanooga, TN
Distribution: Ubuntu
Posts: 84

Original Poster
Rep: Reputation: 15
there doesn't seem to be a --no-recursion option for xargs
 
Old 10-01-2007, 11:50 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Uh oh... sorry, I mispelled the command (forgotten tar). --no-recursion is a tar option
Code:
find /path/to/source/dir -cnewer /path/to/reftime -print0 | xargs -0 tar --no-recursion -zcvf /path/to/archive.tar.gz
 
Old 10-01-2007, 11:51 AM   #7
lrt
Member
 
Registered: Sep 2007
Location: Chattanooga, TN
Distribution: Ubuntu
Posts: 84

Original Poster
Rep: Reputation: 15
works like a charm thanks again
 
Old 10-01-2007, 12:23 PM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You're welcome!
 
  


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
NEVERMIND: removing non empty directories AmphetaminePhreak Linux - Newbie 1 07-28-2006 02:49 AM
How do I have tar skip files already on destination HD? Bob Ross Red Hat 2 02-04-2004 12:43 PM
Removing non-empty directories brunnopessoa Linux - Newbie 8 11-13-2003 07:56 PM
how do I remove all empty directories? noisybastard Linux - General 2 08-10-2003 11:47 PM
finding empty directories... PokerFace Linux - General 2 10-16-2002 08:23 AM


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