简体   繁体   English

ls和正则表达式linux

[英]ls and regular expression linux

I have two directories: 我有两个目录:

  1. run.2016-02-25_01.
  2. run.2016-02-25_01.47.04

Both these directories are present under a common directory called gte . 这两个目录都位于名为gte的通用目录下。

I want a directory that ends without a dot character . 我想要一个没有点号结尾的目录. .

I am using the following command, however, I am not able to make it work: 我正在使用以下命令,但是,我无法使其工作:

ls run* | grep '.*\d+' 

The commands is not able to find anything. 命令无法找到任何东西。

The negated character set in shell globbing uses ! shell全局中的否定字符集使用! not ^ : 不是^

ls -d run*[!.]

(The ^ was at one time an archaic synonym for | .) The -d option lists directory names, not the contents of those directories. ^曾经是|的古老同义词。)- -d选项列出目录名称,而不是这些目录的内容。


Your attempt using: 您尝试使用:

ls run* | grep '.*\d+'

requires a PCRE-enabled grep and the PCRE regex option ( -P ), and you are looking for zero or more of any character followed by one or more digits, which isn't what you said you wanted. 需要启用PCRE的grep和PCRE regex选项( -P ),并且您正在寻找零个或多个任何字符,后接一个或多个数字,这不是您想要的。 You could use: 您可以使用:

ls -d run* | grep '[^.]$'

which doesn't require the PCRE regexes, but simply having the shell glob the right names is probably best. 不需要PCRE正则表达式,但是简单地使用正确的名称作为shell glob可能是最好的。

If you're worried that there might not be a name starting run and ending with something other than a dot, you should consider shopt -s nullglob , as mentioned in Anubhava 's answer . 如果你担心,有可能不是一个名字开始run ,并具有比其他点结束的东西,你应该考虑shopt -s nullglob ,正如上文Anubhava答案 However, note the discussion below between hek2mgl and myself about the potentially confusing behaviour of, in particular, the ls command in conjunction with shopt -s nullglob . 但是,请注意下面在hek2mgl和我自己之间有关ls命令和shopt -s nullglob的潜在混乱行为的讨论。 If you were using: 如果您使用的是:

for name in run*[!.]
do
    …
done

then shopt -s nullglob is perfect; 那么shopt -s nullglob是完美的; the loop iterates zero times when there's no match for the glob expression. 当glob表达式不匹配时,循环迭代零次。 It isn't so good when the glob expression is an argument to commands such as ls that provide a default behaviour in the absence of command line arguments. 如果glob表达式是ls命令的参数,而ls在没有命令行参数的情况下提供默认行为,那么效果就不好了。

You don't need grep . 您不需要grep Just use: 只需使用:

shopt -s nullglob
ls -d run*[0-9]

If your directories are not always ending with digits then use extglob : 如果您的目录并非总是以数字结尾,请使用extglob

shopt -s nullglob extglob
ls -d run*+([^.])

or to list all entries inside the run* directory ending without DOT: 或列出不带DOT的run*目录中的所有条目:

printf "%s\n" run*+([^.])/*

This works... 这有效...

ls|grep '.*[^.]$'

That is saying I want any amount of anything but I want the last character before the line ending to be anything except for a period. 就是说我想要任何数量的东西,但是我希望行尾之前的最后一个字符是除了句点之外的任何东西。

我会用find

find -regextype posix-awk -maxdepth 1 -type d -regex '-*[[:digit:]]+$'

To list the directories that don't end with a . 列出不以结尾的目录. .

ls -d run* |grep "[^.]$"

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

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