archiving (ubuntu tar) hidden directories

五迷三道 提交于 2019-12-03 15:30:16

问题


tar on a directory mydir will archive hidden files and hidden subdirectories, but tar from within mydir with a * wildcard will not. Is this a longstanding and known inconsistency or bug?

Edit (additional information): tar from within mydir with a wildcard will not "see" nor archive hidden files and hidden subdirectories in the immediate directory. However, in the non-hidden subdirectories of mydir hidden files and hidden subdirectories will be archived.


回答1:


With wildcard it will not work. You have to specify . (current directory) if you mean full directory including hidden files. You can do

tar -cvpzf test.tgz .



回答2:


You can use:

tar -cvpzf test.tgz * .??*

But, this works only for hidden files with names > 2 (to prevent '.' and '..')




回答3:


The answer is that the * wildcard is handled by the shell and it just does not expand to things that start with a dot. The other wildcard ? also does not expand to things that start with a dot. Thanks to Keith for pointing out it is the shell that does the expansion and so it has nothing to do with tar.

If you use shopt -s dotglob then expansion will include things like .filename. Thanks to Andy.

Use shopt -u dotglob to turn it off.

Switching the dotglob option does not change ls itself. Rather it just changes expansion behaviour as exhibited in something like ls *.

Edit: My recommendations are in a comment below.




回答4:


The shell expands the wildcards so tar doesn't even see it. You have to add them explicitly if you want to do that. (.*). However, it's most common to tar a single directory so that when you untar it all goes to the same place.




回答5:


You can compress all the files / folders in your current directory (including .hidden) by using:

tar -zcvf compressed.tgz `ls -A -1`

The last argument are the folders you want to compress. If you pass it ls -A -1 , you are passing it all folders in your current directory but . and .. . When it comes to subdirectories, .hidden files are already included in the compression by default.




回答6:


shopt -s dotglob

this will make the



来源:https://stackoverflow.com/questions/4572973/archiving-ubuntu-tar-hidden-directories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!