2

Does the tar command have an option to skip/ignore tar-ing the already tar-ed files?

consider I had a file1.txt and it's tar-ed as file1.tar.gz , now I copied another copy of file1.txt and a lot of other files like file2.txt, file3.txt, etc

what I expect, when tar-ing all these files, don't tar file1.txt again; I'm not interested in excluding all *.tar.gz files since it doesn't telling tar command to don't pick file1.txt to tar.

What I need actually prevent file1.txt to pick by tar command to do tar it as file1.tar.gz again which that's exist before (right now its overwriting).

Note: I could write a script, but I want to make sure, tar command cannot do this.

5
  • No, you will have to exclude them from the file list you feed to tar, f.e.: tar cvf /storage/someFiles.tar $(find /sourceDir | grep -v '.tar'). Not sure the syntax is correct, but I trust you get the idea.
    – user680858
    May 27, 2017 at 9:35
  • The linux filesystem(s) doesn't have any notion of what you did to a file earlier, and how should the tar command know where to look for things you have tar'ed in an other session?
    – Soren A
    May 22, 2019 at 11:11
  • @SorenA it can be implemented as the same way as mv file1.txt file1.txt does for example. so when you do something like tar -czf file1.tar.gz file1.txt and again and again trying to do the same tar -czf file1.tar.gz file1.txt, I wanted tar to knows that there is file1.tar.gz already why it bother itself to tar it again. idea is coming from where I wanted to tar a very big file and every time it will try to tar and tar and tar again ---> May 22, 2019 at 11:18
  • <--- (yes, I need to run it within find as shown below answer in find command but without checking if there is its tar version and wanted tar manage it by itself for every files found ). May 22, 2019 at 11:18
  • What ever mv file1.txt file1.txt does, it do it within it's own invocation / session. What you wan't to do with tar are over multiple invocations.
    – Soren A
    May 22, 2019 at 11:23

2 Answers 2

1

I can achieve that using the following tar command:

tar -cf koko.tar --exclude="*.tar" kokotar

Another neat trick is to create a file with the patterns of file names or extension you wish to ignore then read that file from the tar command to ignore all such file with this:

tar -cf koko.tar --exclude-from <file | /path/to/exclusion/list/file> kokotar

or

tar -cf koko.tar -X <file | /path/to/exclusion/list/file> kokotar

Syntax format:

tar -cf <name_of_new_tar_file>.tar --exclude='pattern' <source>

tar -cf <name_of_new_tar_file>.tar --exclude-from /path/to/name/exclusion/list <source>
3
  • sorry, but seems this only exclude *. tar files only to don't re-tar, not prevent kokotar to don't tar as kokotar.tar while there is kokotar.tar May 27, 2017 at 17:30
  • Oh is that what you meant? May 27, 2017 at 17:32
  • sorry, but this doesn't answer my question as I explained. May 19, 2018 at 15:08
0

sounds tar currently has no possibility to skip files those that have a tar-ed version, so this is a way to skip those files:

find -type f -name '*.txt' -exec sh -c '
    [ -f "${1%.txt}.tar.gz" ] && echo "$1 skipping ..." || echo "$1 taring ..."
' _ {} \;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.