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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:35:17  来源:igfitidea点击:

Shell Bash check if a filename has a string on it

bashshell

提问by Otorrinolaringologista -man

I'm having problems creating an ifstatement 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_okto 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/

请看:http: //www.shellcheck.net/

回答by hek2mgl

Use findfor that:

用途find为:

find directory1 -maxdepth 1 -name '*1_ok*' -exec cp -v {} directory2 \;

The advantage of using findover the glob solution posted by Faibbusis that it can deal with an unlimited number of files which contain 1_okwere the glob solution will lead to an argument list too longerror when calling cpwith too many arguments.

使用Faibbus 发布findglob 解决方案的优点是它可以处理无限数量的文件,其中包含1_okglob 解决方案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 extglobmatching in bashwith the below pattern,

使用与以下模式extglob匹配bash

+(pattern-list) Matches one or more occurrences of the given patterns.

+(pattern-list) 匹配一个或多个给定模式。

First enable extglobby

首先使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