0

I'm trying to use tar(1) to create an archive of files newer than a specific file (fileA). However, when I use find(1) to obtain the list of files to pass to tar, some files are listed multiple times:

$ touch fileA
$ mkdir test
$ touch test/{fileB,fileC}
$ tar -c -v $(find test -newer fileA) > test.tar
test/
test/fileC
test/fileB
test/fileC
test/fileB

Using xargs(1) to pass the list of files to tar results in similar behavior:

$ find test -newer fileA | xargs tar -c -v > test.tar
test/
test/fileC
test/fileB
test/fileC
test/fileB

Using sort(1) and uniq(1) to remove duplicates doesn't work either:

$ find test -newer fileA | sort | uniq | xargs tar -c -v > test.tar
test/
test/fileC
test/fileB
test/fileB
test/fileC

Is there a way for tar to only include each file newer than fileA once?

Edit: I'm specifically looking for a solution that doesn't involve GNU extensions to tar (for example, which would work with suckless tar).

2 Answers 2

1
find test -newer fileA

finds the test directory as well as the individual files therein, so tar adds test (and all its contents), then test/fileB and test/fileC.

Tighten your find to avoid this:

tar -c -v $(find test -type f -newer fileA) > test.tar

Note that using command substitution in this way can cause issues e.g. with filenames containing spaces or wildcards; to avoid that, use

find test -type f -newer fileA -print0 | tar -c -v --null -T- -f - > test.tar

(with GNU find and tar), or

find test -type f -newer fileA -exec tar cvf - {} + > test.tar

(assuming you don’t have too many files to archive).

0
0

You can try something like:

tar --newer=fileA cvf test.tar test
3
  • That command gives tar: Substituting -9223372036854775807 for unknown date format 'fileA'. I guess that fileA modification time could be extracted and passed to --newer option to GNU tar, but I'm looking for a solution that doesn't involve GNU extensions to tar. I'll update the question to reflect that. Jul 6, 2022 at 14:35
  • 1
    @Vilinkameni, tar is not a standard command, all implementations are different and mostly incompatible between each other. Jul 6, 2022 at 15:01
  • I never said it is, just that I want to avoid options specific to GNU tar. I updated my question to reflect that. The options supported by suckless tar are a subset of the options of GNU tar, however, and I suspect also a subset of some other variants of tar. Jul 6, 2022 at 15:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .