如何使用*通配符在bash,但不包括父目录(..)? [英] How to use the .* wildcard in bash but exclude the parent directory (..)?
问题描述
有常,我想在一个目录下的所有文件(包括隐藏文件)执行命令倍。当我尝试使用
There are often times that I want to execute a command on all files (including hidden files) in a directory. When I try using
chmod g+w * .*
它改变在父目录上的所有我想要的文件的权限(目录)和所有文件(我想独自离开)。
it changes the permissions on all the files I want (in the directory) and all the files in the parent directory (that I want left alone).
有没有做正确的事还是我需要开始使用找到一个通配符?
Is there a wildcard that does the right thing or do I need to start using find?
推荐答案
您将需要两个glob模式覆盖所有潜在的点文件: [^] *
和 .. *?
。
You will need two glob patterns to cover all the potential "dot files": .[^.]*
and ..?*
.
第一比赛用两个或多个字符,其中的第一个字符是一个点和第二个字符不是一个点的所有目录条目。第二拿起条目有与 ..
启动三个或更多字符(不包括 ..
,因为它只有两个字符,并开始与。
,但包括像(不太可能)项 ..富
)。
The first matches all directory entries with two or more characters where the first character is a dot and the second character is not a dot. The second picks up entries with three or more characters that start with ..
(this excludes ..
because it only has two characters and starts with a .
, but includes (unlikely) entries like ..foo
).
chmod g+w .[^.]* ..?*
这在大多数的所有炮弹运作良好,是适合的脚本。
This should work well in most all shells and is suitable for scripts.
对于经常交互使用,图案可能过于很难记住。对于这些情况下,你的shell可能有一个更方便的方式来跳过。
和 ..
。
的zsh 的始终不包括。
和 ..
从像模式。 *
。
随着的庆典的,你必须使用GLOBIGNORE shell变量。
For regular interactive use, the patterns may be too difficult to remember. For those cases, your shell might have a more convenient way to skip .
and ..
.
zsh always excludes .
and ..
from patterns like .*
.
With bash, you have to use the GLOBIGNORE shell variable.
# bash
GLOBIGNORE=.:..
echo .*
您可能会考虑在你的庆典的自定义文件(如的.bash_profile
/ .bash_login文件一个设置GLOBIGNORE code>或
的.bashrc
)。
但请注意,变得习惯于这种定制,如果你经常使用等环境。
如果你运行像搭配chmod G + w的命令。*
在缺少您的自定义的环境中,那么你会意外地结束了包括。
和 ..
在你的命令。
You might consider setting GLOBIGNORE in one of your bash customization files (e.g. .bash_profile
/.bash_login
or .bashrc
).
Beware, however, becoming accustomed to this customization if you often use other environments.
If you run a command like chmod g+w .*
in an environment that is missing your customization, then you will unexpectedly end up including .
and ..
in your command.
此外,您可以配置炮弹,包括在图案点文件不以一个明确的点开始(例如 *
)。
Additionally, you can configure the shells to include "dot files" in patterns that do not start with an explicit dot (e.g. *
).
# zsh
setopt glob_dots
# bash
shopt -s dotglob
# show all files, even "dot files"
echo *
这篇关于如何使用*通配符在bash,但不包括父目录(..)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!