Finding a string in a file on a file system using Grep
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I need to do a search on a number of files in a file system to look for a string "RUSH2112". I have completed the following command however I am not sure if this is correct. I stand in the mounted file system.
find . -type f -exec grep -i1 "Rush2112" ;
grep find
add a comment |Â
up vote
1
down vote
favorite
I need to do a search on a number of files in a file system to look for a string "RUSH2112". I have completed the following command however I am not sure if this is correct. I stand in the mounted file system.
find . -type f -exec grep -i1 "Rush2112" ;
grep find
1
What Unix are you on and what does the-1
option do with yourgrep
?
– Kusalananda
1 hour ago
What would be your expected output? The lines containing the matches, or the filenames that contain the string, or ???
– Jeff Schaller
1 hour ago
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I need to do a search on a number of files in a file system to look for a string "RUSH2112". I have completed the following command however I am not sure if this is correct. I stand in the mounted file system.
find . -type f -exec grep -i1 "Rush2112" ;
grep find
I need to do a search on a number of files in a file system to look for a string "RUSH2112". I have completed the following command however I am not sure if this is correct. I stand in the mounted file system.
find . -type f -exec grep -i1 "Rush2112" ;
grep find
grep find
edited 1 hour ago


Jeff Schaller
34.9k952115
34.9k952115
asked 1 hour ago
Deirdre
315
315
1
What Unix are you on and what does the-1
option do with yourgrep
?
– Kusalananda
1 hour ago
What would be your expected output? The lines containing the matches, or the filenames that contain the string, or ???
– Jeff Schaller
1 hour ago
add a comment |Â
1
What Unix are you on and what does the-1
option do with yourgrep
?
– Kusalananda
1 hour ago
What would be your expected output? The lines containing the matches, or the filenames that contain the string, or ???
– Jeff Schaller
1 hour ago
1
1
What Unix are you on and what does the
-1
option do with your grep
?– Kusalananda
1 hour ago
What Unix are you on and what does the
-1
option do with your grep
?– Kusalananda
1 hour ago
What would be your expected output? The lines containing the matches, or the filenames that contain the string, or ???
– Jeff Schaller
1 hour ago
What would be your expected output? The lines containing the matches, or the filenames that contain the string, or ???
– Jeff Schaller
1 hour ago
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
5
down vote
accepted
To look for a string (not a regular expression) without caring for case, use grep -iF STRING
(where STRING
should be the quoted string you are looking for).
To recurse over all regular files in a directory hierarchy, do
find . -type f -exec grep -iF STRING /dev/null +
The main difference between this and your command is that this will execute grep
for groups of found files. Your command would run grep
once for each found file which may be slower if there are many files.
The other difference is that since grep
is invoked with more than one file, its output will contain the pathnames of the files where matches are found. The /dev/null
is there to force this behaviour in case grep
happens to be invoked with only a single file. You may use grep
's -H
option instead of including /dev/null
if your implementation of grep
has that non-standard (but common) option.
Some implementation of grep
also has the option to recurse directories by itself, without the help of find
. This is often done with an -R
option:
grep -R -iF STRING .
Note that GNU grep
also has a -r
option that is slightly different from its -R
option (with -r
, symbolic links will not be followed, which would more closely mimic the behaviour you'd get with running grep
via find . -type f
).
If what you're after is to find only the pathnames of the files that contain the string and not necessarily the actual lines from those files that match, then you may do that in a number of different ways.
Use
grep -q
as a test on each file:find . -type f -exec grep -qiF STRING ';' -print
Use
grep -l
across groups of files found byfind
:find . -type f -exec grep -ilF STRING +
Use recursive
grep -l
:grep -R -ilF STRING .
If you are wanting to do further processing of the files that contain the given string, then I would go with the first of these alternatives:
find . -type f -exec grep -qiF STRING ';' -exec sh -c '
for pathname do
# Process "$pathname" here
done' sh +
Related:
- Understanding the -exec option of `find`
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
To look for a string (not a regular expression) without caring for case, use grep -iF STRING
(where STRING
should be the quoted string you are looking for).
To recurse over all regular files in a directory hierarchy, do
find . -type f -exec grep -iF STRING /dev/null +
The main difference between this and your command is that this will execute grep
for groups of found files. Your command would run grep
once for each found file which may be slower if there are many files.
The other difference is that since grep
is invoked with more than one file, its output will contain the pathnames of the files where matches are found. The /dev/null
is there to force this behaviour in case grep
happens to be invoked with only a single file. You may use grep
's -H
option instead of including /dev/null
if your implementation of grep
has that non-standard (but common) option.
Some implementation of grep
also has the option to recurse directories by itself, without the help of find
. This is often done with an -R
option:
grep -R -iF STRING .
Note that GNU grep
also has a -r
option that is slightly different from its -R
option (with -r
, symbolic links will not be followed, which would more closely mimic the behaviour you'd get with running grep
via find . -type f
).
If what you're after is to find only the pathnames of the files that contain the string and not necessarily the actual lines from those files that match, then you may do that in a number of different ways.
Use
grep -q
as a test on each file:find . -type f -exec grep -qiF STRING ';' -print
Use
grep -l
across groups of files found byfind
:find . -type f -exec grep -ilF STRING +
Use recursive
grep -l
:grep -R -ilF STRING .
If you are wanting to do further processing of the files that contain the given string, then I would go with the first of these alternatives:
find . -type f -exec grep -qiF STRING ';' -exec sh -c '
for pathname do
# Process "$pathname" here
done' sh +
Related:
- Understanding the -exec option of `find`
add a comment |Â
up vote
5
down vote
accepted
To look for a string (not a regular expression) without caring for case, use grep -iF STRING
(where STRING
should be the quoted string you are looking for).
To recurse over all regular files in a directory hierarchy, do
find . -type f -exec grep -iF STRING /dev/null +
The main difference between this and your command is that this will execute grep
for groups of found files. Your command would run grep
once for each found file which may be slower if there are many files.
The other difference is that since grep
is invoked with more than one file, its output will contain the pathnames of the files where matches are found. The /dev/null
is there to force this behaviour in case grep
happens to be invoked with only a single file. You may use grep
's -H
option instead of including /dev/null
if your implementation of grep
has that non-standard (but common) option.
Some implementation of grep
also has the option to recurse directories by itself, without the help of find
. This is often done with an -R
option:
grep -R -iF STRING .
Note that GNU grep
also has a -r
option that is slightly different from its -R
option (with -r
, symbolic links will not be followed, which would more closely mimic the behaviour you'd get with running grep
via find . -type f
).
If what you're after is to find only the pathnames of the files that contain the string and not necessarily the actual lines from those files that match, then you may do that in a number of different ways.
Use
grep -q
as a test on each file:find . -type f -exec grep -qiF STRING ';' -print
Use
grep -l
across groups of files found byfind
:find . -type f -exec grep -ilF STRING +
Use recursive
grep -l
:grep -R -ilF STRING .
If you are wanting to do further processing of the files that contain the given string, then I would go with the first of these alternatives:
find . -type f -exec grep -qiF STRING ';' -exec sh -c '
for pathname do
# Process "$pathname" here
done' sh +
Related:
- Understanding the -exec option of `find`
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
To look for a string (not a regular expression) without caring for case, use grep -iF STRING
(where STRING
should be the quoted string you are looking for).
To recurse over all regular files in a directory hierarchy, do
find . -type f -exec grep -iF STRING /dev/null +
The main difference between this and your command is that this will execute grep
for groups of found files. Your command would run grep
once for each found file which may be slower if there are many files.
The other difference is that since grep
is invoked with more than one file, its output will contain the pathnames of the files where matches are found. The /dev/null
is there to force this behaviour in case grep
happens to be invoked with only a single file. You may use grep
's -H
option instead of including /dev/null
if your implementation of grep
has that non-standard (but common) option.
Some implementation of grep
also has the option to recurse directories by itself, without the help of find
. This is often done with an -R
option:
grep -R -iF STRING .
Note that GNU grep
also has a -r
option that is slightly different from its -R
option (with -r
, symbolic links will not be followed, which would more closely mimic the behaviour you'd get with running grep
via find . -type f
).
If what you're after is to find only the pathnames of the files that contain the string and not necessarily the actual lines from those files that match, then you may do that in a number of different ways.
Use
grep -q
as a test on each file:find . -type f -exec grep -qiF STRING ';' -print
Use
grep -l
across groups of files found byfind
:find . -type f -exec grep -ilF STRING +
Use recursive
grep -l
:grep -R -ilF STRING .
If you are wanting to do further processing of the files that contain the given string, then I would go with the first of these alternatives:
find . -type f -exec grep -qiF STRING ';' -exec sh -c '
for pathname do
# Process "$pathname" here
done' sh +
Related:
- Understanding the -exec option of `find`
To look for a string (not a regular expression) without caring for case, use grep -iF STRING
(where STRING
should be the quoted string you are looking for).
To recurse over all regular files in a directory hierarchy, do
find . -type f -exec grep -iF STRING /dev/null +
The main difference between this and your command is that this will execute grep
for groups of found files. Your command would run grep
once for each found file which may be slower if there are many files.
The other difference is that since grep
is invoked with more than one file, its output will contain the pathnames of the files where matches are found. The /dev/null
is there to force this behaviour in case grep
happens to be invoked with only a single file. You may use grep
's -H
option instead of including /dev/null
if your implementation of grep
has that non-standard (but common) option.
Some implementation of grep
also has the option to recurse directories by itself, without the help of find
. This is often done with an -R
option:
grep -R -iF STRING .
Note that GNU grep
also has a -r
option that is slightly different from its -R
option (with -r
, symbolic links will not be followed, which would more closely mimic the behaviour you'd get with running grep
via find . -type f
).
If what you're after is to find only the pathnames of the files that contain the string and not necessarily the actual lines from those files that match, then you may do that in a number of different ways.
Use
grep -q
as a test on each file:find . -type f -exec grep -qiF STRING ';' -print
Use
grep -l
across groups of files found byfind
:find . -type f -exec grep -ilF STRING +
Use recursive
grep -l
:grep -R -ilF STRING .
If you are wanting to do further processing of the files that contain the given string, then I would go with the first of these alternatives:
find . -type f -exec grep -qiF STRING ';' -exec sh -c '
for pathname do
# Process "$pathname" here
done' sh +
Related:
- Understanding the -exec option of `find`
edited 42 mins ago
answered 1 hour ago


Kusalananda
113k15216344
113k15216344
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f479113%2ffinding-a-string-in-a-file-on-a-file-system-using-grep%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
What Unix are you on and what does the
-1
option do with yourgrep
?– Kusalananda
1 hour ago
What would be your expected output? The lines containing the matches, or the filenames that contain the string, or ???
– Jeff Schaller
1 hour ago