简体   繁体   English

如何过滤linux中的目录文件

[英]How to filter files in directory in linux

How to display the types of all the files in the personal directory in home directory 如何显示主目录中个人目录中所有文件的类型
that: 那:

  1. do not start with certain letters like a , k 不要以某些字母开头,如ak
  2. and the third letter in their name is not a digit and not a letter (upper or lower case) 并且他们名下的第三个字母不是数字而不是字母(大写或小写)

Try grep with regular expressions: 尝试使用正则表达式grep:

ls -1 | grep -ve "^a\|^k\|^..[0-9]\|^..\w" | cut -d . -f 2

-v : veto matches -e : use regular expressions -v :veto matches -e :使用正则表达式

The -1 is to make sure that you don't have several files in one line. -1是为了确保一行中没有多个文件。

The regular expression means: 正则表达式意味着:

  1. ^a\\|^k : no a or k at the beginning ^a\\|^k :开头没有ak
  2. ^..[0-9] : no number at the third position ( . is a wildcard for one position) ^..[0-9] :第三个位置没有数字( .是一个位置的通配符)
  3. ^..\\w : no word (character or _ ) at the third position. ^..\\w :第三个位置没有单词(字符或_ )。

It's a bit longer but I'd consider it as a goog way to dive into regular expressions. 它有点长,但我认为这是一种潜入正则表达式的goog方式。 For more details about regular expressions look for example here . 有关正则表达式的更多详细信息,请参见此处

The cut command looks for the delimiter . cut命令查找分隔符. and prints the second part of it ( -f 2 ). 并打印它的第二部分( -f 2 )。

If by "types" you mean extensions: 如果“类型”是指扩展名:

$ ls | egrep '^[^ak].[^a-zA-Z0-9]' | sed -e s/.*\\.//

There are shorthands for [^a-zA-Z0-9] but this one is easy to understand and adapt. [^a-zA-Z0-9]有一些缩写,但这个很容易理解和适应。

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

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