In shell scripting, what does .[!.]* mean?

 ̄綄美尐妖づ 提交于 2019-12-19 08:00:06

问题


A command that prints a list of files and folders in the current directory along with their total sizes is du -sh *. That command alone doesn't, however, list hidden files or folders. I found a solution for a command that does correctly list the hidden files and folders along with the rest: du -sh .[!.]* *. Although it works perfectly, the solution was provided as-is, without any explanation.

What is the meaning of .[!.]*, exactly? How does it work?


回答1:


It's a globbing pattern that basically tells bash to find all files starting with a ., followed by any character but a .and containing any character after that.

See this page for a great explanation of bash globbing patterns.




回答2:


. - match a ., prefix of hidden file

[!.] - match any character, as long as it is not a ., see ref

* - any number of characters

so this pattern means match files starts with . but not ..




回答3:


.[!.]* the meaning is any file or directory name start with . but not following with ., so it will include all hidden files and directories under current directory but exclude parent directory.

Because this behaviour is decided by shell glob pattern. So you can use ls .[!.]* to see what actually get in your shell environment.

BTW, you can turn dotglob on in your shell to simplify your du command.

$ shopt -s dotglob
$ du -sh *
$ shopt -u dotglob

From bash manual

dotglob If set, bash includes filenames beginning with a `.' in the results of pathname expansion.



来源:https://stackoverflow.com/questions/41034115/in-shell-scripting-what-does-mean

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