深度阅读

How to use regular expressions in 'find' searches in Linux?

作者
作者
2023年08月22日
更新时间
10.55 分钟
阅读时间
0
阅读量

To use regular expressions in ‘find’ searches in Linux, you can use the ‘-regex’ option followed by the regular expression you want to use to match file names.

For example, the following command will find all files in the current directory and its subdirectories that have file names starting with “log” and ending with a “.txt” extension:

find . -type f -regex "./log.*\.txt"

In this example, the regular expression “./log.*.txt” matches file names starting with “log” and ending with a “.txt” extension.

You can also use different types of regular expressions, such as extended regular expressions and Perl-style regular expressions. The ‘-regextype’ option is used to specify the type of regular expression you want to use. For example:

find . -type f -regextype posix-extended -regex "./log.*\.(txt|pdf)"

This will find all files in the current directory and its subdirectories that have file names starting with “log” and ending with either a “.txt” or “.pdf” extension using the POSIX extended regular expression type.

相关标签

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。