1

I've done quite a lot of research and tried each and every technique listed (some don't work on freebsd/osx), yet when i use the find command for the utmost simple search it comes back empty.

I have a directory full of files like:

file_name_one.ext
_something.ext
some_other_file.ext
maybe_one_more.ext

I've tried the following to find all files containing an underscore:

find . -regex "_"
find . -regex _
find . -name "_"
find . -regex "_"
find . -regex "\_"
find . -regex "_.*"
find . -regex "\_,*"
find . -regex "\_.*"
find . -name "_" -print 
find . -regex '_'
find . -regex '\(_\)'
find . -regex '$\137'
find . -regex '\137'
find . -regex '(_)'
find . -regex '\x5F'
find . -regex '.*\x5F'
find . -regex '.*/[\x5F/]
find . -regex '.*/[\x5F/]'
find . -regex '/_/'
find -E . -regex '.*/[^_/]'
find -E . -regex '\137'
find -E . -regex '\137' -print
find -E . -regex '$\137'
find -E . -regex '(_)'
find . -regextype sed -regex "_"

with NO LUCK ...what in heck is going on here? why doesn't it match any files containing an underscore?

Community
  • 1
  • 1
user3161696
  • 11
  • 1
  • 2
  • 2
    Try `find . -name '*_*'` or `find . -regex '.*_.*'` The man page says that the regex needs to match the entire path. Actually, `find . -regex '.*/.*_.*'` is probably better, so that you don't unintentionally match paths like ./a_b/c – Mark Plotnick Jan 05 '14 at 21:03

2 Answers2

2

Let's break down the question into the significant parts. You want to:

  • find files
  • that have an underscore in the filename.

For finding only regular files, use -type f. For finding names that contain a certain character, use -name and a shell pattern.

From the documentation;

 -type t
         True if the file is of the specified type.  Possible file types
         are as follows:

         b       block special
         c       character special
         d       directory
         f       regular file
         l       symbolic link
         p       FIFO
         s       socket

and

 -name pattern
         True if the last component of the pathname being examined matches
         pattern. Special shell pattern matching characters (“[”, “]”,
         “*”, and “?”) may be used as part of pattern. These characters
         may be matched explicitly by escaping them with a backslash
         (“\”).

So:

find -s . -type f -name '*_*'

The -s option is used for sorting the results.

You should use -regex if you want to match the whole path and if you require an expression that is not reproducable in a shell pattern.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94
0

Or you could spice it up with any of the following regular expressions:

find . -type f -regex ".*_.*"
find . -type f -regex '.*_.*'
find . -type f -regex .*_.*

What you missed in your first example was that something could come before/after the underscore, hence the .*, where . equals any character (except line breaks) and * equals zero or more characters. Wether you use ", ' or nothing surrounding the expression does not matter in this case.

tivolimeister
  • 121
  • 1
  • 2