如何在 bash 中找到所有不以给定前缀开头的文件? [英] How do I find all files that do not begin with a given prefix in bash?

查看:13
本文介绍了如何在 bash 中找到所有不以给定前缀开头的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个文件夹中有一堆文件:

I have a bunch of files in a folder:

foo_1 
foo_2
foo_3
bar_1
bar_2
buzz_1
...

我想找到所有以给定前缀开头的文件,并将列表保存到文本文件中.以下是具有给定前缀的文件的示例:

I want to find all the files that do not start with a given prefix and save the list to a text file. Here is an example for the files that do have a given prefix:

find bar_* > Positives.txt

推荐答案

这应该适用于任何 shell

This should do the trick in any shell

ls | grep -v '^prefix'

-v 选项反转 grep 的搜索逻辑,使其过滤掉所有匹配项.使用 grep 代替 find 您可以使用强大的正则表达式代替有限的 glob 模式.

The -v option inverts grep's search logic, making it filter out all matches. Using grep instead of find you can use powerful regular expressions instead of the limited glob patterns.

这篇关于如何在 bash 中找到所有不以给定前缀开头的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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