Assuming GNU tar
, you could extract the archive through a short bash
script:
tar -x -f archive.tar --to-command=./script.sh
... where script.sh
is the executable script
#!/bin/bash
if [ "$TAR_FILETYPE" = f ] &&
[[ $TAR_FILENAME == wordpress/* ]] &&
[[ $TAR_FILENAME != wordpress/*/* ]]
then
mkdir -p "${TAR_FILENAME%/*}" &&
cat >"$TAR_FILENAME" &&
chmod "$TAR_MODE" "$TAR_FILENAME" &&
chown "$TAR_UNAME:$TAR_GNAME" "$TAR_FILENAME" || true
else
cat >/dev/null
fi
The TAR_*
variables used here are set in the script's environment by GNU tar
, and the data of the file currently being extracted is arriving over standard input.
The tests used in the script is to make sure that
- The file we're extracting is a regular file, and
- The file is located in the
wordpress
directory and nowhere else.
To extract a file, we simply make sure that the file's directory exists, redirect the standard input to the correct filename, and then modify the file mode and ownerships so that the permissions and owners are correct.
You could obviously also give the script in-line on the tar
command line:
tar -x -f archive.tar --to-command='bash -c "
if [ \"\$TAR_FILETYPE\" = f ] &&
[[ \$TAR_FILENAME == wordpress/* ]] &&
[[ \$TAR_FILENAME != wordpress/*/* ]]
then
mkdir -p \"\${TAR_FILENAME%/*}\" &&
cat >\"\$TAR_FILENAME\" &&
chmod \"\$TAR_MODE\" \"\$TAR_FILENAME\" &&
chown \"\$TAR_UNAME:\$TAR_GNAME\" \"\$TAR_FILENAME\" || true
else
cat >/dev/null
fi"'
.
in their names within the archive (likefolder.suff
)?--wildcards --no-wildcards-match-slash
and they do not prevent subdirectories extraction at all--files-from
option not work for you? You'd list the contents of the tar file, then filter for only the wordpress folder, then exclude the sub-folders, then pass that file to tar.