简体   繁体   English

如何排除 Linux 中的字符

[英]How do I exclude a character in Linux

Write a wildcard to match all files (does not matter the files are in which directory, just ask for the wildcard) named in the following rule: starts with a string “image”, immediately followed by a one-digit number (in the range of 0-9), then a non-digit char plus anything else, and ends with either “.jpg” or “.png”.写一个通配符来匹配所有文件(不管文件在哪个目录,只要要求通配符)命名在以下规则中:以字符串“image”开头,紧跟一个数字(在范围内) 0-9),然后是一个非数字字符加上其他任何字符,并以“.jpg”“.png”. For example, image7.jpg and image0abc.png should be matched by your wildcard while image2.txt or image11.png should not.例如, image7.jpg和 image0abc.png 应该与您的通配符匹配,而image2.txtimage11.png不应该。

My folder contained these files imag2gh.jpeg image11.png image1agb.jpg image1.png image2gh.jpg image2.txt image5.png image70.jpg image7bn.jpg Screenshot.png我的文件夹包含这些文件imag2gh.jpeg image11.png image1agb.jpg image1.png image2gh.jpg image2.txt image5.png image70.jpg image7bn.jpg Screenshot.png

If my command work it should only display image1agb.jpg image1.png image2gh.jpg image5.png image70.jpg image7bn.jpg如果我的命令有效,它应该只显示image1agb.jpg image1.png image2gh.jpg image5.png image70.jpg image7bn.jpg

This is the command I used (ls -ad image[0-9][^0-9]*{.jpg,.png}) but I'm only getting this image1agb.jpg image2gh.jpg image7bn.jpg so I'm missing (image1.png image5.png) Kali Terminal and what I did这是我使用的命令(ls -ad image[0-9][^0-9]*{.jpg,.png})但我只得到这个image1agb.jpg image2gh.jpg image7bn.jpg所以我'米失踪(image1.png image5.png)卡利终端和我做了什么

ls -ad image[0-9][.0-9]*{,jpg..png}

Info信息

Character ranges like [0-9] are usually seen in RegEx statements and such.像 [0-9] 这样的字符范围通常出现在 RegEx 语句等中。 They won't work as shell globs (wildcards) like that.它们不会像 shell 球体(通配符)那样工作。

Possible solution可能的解决方案

  1. Pipe output of command ls -a1 to standard input of the grep command (which does support RegEx). Pipe output 命令ls -a1 -a1grep命令的标准输入(支持 RegEx)。
  2. Use a RegEx statement to make grep filter filenames.使用 RegEx 语句使grep过滤文件名。
ls -a1|grep "image"'[[:digit:]]\+[[:alpha:]]*\.\(png\|jpg\)'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM