0

I am comfortable with AWK regular expressions and would like to use them in GNU find. Searching find's manpages I found there are two options called -regex and -regextype which seemed relevant.

So I tried the following to list all the files with prefix file in my directory which looks like this

[Desktop/test]$ tree .
.
├── bar
├── file1.pdf
├── file2.pdf
├── file3.pdf
└── foo

0 directories, 5 files
[Desktop/test]$ 

The command

[Desktop/test]$ find . -type f -name "*.pdf"
./file3.pdf
./file2.pdf
./file1.pdf

works as expected.

But trying

find . -type f -regextype awk -regex "file"

gave me no output (or errors). Where am I going wrong in the second invocation?


NOTE

I am using the following version of find on my Linux Mint box

find (GNU findutils) 4.7.0-git Copyright (C) 2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

Written by Eric B. Decker, James Youngman, and Kevin Dalley. Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION FTS(FTS_CWDFD) CBO(level=2)

1 Answer 1

3

Read the find manual:

-regex pattern

File name matches regular expression pattern. This is a match on the whole path, not a search. For example, to match a file named ./fubar3, you can use the regular expression .*bar. or .*b.*3, but not f.*r3. The regular expressions understood by find are by default Emacs Regular Expressions (except that `.' matches newline), but this can be changed with the -regextype option.

So you want

find . -type f -regextype awk -regex ".*\.pdf"
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .