Shell Bash 检查文件名上是否包含字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41506831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shell Bash check if a filename has a string on it
提问by Otorrinolaringologista -man
I'm having problems creating an if
statement to check the files in my directory for a certain string in their names.
我在创建if
语句以检查我的目录中的文件的名称中是否包含某个字符串时遇到问题。
For example, I have the following files in a certain directory:
例如,我在某个目录中有以下文件:
file_1_ok.txt
file_2_ok.txt
file_3_ok.txt
file_4_ok.txt
other_file_1_ok.py
other_file_2_ok.py
other_file_3_ok.py
other_file_4_ok.py
another_file_1_not_ok.sh
another_file_2_not_ok.sh
another_file_3_not_ok.sh
another_file_4_not_ok.sh
I want to copy all files that contain 1_ok
to another directory:
我想将包含的所有文件复制1_ok
到另一个目录:
#!/bin/bash
directory1=/FILES/user/directory1/
directory2=/FILES/user/directory2/
string="1_ok"
cd $directory
for every file in $directory1
do
if [$string = $file]; then
cp $file $directory2
fi
done
UPDATE:
更新:
The simpler answer was made by Faibbus, but refer to Inianif you want to remove or simply move files that don't have the specific string you want.
Faibbus 给出了更简单的答案,但如果您想删除或只是移动没有您想要的特定字符串的文件,请参考Inian。
The other answers are valid as well.
其他答案也有效。
回答by Faibbus
cp directory1/*1_ok* directory2/
回答by Cyrus
With your script I suggest:
使用您的脚本,我建议:
#!/bin/bash
source="/FILES/user/directory1"
target="/FILES/user/directory2"
regex="1_ok"
for file in "$source"/*; do
if [[ $file =~ $regex ]]; then
cp -v "$file" "$target"
fi
done
From help [[
:
来自help [[
:
When the
=~
operator is used, the string to the right of the operator is matched as a regular expression.
使用
=~
运算符时,运算符右侧的字符串作为正则表达式进行匹配。
Please take a look: http://www.shellcheck.net/
回答by hek2mgl
Use find
for that:
用途find
为:
find directory1 -maxdepth 1 -name '*1_ok*' -exec cp -v {} directory2 \;
The advantage of using find
over the glob solution posted by Faibbusis that it can deal with an unlimited number of files which contain 1_ok
were the glob solution will lead to an argument list too long
error when calling cp
with too many arguments.
使用Faibbus 发布find
的glob 解决方案的优点是它可以处理无限数量的文件,其中包含1_ok
glob 解决方案argument list too long
在调用cp
过多参数时会导致错误。
Conclusion: For interactive use with a limited number of input files the glob will be fine, for a shell script, which has to be stable, I would use find
.
结论:对于输入文件数量有限的交互式使用,glob 会很好,对于必须稳定的 shell 脚本,我会使用find
.
回答by Inian
Using extglob
matching in bash
with the below pattern,
使用与以下模式extglob
匹配bash
,
+(pattern-list) Matches one or more occurrences of the given patterns.
+(pattern-list) 匹配一个或多个给定模式。
First enable extglob
by
首先使extglob
通过
shopt -s extglob
cp -v directory1/+(*not_ok*) directory2/
An example,
一个例子,
$ ls *.sh
another_file_1_not_ok.sh another_file_3_not_ok.sh
another_file_2_not_ok.sh another_file_4_nnoot_ok.sh
$ shopt -s extglob
$ cp -v +(*not_ok*) somedir/
another_file_1_not_ok.sh -> somelib/another_file_1_not_ok.sh
another_file_2_not_ok.sh -> somelib/another_file_2_not_ok.sh
another_file_3_not_ok.sh -> somelib/another_file_3_not_ok.sh
To remove the files except the one containing this pattern, do
要删除包含此模式的文件以外的文件,请执行
$ rm -v !(*not_ok*) 2>/dev/null