As others have identified, the problem with your command is that it includes directories, and tar archives them recursively. If a directory has been modified recently, all the files in it and its subdirectories get included, whether they have been modified or not.
If you don't care to back up directory metadata, then just tell find
not to print directory names. It isn't enough to omit the root: the same thing can happen with subdirectories too.
find "$SOURCEDIR" -mtime -1 ! -type d -print | xargs -r tar -rcf "$ARCHIVE"
Using xargs fails with file names containing spaces and some other special characters. This is easy to fix: use -exec
instead of xargs
.
find "$SOURCEDIR" -mtime -1 ! -type d -exec tar -rcf "$ARCHIVE" {} +
If you want to back up directory metadata, let find
print everything and instead tell tar
not to recurse into subdirectories. Since find
is doing the recursion, tar
doesn't need to.
find "$SOURCEDIR" -mtime -1 -exec tar -rcf "$ARCHIVE" --no-recursion {} +
With this approach, you can avoid the use of tar -rc
and instead solve the problem of repeated tar invocations by first creating an archive with only the root directory, and then appending to it in batches. (Why the root directory? Because GNU tar is afraid of creating an empty archive.)
tar -cf "$ARCHIVE" --no-recursion "$SOURCEDIR"
find "$SOURCEDIR" -mindepth 1 -mtime -1 -exec tar -rf "$ARCHIVE" --no-recursion {} +
You are not specifying an archive in your statements.
It should look something like:
tar -cvf tar2/tar1.tar tar1/
This places the tarball tar1.tar
inside the directory tar2/
.
Before:
tree tar*
tar1
├── a.txt
└── b.txt
tar2
├── a.txt
└── b.txt
0 directories, 4 files
After:
tar -cvf tar2/tar1.tar tar1/
tar1/
tar1/a.txt
tar1/b.txt
tree tar*
tar1
├── a.txt
└── b.txt
tar2
├── a.txt
├── b.txt
└── tar1.tar
0 directories, 5 files
Environment:
Distributor ID: Debian
Description: Debian GNU/Linux 8.6 (jessie)
Release: 8.6
Codename: jessie
Best Answer
As the message says, you can't combine
c
andr
; the former means "create an archive", the latter "append to an archive", so they can't be used simultaneously.You can simply do