4

I have a tar.gz archive that contains some executable files. Is there a way to automatically make them not executable when I extract the archive? I tried using the --mode flag but it apparently did nothing.

tar xzf archive.tar.gz --mode 'a-x'

I would like to set the permissions using tar itself if possible, because I don't know upfront what will be the name of the folder that tar generates. I'm OK with using GNU tar features.

1
  • 2
    Maybe you can write a script for use with --command that will do the actual file/directory creation using the data provided by tar. Might a bit too much effort comparing to just running find . -type f -exec chmod a-x {} + afterwards. As for "because I don't know upfront what will be the name of the folder that tar generates", you can always use --transform to change the path to what you want.
    – muru
    Commented Oct 28, 2023 at 5:33

2 Answers 2

4

If using star (included in schilytools, by @schily (deceased)) instead of GNU tar is an option, you could do:

star xf file.tar -find ! -type d -chmod a-x

Where a find-like interface can be used to manipulate the archive members.

With libarchive's bsdtar, and assuming the archive doesn't contain files of any type other than directory and regular and not hardlink and you don't care about restoring any of the files metadata, you could convert the archive to zip format (which doesn't support permissions) on the fly before extracting:

bsdtar --format zip -cf - @file.tar | bsdtar xvf -

That would create all files with default permissions, with the umask also applied. So with umask 022, you'd get directories with rwxr-xr-x permission and regular files with rw-r--r--.

With GNU tar, you can achieve something similar with:

tar -xf file.tar --to-command='
  set -o noclobber
  mkdir -p -- "$(dirname -- "$TAR_FILENAME")" &&
    cat > "$TAR_FILENAME"'

With the additional limitation that it won't create directories that don't have regular files under them and fails if some directory names end in newline characters.

1

You should be able to combine setting a umask along with using tar's --no-same-permissions argument, thereby preventing the executable bit from being set upon extraction.

2
  • 4
    Note that --no-same-permissions is the default for non-root users. That would also affect directories making them non-searchable, so (umask 111; tar xf file.tar) would only work if the archive didn't contain any directory. Commented Oct 28, 2023 at 7:39
  • 1
    Sadly, "not containing directories" also applies to the root directory. We can only use this umask trick if it's one of those tar archives that blow up in the current directory when you extract them.
    – hugomg
    Commented Oct 28, 2023 at 14:43

You must log in to answer this question.

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