Linux find file names with given string recursively
Use the find command,
find . -type f -name "*John*"
How can I recursively find all files in current and subfolders based on wildcard matching?
Use find
:
find . -name "foo*"
find
needs a starting point, so the .
(dot) points to the current directory.
How to find all files containing specific text (string) on Linux?
Do the following:
grep -rnw '/path/to/somewhere/' -e 'pattern'
-r
or-R
is recursive,-n
is line number, and-w
stands for match the whole word.-l
(lower-case L) can be added to just give the file name of matching files.-e
is the pattern used during the search
Along with these, --exclude
, --include
, --exclude-dir
flags could be used for efficient searching:
This will only search through those files which have .c or .h extensions:
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
This will exclude searching all the files ending with .o extension:
grep --exclude=\*.o -rnw '/path/to/somewhere/' -e "pattern"
For directories it's possible to exclude one or more directories using the
--exclude-dir
parameter. For example, this will exclude the dirsdir1/
,dir2/
and all of them matching*.dst/
:grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/search/' -e "pattern"
This works very well for me, to achieve almost the same purpose like yours.
For more options, see man grep
.
Linux search file with given name containing string recursively
The find
command's -exec grep
can solve your question, as in this example:
find /dir -name "*name_string*" -exec grep "content_string" {} /dev/null \;
This, however, will not only show you the name of the file, but also the line, containing the content_string. In case you just want the name of the string:
find /dir -name "*name_string*" -exec grep -l "content_string" {} \;
Obviously, you can use -exec
with other commands (head
, tail
, chmod
, ...)
Find all files with name containing string
Use find
:
find . -maxdepth 1 -name "*string*" -print
It will find all files in the current directory (delete maxdepth 1
if you want it recursive) containing "string" and will print it on the screen.
If you want to avoid file containing ':', you can type:
find . -maxdepth 1 -name "*string*" ! -name "*:*" -print
If you want to use grep
(but I think it's not necessary as far as you don't want to check file content) you can use:
ls | grep touch
But, I repeat, find
is a better and cleaner solution for your task.
Linux terminal: Recursive search for string only in files w given file extension; display file name and absolute path
According to the grep
manual, you can do this using the --include
option (combined with the -l
option if you want only the name — I usually use -n
to show line numbers):
--include=
globSearch only files whose name matches glob, using wildcard matching as described under
--exclude
.
-l
--files-with-matches
Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning of each file stops on the first match. (
-l
is specified by POSIX.)
A suitable glob would be "*.doc" (ensure that it is quoted, to allow the shell to pass it to grep
).
GNU grep also has a recursive option -r
(not in POSIX grep). Together with the globbing, you can search a directory-tree of ".doc" files like this:
grep -r -l --include="*.doc" "mystring" .
If you wanted to make this portable, then find
is the place to start. But using grep
's extension makes searches much faster, and is available on any Linux platform.
Trying to use GNU find to search recursively for filenames only (not directories) containing a string in any portion of the file name
specification:
- match "rain"
- in filename
- only at start of a word
- case-insensitive
assumptions:
- define "word" to be sequence of letters (no punctuation, digits, etc)
- paths have form
prefix/name
whereprefix
can have one or more levels delimited by/
and name does not contain/
constraints:
find -iregex
matches against entire path (-name
only matches filename)find -iregex
must match entirety of path (eg. "c" is only a partial match and does not match path "a/b/c")
method:
find
can return matches against non-files (eg. directories). Given definition 6, we would be unable to tell if name
is a directory or an ordinary file. To satisfy 2, we can exclude non-files using find
's -type f
predicate.
We can compare paths found by find
against our specification by using find
's case-insensitive regex matching predicate (-iregex
). The "grep" flavour (-regextype grep
) is sufficiently expressive.
Just using 1, a suitable regex is: rain
2+6+7 says we must forbid /
after "rain": rain[^/]*$
[/]
matches character in set (ie./
)[^/]
:^
inverts match: ie. character that is not/
*
matches preceding match zero or more times$
constrains preceding match to occur at end of input
3+5 says there must be no immediately preceding word characters: [^a-z]rain[^/]*$
a-z
is a shortcut for the rangea
toz
8 requires matching the prefix explicitly: ^.*[^a-z]rain[^/]*$
^
outside of[...]
constrains subsequent match to occur at beginning of input.
matches anything[^a-z]
matches a non-alphabetic
Final command-line:
find . -type f -regextype grep -iregex '^.*[^a-z]rain[^/]*$'
Note: The leading ^
and trailing $
are not actually required, given 8, and could be elided.
exercise for the reader:
- extend "word" to non-ASCII characters (eg. UTF-8)
How do I recursively grep all directories and subdirectories?
grep -r "texthere" .
The first parameter represents the regular expression to search for, while the second one represents the directory that should be searched. In this case, .
means the current directory.
Note: This works for GNU grep, and on some platforms like Solaris you must specifically use GNU grep as opposed to legacy implementation. For Solaris this is the ggrep
command.
Related Topics
Compiler Can't Find Libxml/Parser.H
How to Confirm Sftp File Delivery
What Would Be the Equivalent of Win32 API in Linux
How to Install Maven on Redhat Linux
What Is the Use of _Iomem in Linux While Writing Device Drivers
Curl Does Not Work But Same Site Works with Browser
Linux - Save Only Recent 10 Folders and Delete The Rest
Concatenating Two String Variables in Bash Appending Newline
Memory Access Error Sys_Rt_Sigaction (Signal Handler)
How to Assign a Name for a Screen
How to File Split at a Line Number
Expression After Last Specific Character
Postgresql CSV Import from Command Line