Find files containing a given text
In bash I want to return file name (and the path to the file) for every file of type .php|.html|.js containing the case-insensitive string «document.cookie» | «setcookie» How would I do that?
6 Answers 6
egrep -ir --include=*. "(document.cookie|setcookie)" .
The r flag means to search recursively (search subdirectories). The i flag means case insensitive.
If you just want file names add the l (lowercase L ) flag:
egrep -lir --include=*. "(document.cookie|setcookie)" .
that didn’t seem to work for me(at least not on mac). just hangs. egrep -lir —include=* «repo» egrep: warning: recursive search of stdin
You forgot to add the path to search. The path is ‘.’ in the above example. In your case, the script is waiting for the input to search on stdin. Try: egrep -lir —include=* «repo» / (or any other path)
Try something like grep -r -n -i —include=»*.html *.php *.js» searchstrinhere .
the -i makes it case insensitlve
the . at the end means you want to start from your current directory, this could be substituted with any directory.
the -r means do this recursively, right down the directory tree
the -n prints the line number for matches.
the —include lets you add file names, extensions. Wildcards accepted
find them and grep for the string:
This will find all files of your 3 types in /starting/path and grep for the regular expression ‘(document\.cookie|setcookie)’ . Split over 2 lines with the backslash just for readability.
find /starting/path -type f -name "*.php" -o -name "*.html" -o -name "*.js" | \ xargs egrep -i '(document\.cookie|setcookie)'
Thanks @Michael Berkowski This way fastest more than 5 or 8 times # egrep -ir —include=file.foo «(foo|bar)» /dir on ~500Gb weigth directory.
Sounds like a perfect job for grep or perhaps ack
Or this wonderful construction:
find . -type f \( -name *.php -o -name *.html -o -name *.js \) -exec grep "document.cookie\|setcookie" /dev/null <> \;
@MichaelBerkowski : You can use it like this to deal with whitespace in filenames: find . -type f -print0 | xargs -0 -I <> grep «search_string» <> . Of course, the other options can be added as well.
find . -type f -name '*php' -o -name '*js' -o -name '*html' |\ xargs grep -liE 'document\.cookie|setcookie'
Just to include one more alternative, you could also use this:
find «/starting/path» -type f -regextype posix-extended -regex «^.*\.(php|html|js)$» -exec grep -EH ‘(document\.cookie|setcookie)’ <> \;
- -regextype posix-extended tells find what kind of regex to expect
- -regex «^.*\.(php|html|js)$» tells find the regex itself filenames must match
- -exec grep -EH ‘(document\.cookie|setcookie)’ <> \; tells find to run the command (with its options and arguments) specified between the -exec option and the \; for each file it finds, where <> represents where the file path goes in this command. while
- E option tells grep to use extended regex (to support the parentheses) and.
- H option tells grep to print file paths before the matches.
And, given this, if you only want file paths, you may use:
find «/starting/path» -type f -regextype posix-extended -regex «^.*\.(php|html|js)$» -exec grep -EH ‘(document\.cookie|setcookie)’ <> \; | sed -r ‘s/(^.*):.*$/\1/’ | sort -u
- | [pipe] send the output of find to the next command after this (which is sed , then sort )
- r option tells sed to use extended regex.
- s/HI/BYE/ tells sed to replace every First occurrence (per line) of «HI» with «BYE» and.
- s/(^.*):.*$/\1/ tells it to replace the regex (^.*):.*$ (meaning a group [stuff enclosed by () ] including everything [ .* = one or more of any-character] from the beginning of the line [ ^ ] till’ the first ‘:’ followed by anything till’ the end of line [ $ ]) by the first group [ \1 ] of the replaced regex.
- u tells sort to remove duplicate entries (take sort -u as optional).
. FAR from being the most elegant way. As I said, my intention is to increase the range of possibilities (and also to give more complete explanations on some tools you could use).
Find all files with name containing string [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
I have been searching for a command that will return files from the current directory which contain a string in the filename. I have seen locate and find commands that can find files beginning with something first_word* or ending with something *.jpg . How can I return a list of files which contain a string in the filename? For example, if 2012-06-04-touch-multiple-files-in-linux.markdown was a file in the current directory. How could I return this file and others containing the string touch ? Using a command such as find ‘/touch/’
8 Answers 8
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:
But, I repeat, find is a better and cleaner solution for your task.
@Dru, if you want it ‘shorter’ you can avoid -print as this is the default behaviour and . as this is the default folder where it checks.
Awesome. I see myself using this a lot. I will take your -print and . removal suggestions, make it a command, and try to pass *string* in as a command line argument.
find . -name «*string*» Works great too. Removing . throws an error on my end. Thanks again @Zagorax.
Just an observation, the above command complained about the position of -maxdepth argument better to move it before -name as @Sunil Dias mentioned
I have find *.jpg -name «*from*» -print which works for a given directory. How can I make search recursively? I’ve tried -maxdepth .
-R means recurse. If you would rather not go into the subdirectories, then skip it.
-i means «ignore case». You might find this worth a try as well.
Great. I noticed that some file contents follow a : . Is there anyway to withhold that? Using an option perhaps?
That seems to only produce the contents of the files. You essentially answered my question though, I can try to do some digging for withholding the contents.
Ah. you only need the file names? Run : grep -R «touch» . | cut -d «:» -f 1 (sorry must have misread you).
Thanks @carlspring this is interesting. grep either returns files with contents and filenames containing touch or contents containing touch , I’m not sure which is the case, yet. Of the list of files returned, half contain touch in the title and the other half conatains touch in the body, not the title. Just realized this.
The -maxdepth option should be before the -name option, like below.,
find . -maxdepth 1 -name "string" -print
find $HOME -name "hello.c" -print
This will search the whole $HOME (i.e. /home/username/ ) system for any files named “hello.c” and display their pathnames:
/Users/user/Downloads/hello.c /Users/user/hello.c
However, it will not match HELLO.C or HellO.C . To match is case insensitive pass the -iname option as follows:
find $HOME -iname "hello.c" -print
/Users/user/Downloads/hello.c /Users/user/Downloads/Y/Hello.C /Users/user/Downloads/Z/HELLO.c /Users/user/hello.c
Pass the -type f option to only search for files:
find /dir/to/search -type f -iname "fooBar.conf.sample" -print find $HOME -type f -iname "fooBar.conf.sample" -print
The -iname works either on GNU or BSD (including OS X) version find command. If your version of find command does not supports -iname , try the following syntax using grep command:
find $HOME | grep -i "hello.c" find $HOME -name "*" -print | grep -i "hello.c"
find $HOME -name '[hH][eE][lL][lL][oO].[cC]' -print
/Users/user/Downloads/Z/HELLO.C /Users/user/Downloads/Z/HEllO.c /Users/user/Downloads/hello.c /Users/user/hello.c
If the string is at the beginning of the name, you can do this
$ compgen -f .bash .bashrc .bash_profile .bash_prompt
compgen is not an appropriate hammer for this nail. This little-used tool is designed to list available commands, and as such, it lists files in the current directory (which could be scripts) and it can neither recurse nor look past the beginning of a file name nor search file contents, making it mostly useless.
An alternative to the many solutions already provided is making use of the glob ** . When you use bash with the option globstar ( shopt -s globstar ) or you make use of zsh , you can just use the glob ** for this.
does a recursive directory search for files named bar (potentially including the file bar in the current directory). Remark that this cannot be combined with other forms of globbing within the same path segment; in that case, the * operators revert to their usual effect.
Note that there is a subtle difference between zsh and bash here. While bash will traverse soft-links to directories, zsh will not. For this you have to use the glob ***/ in zsh .
How to find lines containing a string in linux [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
I have a file in Linux, I would like to display lines which contain a specific string in that file, how to do this?
5 Answers 5
The usual way to do this is with grep , which uses a regex pattern to match lines:
Each line which matches the pattern will be output. If you want to search for fixed strings only, use grep -F ‘pattern’ file . fgrep is shorthand for grep -F .
addition grep -rn ‘string’ /path/ if you want to search a string in a folder in which file including and line number
Besides grep , you can also use other utilities such as awk or sed
Here is a few examples. Let say you want to search for a string is in the file named GPL .
Your sample file
$ cat -n GPL 1 The GNU General Public License is a free, copyleft license for 2 The licenses for most software and other practical works are designed 3 the GNU General Public License is intended to guarantee your freedom to 4 GNU General Public License for most of our software;
$ grep is GPL The GNU General Public License is a free, copyleft license for the GNU General Public License is intended to guarantee your freedom to
$ awk /is/ GPL The GNU General Public License is a free, copyleft license for the GNU General Public License is intended to guarantee your freedom to
$ sed -n '/is/p' GPL The GNU General Public License is a free, copyleft license for the GNU General Public License is intended to guarantee your freedom to