根据文件名删除一些文件

时间:2019-04-24 15:44:46

标签: bash

我正在使用macOS,创建一个Bash脚本。

我在一些/path/to/inc/文件夹中有这些文件:

./lorem-ipsum.php
./dolor-sit.amet.php
./config-prod.php
./enqueue.php
./config.php
./setup.php
./config-uat.php
./config-some-other-thing.php

现在,我想获取名称为config*.php的文件,并删除它们 ,将其命名为config-prod.php,因此该文件夹结束看起来像这样:

./lorem-ipsum.php
./dolor-sit.amet.php #Still here
./config-prod.php
./enqueue.php
./config.php #Removed
./setup.php
./config-uat.php #Removed
./config-some-other-thing.php #Removed

有什么方法可以使用find或类似的方法吗?

2 个答案:

答案 0 :(得分:6)

您可以使用extended globs

$ shopt -s extglob
$ echo config!(-prod).php
config-some-other-thing.php config-uat.php config.php

并使用

删除文件
rm config!(-prod).php

!(pattern)匹配除 pattern 之外的所有内容。

答案 1 :(得分:-2)

我在linux上进行过测试:

find . ! -name 'config-prod.php' -type f -exec rm -f {} \;
相关问题