[英]Linux: How to get files with a name that ends with a number greater than 10
I have some files that have names like this: 'abcdefg_y_zz.jpg' The 'abcdefg' is a sequence of digits, while the 'y' and 'zz' are letters. 我有一些文件名如下:'abcdefg_y_zz.jpg''abcdefg'是一个数字序列,而'y'和'zz'是字母。 I need to get all the files that have the sequence of digits ending with a number greater than 10. The files that have 'fg' greater that 10.
我需要获得所有数字序列以大于10的数字结尾的文件。“fg”大于10的文件。
Does anyone have an idea on how to do that in a bash script? 有没有人知道如何在bash脚本中执行此操作?
好的,从技术上讲,基于你的所有信息......
ls | grep '[0-9]{5}[1-9][0-9]_[[:alpha:]]_[[:alpha:]]{2}.jpg'
How about this? 这个怎么样? Just exclude ones which have 0 in position f.
只要排除位置f为0的那些。
ls -1 | grep -v "?????0?_?_??.jpg"
Update Since you want > 10 and not >= 10, you'll need to exclude 10 too. 更新因为你想要> 10而不是> = 10,你也需要排除10。 So do this:
这样做:
ls -1 | grep -v "?????0*_?_??.jpg" | grep -v "??????10_?_??.jpg"
with more scripting 更多脚本
#!/bin/bash
for i in seq FIRST INCREMENT LAST
do
cp abcde$i_y_zz.jpg /your_new_dir //or whatever you want to do with those files
done
so in your example line with seq will be 所以在您的示例中,seq将会是
for i in seq 11 1 100000000
If the filenames are orderly named this awk
solution works: 如果文件名按顺序命名,则此
awk
解决方案有效:
ls | awk 'BEGIN { FIELDWIDTHS = "5 2" } $2 > 10'
FIELDWIDTHS = "5 2"
means that $1
will refer to the first 5 characters and $2
the next 2. FIELDWIDTHS = "5 2"
表示$1
将引用前5个字符, $2
表示接下来的2个字符。 $2 > 10
matches when field 2 is greater than 10 and implicitly invokes the default code block, ie '{ print }'
$2 > 10
时, $2 > 10
匹配并隐式调用默认代码块,即'{ print }'
只需一个过程:
ls ?????+(1[1-9]|[2-9]?)_?_??.jpg
All the solutions provided so far are fine, but anybody who's had some experience with shell programming knows that parsing ls
is never a good idea and must be avoided . 到目前为止提供的所有解决方案都很好,但任何有shell编程经验的人都知道解析
ls
永远不是一个好主意,必须避免 。 This actually doesn't apply in this case, where we can assume that the names of the files follow a certain pattern, but it's a rule that should be remembered. 这实际上不适用于这种情况,我们可以假设文件的名称遵循某种模式,但这是一个应该被记住的规则。 More explanation here .
这里有更多解释。
What you want can be achieved much safer with GNU find
- assuming that you run the command in the directory where the files are, it would look something like this : 使用GNU
find
可以实现您想要的更安全 - 假设您在文件所在的目录中运行命令,它看起来像这样:
find . -regextype posix-egrep -regex '\./[0-9]{5}[1-9][0-9]_[[:alpha:]]_[[:alpha:]]{2}.jpg$'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.