简体   繁体   English

如何grep文件扩展名

[英]How to grep for a file extension

I am currently trying to a make a script that would grep input to see if something is of a certain file type (zip for instance), although the text before the file type could be anything, so for instance我目前正在尝试制作一个脚本,该脚本将 grep 输入以查看某些文件是否属于某种文件类型(例如 zip),尽管文件类型之前的文本可以是任何东西,例如

something.zip
this.zip
that.zip

would all fall under the category.都属于该类别。 I am trying to grep for these using a wildcard, and so far I have tried this我正在尝试使用通配符对这些进行 grep,到目前为止我已经尝试过

grep ".*.zip"

But whenever I do that, it will find the .zip files just fine, but it will still display output if there are additional characters after the .zip so for instance .zippppppp or .zipdsjdskjc would still be picked up by grep.但是每当我这样做时,它会发现.zip文件很好,但是如果.zip之后还有其他字符,它仍然会显示输出,例如.zippppppp.zipdsjdskjc仍然会被 grep 拾取。 Having said that, what should I do to prevent grep from displaying matches that have additional characters after the .zip ?话虽如此,我应该怎么做才能防止 grep 显示在.zip之后具有附加字符的匹配项?

Test for the end of the line with $ and escape the second .$测试行尾并转义第二个. with a backslash so it only matches a period and not any character.带有反斜杠,因此它仅匹配句点而不匹配任何字符。

grep ".*\.zip$"

However ls *.zip is a more natural way to do this if you want to list all the .zip files in the current directory or find . -name "*.zip"但是,如果要列出当前目录中的所有.zip文件或find . -name "*.zip" .zip 文件,则ls *.zip是一种更自然的方法。 find . -name "*.zip" for all .zip files in the sub-directories starting from (and including) the current directory. -从当前目录开始(包括)子目录中的所有.zip文件的名称为find . -name "*.zip"

在 UNIX 上,尝试:

find . -type f -name \*.zip

You can also use grep to find all files with a specific extension:您还可以使用 grep 查找具有特定扩展名的所有文件:

find .|grep -e "\.gz$"

The . . means the current folder.表示当前文件夹。 If you want to specify a folder other than the current folder, just replace the .如果要指定当前文件夹以外的文件夹,只需替换. with the path of the folder.与文件夹的路径。 Here is an example: Let's find all files that end with .gz and are in the folder /var/log这是一个示例:让我们查找以.gz结尾且位于文件夹 /var/log 中的所有文件

  find /var/log/ |grep -e "\.gz$"

The output is something similar to the following:输出类似于以下内容:

 ✘ ⚙> find /var/log/ |grep -e "\.gz$"

/var/log//mail.log.1.gz
/var/log//mail.log.0.gz
/var/log//system.log.3.gz
/var/log//system.log.7.gz
/var/log//system.log.6.gz
/var/log//system.log.2.gz
/var/log//system.log.5.gz
/var/log//system.log.1.gz
/var/log//system.log.0.gz
/var/log//system.log.4.gz

The $ sign says that the file extension is ending with gz $符号表示文件扩展名以 gz 结尾

You need to do a couple of things.你需要做几件事。 It should look like this:它应该如下所示:

grep '.*\.zip$'

You need to escape the second dot, so it will just match a dot, and not any character.您需要转义第二个点,所以它只会匹配一个点,而不是任何字符。 Using single quotes makes the escaping a bit easier.使用单引号使转义更容易一些。

You need the dollar sign at the end of the line to indicate that you want the "zip" to occur at the end of the line.您需要在行尾使用美元符号来表示您希望“zip”出现在行尾。

I use this to get a listing of the file types inside a folder.我使用它来获取文件夹内文件类型的列表。

find . -type f | egrep -i -E -o "\.{1}\w*$" | sort -su

Outputs for example:输出例如:

.DS_Store
.MP3
.aif
.aiff
.asd
.doc
.flac
.jpg
.m4a
.m4p
.m4r
.mp3
.pdf
.png
.txt
.wav
.wma
.zip

BONUS: with奖金:与

find . -type f | egrep -i -E -o "\.{1}\w*$" | sort | uniq -c

You'll get the file count:您将获得文件数:

    106 .DS_Store
     35 .MP3
     89 .aif
      5 .aiff
    525 .asd
      1 .doc
     60 .flac
     48 .jpg
    149 .m4a
     11 .m4p
      1 .m4r
  12844 .mp3
      1 .pdf
      5 .png
      9 .txt
    108 .wav
     44 .wma
      2 .zip

Try: grep -o -E "(\\.([Az])+)+"试试: grep -o -E "(\\.([Az])+)+"

I used this to get multi-dotted/multiple extensions.我用它来获得多点/多扩展。 So if the input was hello.tar.gz , then it would output .tar.gz .因此,如果输入是hello.tar.gz ,那么它将输出.tar.gz For single dotted, use grep -o -E "\\.([Az])+$" .对于单点,使用grep -o -E "\\.([Az])+$" Tested on Cygwin/MingW+MSYS.在 Cygwin/MingW+MSYS 上测试。

One more fix/addon of the above example:上述示例的另一个修复/插件:

# multi-dotted/multiple extensions
grep -oEi "(\\.([A-z0-9])+)+" file.txt

# single dotted
grep -oEi "\\.([A-z0-9])+$" file.txt

This will get file extensions like '.mp3' and etc.这将获得文件扩展名,如“.mp3”等。

如果您只想在当前文件夹中查找,为什么不用这个没有 grep 的简单命令呢?

ls *.zip 

Just reviewing some of the other answers.只是回顾一些其他的答案。 The .* isn't necessary, and if you're looking for a certain file extension, it's best to include -i so that it's case-insensitive; .*不是必需的,如果您正在寻找某个文件扩展名,最好包含 -i 以便它不区分大小写; in case the file is HELLO.ZIP, for example.例如,如果文件是 HELLO.ZIP。 I don't think the quotes are necessary, either.我也不认为引号是必要的。

grep -i \.zip$
grep -r pattern --include="*.txt" /path/to/dir/

Simply do :只需这样做:

grep ".*.zip$"

The "$" indicates the end of line “$”表示行尾

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

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