简体   繁体   中英

How to find files with regex and list them?

I am new to the whole command-line thing and trying to figure out how to search the current directory and its sub directories for files with a specific filename via regex. Then I want to have the files listed in my command-line.

The regex should match files like:

B2ctes_UCUAAwF-K-large-123x322-132x423.jpg
this_is-a-123-file_name-3124x2445-4235x32.jpeg
file-32x32-64x64.png

The important part is the -[number]x[number]-[number]x[number]

My attempt looks like this:

find . -type f -regex ".+?-\d+x\d+-\d+x\d+\.\w{3,4}" -ls;

There are two problems with this:

  1. -ls puts shows a lot of information. I just want the filenames.
  2. The regex doesn't work. I have tried to use .+ , but even that does not return anything.

You can use this find with regex :

find . -regextype posix-extended -type f -regex ".*-[[:digit:]]+x[[:digit:]]+-[[:digit:]]+x[[:digit:]]+\.[[:alnum:]]{3,4}"

Or on OSX:

find -E . -type f -regex ".*-[[:digit:]]+x[[:digit:]]+-[[:digit:]]+x[[:digit:]]+\.[[:alnum:]]{3,4}"

And without regex :

find . -type f -name "*-[[:digit:]]*x[[:digit:]]*-[[:digit:]]*x[[:digit:]]*.[[:alnum:]]*"

What about simply :

find . -type f -name '-[0-9]*x[0-9]*-[0-9]*x-[0-9]*'

or

find . -type f  -regextype posix-egrep -regex '.*-[0-9]+x[0-9]+-[0-9]+x-[0-9]+.*'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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