击。删除所有文件和目录,但某些那些 [英] Bash. Delete all files and directories but certain ones
问题描述
我正在写需要抹掉一切从目录中,除了两个目录,mysql和温度的脚本。
I'm writing a script that needs to erase everything from a directory except two directories, mysql and temp.
我试过这样:
ls * | grep -v mysql | grep -v temp | xargs rm -rf
不过这也使一切有mysql在他们的名字,我不需要的文件。它也不会删除任何其他目录。
but this also keeps all the files that have mysql in their name, that i don't need. it also doesn't delete any other directories.
什么想法?
推荐答案
您可以尝试:
rm -rf !(mysql|init)
这是 POSIX定义:
Glob patterns can also contain pattern lists. A pattern list is a sequence
of one or more patterns separated by either | or &. ... The following list
describes valid sub-patterns.
...
!(pattern-list):
Matches any string that does not match the specified pattern-list.
...
注意:请花时间先测试一下吧!无论是创建一些测试文件夹,或者干脆回声
参数替换,如正式通过@mnagel注意的是:
Note: Please, take time to test it first! Either create some test folder, or simply echo
the parameter substitution, as duly noted by @mnagel:
echo !(mysql|init)
<一个href=\"http://stackoverflow.com/questions/216995/how-can-i-use-inverse-or-negative-wildcards-when-pattern-matching-in-a-unix-linu\">Adding有用的信息的:如果匹配不是的有效的,你可以启用/使用禁用:
Adding useful information: if the matching is not active, you may to enable/disable it by using:
shopt extglob # shows extglob status
shopt -s extglob # enables extglob
shopt -u extglob # disables extglob
这篇关于击。删除所有文件和目录,但某些那些的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!