简体   繁体   English

Linux:如何获取名称以大于10的数字结尾的文件

[英]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'

Explanation 说明

  • 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大于$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.

相关问题 Linux如何在文件名中列出大于特定时间戳的文件? - Linux How to List Files greater than particular timestamp in file name? 如何在 linux 中查找大于大小的文件 - How to find files greater than a size in linux 查找文件名大于的文件 - find files that have number in file name greater than 如何通过ftp连接获取比Linux命令行(bash)中指定的文本字母数字大的文件列表? - How to get list of files alphanumerically greater than text specified in Linux command line (bash) via ftp connection? 查找名称中带有大于某个整数的整数的所有文件,最好在LINUX设置中 - Find all files with integers in the name that are greater than a certain value, preferably in a LINUX setting 如何在 Linux 中找到大于给定数字的列中的元素数? - How do I find the number of elements in a column greater than a given number in Linux? 获取创建日期大于某个日期 linux 的文件列表 - Get list of files whose creation date is greater than some date linux 如何在Linux上获取虚拟桌面名称/号码? - How to get virtual desktop name/number on Linux? 如何在 Linux 中找到大于特定数字的变量? - How can I find variable which greater than specific number in Linux? 如何在名称短于n个字符的Linux上查找文件? - How to find files on Linux with a name shorter than n characters?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM