Find's -name
option supports file globbing. It also supports a limited set of regex-like options like limited square-bracket expressions, but for actual regex matches, use -regex
.
If you're looking for a match in the contents of a file, use grep -r
as Craig suggested.
If you want to match the filename, then use find
with its -regex
option:
find . -type f -regex '.*[Rr]eading[Tt]est[Dd]ata.*' -print
Note the shift in regex, because find
doesn't portably support bracketed atoms in its regex. If you happen to be on a Linux system, GNU find supports a -regextype
option that gives you more control:
find . -regextype posix-extended -regex '.*((R|r)eading(T|t)est(D|d)ata).*' -print
Note that if all you're looking for is case matching, -iregex
or even -iname
may be sufficient. If you're using bash
as your shell, Gilles' globstar solution should work too.
You don't need to use awk
at all. Use the built-in tests that ksh
provides, something like this:
#!/bin/ksh
for NAME in *
do
FOUND=no
if [[ -d $NAME && $NAME != '.' && $NAME != '..' ]]
then
for SUBNAME in $NAME/*
do
if [[ -d $SUBNAME ]]
then
FOUND=yes
break
fi
done
if [[ $FOUND == no ]]
then
echo Found only files in $NAME
fi
fi
done
That little script looks in all the directories in the current directory, and tells you if they only contain files, no sub-directories.
Best Answer
Simply:
Add a
! -type d
to also exclude the files of type directory (like.
itself), or-type f
to include only regular files, excluding all other types of files (directories, fifos, symlinks, devices, sockets...).Beware however that
*
matches a sequence of 0 or more characters. So it could report file names that contain2013
if that2013
was preceded or followed by something that cannot be fully decoded as valid characters in the current locale.That can happen if you're in a locale where the characters can be encoded on more than one byte (like in UTF-8) for file names that are encoded in a different encoding. For instance, in a UTF-8 locale, it would report a
Stéphane2013
file if thaté
had been encoded in the iso8859-15 character set (as the 0xe9 byte).Best would be to make sure the file names are encoded in the locale's character set, but if you can't guarantee it, a work around is to run
find
in the C locale: