Bash的shopt命令学习


之前稍微了解过shopt命令,但用的较少,所以印象不深,这次无意中碰到了,刚好学习学习。

怎样遍历移除项目中的所有 .pyc 文件 ?

如果你使用的是linux或者mac,在终端执行这样的操作:

find /tmp -name "*.pyc" | xargs rm -rf

如果提示Permission denied,则使用:

sudo find /tmp -name "*.pyc" | xargs rm -rf

将上面的/tmp替换成工程目录即可。这个命令会遍历删除工程目录(含子目录)下的pyc文件。

==

rm **/*.pyc

补充说明:在bash下要在~/.bashrc中添加shopt -s globstar

==

在打包时忽略 .pyc 文件或许是个更方便的办法。
tar和zip都可以加上 –exclude=*.pyc 参数来排除 pyc 文件

===

find -iname "*.pyc" -exec rm -f {} ;

===

find . -type f -name "*.py[co]" -delete

===

上面的几个find命令的示例其实我都知道,这里感兴趣的主要在于:
rm **/*.pyc
这个方法,补充说明有“在bash下要在~/.bashrc中添加shopt -s globstar”

刚好前段时间看到过一篇文章:

需求:
一个日志目录,里头存着很多 00, 01, 02 … 31 的日期目录,如何列示出00到12号之间的所有目录?也就是说,如何在文件名上面,使用正则?

解法:

# shopt -s extglob
# ls -al +(0[0-9]|1[0-3])

说明:
Ø 平时shell的通配符,只是通配语义,不是正则语义
Ø 加上这个 extglob 之后,才能是正则语义
Ø 语法格式是 +(正则)
—————————-

去Ubuntu上看看(Bash的版本为:GNU bash, version 4.3.11(1)-release (i686-pc-linux-gnu)):

# shopt -s
autocd                   execfail                 lastpipe
cdable_vars              expand_aliases           lithist
cdspell                  extdebug                 login_shell
checkhash                extglob                  mailwarn
checkjobs                extquote                 nocaseglob
checkwinsize             failglob                 nocasematch
cmdhist                  force_fignore            no_empty_cmd_completion
compat31                 globasciiranges          nullglob
compat32                 globstar                 progcomp
compat40                 gnu_errfmt               promptvars
compat41                 histappend               restricted_shell
compat42                 histreedit               shift_verbose
complete_fullquote       histverify               sourcepath
direxpand                hostcomplete             xpg_echo
dirspell                 huponexit
dotglob                  interactive_comments
搜索关键字
找到的有效链接{extglob}:

GNU官方说明:Bash Reference Manual: The Shopt Builtin

另一个shopt的选项{globstar}:

better, simpler searching and scripting with bash globstar « ShellRunner

Globstar is a feature not typically enabled, but present in bash 4+, and it allows you to do more easily select files in bash, using a double star **.

For example, if you want to every .text file, in all subdirectories, you no longer even need to use the find command.

ls **/*.text

The ** here will traverse any number of directories, not just the current directory. Here’s another very useful, easy to remember example. Ever want to find a line of text somewhere in a huge pile of files, but you know some part of the file name? You can do a recursive grep to easily find it, without some abstruse find command.

grep -r needle **/*haystack*

To begin using globstar, you need to enable it. It should be the default, if you ask me, but enabling is simple. From the terminal, type:

shopt -s globstar

You’ll probably want to add that line to your ~/.bashrc file, too, so it’s enabled every time you open a terminal or login.

在我的VPS上globstar默认是关闭的:

# shopt globstar
globstar off

而,extglob默认却是开启的:

# shopt extglob
extglob on

在Stackoverflow上有一个问题对我而言很有启发意义:
http://stackoverflow.com/questions/17191622/why-would-i-not-leave-extglob-enabled-in-bash

提问者给出了一个思考:

All the answers that used ‘shopt -s extglob’ also mentioned ‘shopt -u extglob’ to turn it off. Why would I want to turn something so useful off? Indeed why isn’t it on by default? Presumably it has the potential for giving some nasty surprises. What are they?
所有的答案都告诉我使用“shopt -s extglob”打开extglob,使用“shopt -u extglob”关掉extglob,但是为什么我还是想将这么有用的一个选项关掉呢?为什么这么有用的选项在默认状态下没有被开启?难道是因为这可能会导致一些糟糕的意外?如果是的话,会导致哪些问题呢?


回答简洁明了:

No nasty surprises — default-off behavior is only there for compatibility with traditional, standards-compliant pattern syntax.
默认状态下没有开启不代表这是不好的特性,仅仅只是兼容性的需要而已。

查看shopt的选项都是些什么状态,直接输入:shopt然后回车即可

# shopt
autocd          off
cdable_vars     off
cdspell         off
checkhash       off
checkjobs       off
checkwinsize    on
cmdhist         on
compat31        off
compat32        off
compat40        off
compat41        off
compat42        off
complete_fullquote      on
direxpand       off
dirspell        off
dotglob         off
execfail        off
expand_aliases  on
extdebug        off
extglob         on
extquote        on
failglob        off
force_fignore   on
globstar        off
globasciiranges off
gnu_errfmt      off
histappend      on
histreedit      off
histverify      off
hostcomplete    off
huponexit       off
interactive_comments    on
lastpipe        off
lithist         off
login_shell     on
mailwarn        off
no_empty_cmd_completion off
nocaseglob      off
nocasematch     off
nullglob        off
progcomp        on
promptvars      on
restricted_shell        off
shift_verbose   off
sourcepath      on
xpg_echo        off

 

参考链接:

怎样遍历移除项目中的所有 .pyc 文件

,

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注