使用grep列出目录中的条目 [英] Listing entries in a directory using grep

查看:119
本文介绍了使用grep列出目录中的条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图列出名称中只包含大写字母的目录中的所有条目。

 #!/ bin / bash 
cd〜/ testfiles /
ls | grep -r *。*

由于grep默认只查找大写字母(对吧?) ,我只是通过testfiles下的目录递归搜索所有只包含大写字母的名字。



不幸的是,这是行不通的。



至于追加目录,我不确定为什么我需要这样做。有没有人知道我可以从哪里开始详细解释我可以用grep做什么?此外,如何解决我的问题?

解决方案

不,grep不仅考虑大写字母。

您的问题我有点不清楚,例如:




  • 从您使用-r选项你想递归搜索,但是你不这样说。为了简单起见,我假设你不需要;如果您需要递归,请考虑查看@ twm的答案。

  • 您只想查找大写字母(字母)。这是否意味着您不想接受任何其他(非字母)字符,但它们对文件名(例如数字或破折号,点等)有效直至有效。

  • 因为您不要说它不允许每行只有文件,我假设它是可以的(因此使用 ls -1 ) 。


天真的解决方案是:

  ls -1 | grep^ [[:upper:]] \ + $

即打印所有行只包含大写字母。例如:

  ALLBIG 
LCFEM
WPDNSE

然而,这会排除 README.TXT FILE001 ,这取决于您的要求(参见上文)应该很可能包含在内。



因此,更好的解决方案是:

  ls -1 | grep -v[[:lower:]] \ +

即打印所有行不包含小写字母。在我的TEMP目录中打印例如:

$ $ p $ $ $ $ $ $ ALLBIG $ b $
CRX_75DAF8CB7768
LCFEM
WPDNSE
〜DFA0214428CD719AF6.TMP



<最后,为了标记具有尾部/的目录,可以使用 -F (或 - classify )选项。

  ls -1F | grep -v[[:lower:]] \ +

再次输出示例:

  ALLBIG 
ALLBIG-01.TXT
ALLBIG005.TXT
CRX_75DAF8CB7768
LCFEM /
WPDNSE /
〜DFA0214428CD719AF6.TMP

请注意不同的选项使用 find ,如果您可以使用不同的输出(例如 find!-regex。* [az]。*),但是会有不同的输出。


I'm trying to list all entries in a directory whose names contain ONLY upper-case letters. Directories need "/" appended.

#!/bin/bash
cd ~/testfiles/
ls | grep -r *.*

Since grep by default looks for upper-case letters only (right?), I'm just recursively searching through the directories under testfiles for all names who contain only upper-case letters.

Unfortunately this doesn't work.

As for appending directories, I'm not sure why I need to do this. Does anyone know where I can start with some detailed explanations on what I can do with grep? Furthermore how to tackle my problem?

解决方案

No, grep does not only consider uppercase letters.

Your question I a bit unclear, for example:

  • from your usage of the -r option, it seems you want to search recursively, however you don't say so. For simplicity I assume you don't need to; consider looking into @twm's answer if you need recursion.
  • you want to look for uppercase (letters) only. Does that mean you don't want to accept any other (non letter) characters, but which are till valid for file names (like digits or dashes, dots, etc.)
  • since you don't say th it i not permissible to have only on file per line, I am assuming it is OK (thus using ls -1).

The naive solution would be:

ls -1 | grep "^[[:upper:]]\+$"

That is, print all lines containing only uppercase letters. In my TEMP directory that prints, for example:

ALLBIG
LCFEM
WPDNSE

This however would exclude files like README.TXT or FILE001, which depending on your requirements (see above) should most likely be included.

Thus, a better solution would be:

ls -1 | grep -v "[[:lower:]]\+"

That is, print all lines not containing an lowercase letter. In my TEMP directory that prints for example:

ALLBIG
ALLBIG-01.TXT
ALLBIG005.TXT
CRX_75DAF8CB7768
LCFEM
WPDNSE
~DFA0214428CD719AF6.TMP

Finally, to "properly mark" directories with a trailing '/', you could use the -F (or --classify) option.

ls -1F | grep -v "[[:lower:]]\+"

Again, example output:

ALLBIG
ALLBIG-01.TXT
ALLBIG005.TXT
CRX_75DAF8CB7768
LCFEM/
WPDNSE/
~DFA0214428CD719AF6.TMP

Note a different option would to be use find, if you can live with the different output (e.g. find ! -regex ".*[a-z].*"), but that will have a different output.

这篇关于使用grep列出目录中的条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆