tar command - skip symlinks

31,197

You could do this, to supply tar with a list of all files inside protTests except those which are symlinks:

find protTests -maxdepth 1 -mindepth 1 -not -type l -print0 |
  tar --null --files-from - -cvf protTests.tar

By the way, your existing command:

tar -cvf protTests.tar protTests/*

will not archive all files in protTests, it will only archive those whose names do not begin with . (those that are not hidden). The * glob operator skips files whose names begin with . by design. The command also has the problem that if protTests has lots of files (more than many thousand), then protTests/* can expand to too many arguments to fit on the command line.

A simpler command like this would have neither of those problems:

tar -cvf protTests.tar protTests
Share:
31,197

Related videos on Youtube

Gilles 'SO- stop being evil'
Author by

Gilles 'SO- stop being evil'

Updated on September 18, 2022

Comments

  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' almost 2 years

    I use the tar command as,

    tar -cvf protTests.tar protTests/*
    

    to tar all files inside the folder, protTests. But this is including the symbolic links inside the folder, which is not a desired one.

    Is there a command line option, that will leave out all symlinks?

  • schily
    schily almost 9 years
    Tar has no option --files-from, this is an option from gtar, so of you give such an advise, better mention that this is gtar specific. Find has no primary -not, this is another non-portable GNUism. In general, it is a bad idea to use a separate find call. This may give problems with funny characters in the filename and it definitely gives a low performance as both find and tar need to scan the filesystem. These problems do not apply to programs like star that use libfind and thus have the find code incorporated.
  • schily
    schily almost 9 years
    Let me add another general hint: When giving an advise that is specific to a vendor specific variant of a UNIX program, it is good practice to mention the vendor and that it is vendor specific. This can typically be done by using the official names of the software, e.g gtar instead of tar, vim instead of vi. Note that this is a UNIX related information platform, so in general examples should be aligned with the POSIX standard.
  • Celada
    Celada almost 9 years
    You're right, @schily, this is a GNU-specific solution. Your answer based on star is a good alternative and I have upvoted it for that reason. I also agree with the "funny characters" objection but since GNU tar does not have a -0 option it's a tradeoff. I do not agree with the performance objection, that's really of no consequence in this situation.
  • schily
    schily almost 9 years
    Star allows you to test both variants and I of course did run this test already long time ago when adding libfind to star. There is a noticable to significant performance win with the builtin find. Regarding -0, this is currently not in POSIX by intention. The reason is that if find would support it, many other programs would have to be changed as well and the general UNIX rule is not to output binary data from programs on stdout or stderr. Null bytes in the output make it binary...
  • Stéphane Chazelas
    Stéphane Chazelas almost 9 years
    @schily, both GNU tar and bsdtar have --files-from and --null which removes the problem with funny characters (if combined with find's -print0 or -exec printf '%s\0' {} +). But here, you'd probably want to add the --no-recursion option. Some pax implementations also have a -0 option.
  • Celada
    Celada almost 9 years
    @StéphaneChazelas thanks for --null, I somehow missed it. I'll add that. As for --no-recursion, my interpretation of the OP's request is that recursion is probably wanted (or else irrelevant if the OP expects no subdirectories).
  • schily
    schily almost 9 years
    Gtar is a real problem here as it introduced various options with incompatible names even though the related functionality was in use with other option names by other tar implementations before. Sometimes I am unhappy to see that the youngest tar implementation (bsdtar) copies the gtar options instead of being compatible to older tar implementations.
  • Stéphane Chazelas
    Stéphane Chazelas almost 9 years
    If you don't add --no-recursion, then you may end up archiving symlinks in subdirectories. I think the OP actually wants find protTests ! -type l -print0 | bsdtar --null --files-from=- --no-recursion -cf out.tar which would be the equivalent of @schily's star one. (or gtar, though I tend to prefer bsdtar/libarchive these days to be able to store as much of the file's metadata as possible).
  • schily
    schily almost 9 years
    From my information, star is the implementation of your choice if you like to archive as much meta data as possible. Note that bsdtar just reimplemented SCHILY extensions to the POSIX standard. BTW: star exists since 1982, there is no other free implementation that is available since more than 33 years. gtar started from PD-TAR/SUG-TAR (presented at USENIX in February 1987 and the Sun User Group meeting in December 1987 by John Gilmore). The name gtar was used since 1989 and the first gtar version (then maintained by a different person) was the first version that was in conflict with POSIX.
  • Celada
    Celada almost 9 years
    @schily I feel that this comment thread is not the place for advocacy for different versions of tar. Please take it to chat. It's already way too long and reading like a religious war. Unless the OP chimes in at this point I don't think it matters what version of tar is in use. We don't even know what kind of Unix they are using.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' almost 9 years
    zsh: command not found: star When you mention utilities that you know >99% of your readers won't have on your systems, explain where to get them. And you must disclose your affiliation when you mention your own product (whether you post the link or not, so you might as well post the link).
  • schily
    schily almost 9 years
    A typical Linux system these days misses a lot of important software after a default install and at the same time, a lot of useless software is installed. A Linux user should know how to install missing software and people who frequently use tar typically have star installed anyway.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' almost 9 years
    A vast majority of people who frequently use tar have never heard of pax, let alone star.
  • schily
    schily almost 9 years
    Well those people who don't know that they are using gtar on Linux when they call "tar" are probably not the people who care anyway. Star is the oldest free tar implementation and many features seen in various tar implementations have been taken from star, so why not talk about the original?