LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   tar does not skip empty directories (https://www.linuxquestions.org/questions/linux-software-2/tar-does-not-skip-empty-directories-588606/)

lrt 10-01-2007 08:14 AM

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

bigrigdriver 10-01-2007 09:18 AM

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.

colucix 10-01-2007 09:51 AM

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.

lrt 10-01-2007 10:18 AM

exactly what i was looking for. thank you colucix! (cool screename by the way).

lrt

lrt 10-01-2007 11:43 AM

there doesn't seem to be a --no-recursion option for xargs

colucix 10-01-2007 11:50 AM

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

lrt 10-01-2007 11:51 AM

works like a charm thanks again

colucix 10-01-2007 12:23 PM

You're welcome! :)


All times are GMT -5. The time now is 04:58 AM.