I’m so baffled I had to ask – why this behaviour?

cd /var/www/html
tar czf ~/package.tgz admin/* api/* mobile/*

I do this, and the resulting package doesn’t include a couple of hidden files – api/.htaccess and admin/.htaccess. However…

cd /var/www/html
tar czf ~/package.tgz *

This time the hidden .htaccess files are there.

Does anybody have enlightenment to offer as to why?

  • lemonskate
    link
    fedilink
    arrow-up
    1
    ·
    9 months ago

    Sure. It’s important to first realize that the * character is not read by tar, but by your shell which first expands it into a list of files/directories that are then passed into tar to process. In the first command you are asking your shell to expand all (implicitly non-hidden) files in the listed directories. In the second you are doing the same, but one level up.

    What happens then is that when tar is given a directory as an input to a create command, it will include everything in that directory hidden files included. This is why the second command worked as you expected. In the first command you are providing tar with an explicit list of files which does not include those which are hidden due to how shell glob expansion works.

    You can do a set -x before running these commands and this will show you the exact command line after shell expansion before execution. That should give a clearer view of why you see different behavior.