Linux find str in files

How can I use grep to find a word inside a folder?

In Windows, I would have done a search for finding a word inside a folder. Similarly, I want to know if a specific word occurs inside a directory containing many sub-directories and files. My searches for grep syntax shows I must specify the filename, i.e. grep string filename . Now, I do not know the filename, so what do I do? A friend suggested to do grep -nr string , but I don’t know what this means and I got no results with it (there is no response until I issue a Ctrl + C ).

14 Answers 14

The dot at the end searches the current directory. Meaning for each parameter:

-n Show relative line number in the file 'yourString*' String for search, followed by a wildcard character -r Recursively search subdirectories listed . Directory for search (current directory) 

grep -nr ‘MobileAppSer*’ . (Would find MobileAppServlet.java or MobileAppServlet.class or MobileAppServlet.txt ; ‘MobileAppASer*.*’ is another way to do the same thing.)

To check more parameters use man grep command.

What’s the business with * ? It will either result in shell wildcard expansion (if there are filenames matching the wildcard pattern), or grep will take it as 0-or-more repetition operator for the character preceding * .

Now let’s consider both possibilities for grep -nr MobileAppSer* . 1. Assume we have 3 files in the current directory matching MobileAppSer* wildcard pattern: named MobileAppServlet.java , MobileAppServlet.class , MobileAppServlet.txt . Then grep will be invoked like this: grep -nr MobileAppServlet.class MobileAppServlet.java MobileAppServlet.txt . . It means search for text «MobileAppServlet.class» in files MobileAppServlet.java, MobileAppServlet.txt, and elsewhere in the current directory — which surely isn’t what the user wants here.

2. In case there are no files in the current directory matching the MobileAppSer* wildcard pattern, grep will receive the argument MobileAppSer* as-is and thus will take it as search for text «MobileAppSe» followed by 0 or more occurrences of «r», so it will attempt to find texts «MobileAppSe», «MobileAppSer», «MobileAppSerr», «MobileAppSerrr», etc. in current directory’s files contents — not what the user wants either.

I ran grep -nr ‘yourString*’ . and got some files with «binary file matches». You can add —text or -a to prevent this: grep -anr ‘yourString*’ .

grep -nr string my_directory 

Additional notes: this satisfies the syntax grep [options] string filename because in Unix-like systems, a directory is a kind of file (there is a term «regular file» to specifically refer to entities that are called just «files» in Windows).

grep -nr string reads the content to search from the standard input, that is why it just waits there for input from you, and stops doing so when you press ^C (it would stop on ^D as well, which is the key combination for end-of-file).

Читайте также:  Cannot write no space left on device linux

Hey, so if i want to search for a string irrespective of the case, must I do this: grep -i -nr «my word» .

@kiki: -r for grep means search in subdirectories recursively and -n means prefix each line of output with the corresponding line number of the file which contains that line. man grep describes all of this, and much more.

GREP: Global Regular Expression Print/Parser/Processor/Program.
You can use this to search the current directory.
You can specify -R for «recursive», which means the program searches in all subfolders, and their subfolders, and their subfolder’s subfolders, etc.

-n will print the line number, where it matched in the file.
-i will search case-insensitive (capital/non-capital letters).

grep -inR "your regex pattern" . 

Thanks. And grep -inR «[0-9a-fA-F]<32>» . helps find hashes (which are hex strings) in the files within the current directory. stackoverflow.com/a/25724915/470749

find directory_name -type f -print0 | xargs -0 grep -li word 

but that might be a bit much for a beginner.

find is a general purpose directory walker/lister, -type f means «look for plain files rather than directories and named pipes and what have you», -print0 means «print them on the standard output using null characters as delimiters». The output from find is sent to xargs -0 and that grabs its standard input in chunks (to avoid command line length limitations) using null characters as a record separator (rather than the standard newline) and then applies grep -li word to each set of files. On the grep , -l means «list the files that match» and -i means «case insensitive»; you can usually combine single character options so you’ll see -li more often than -l -i .

If you don’t use -print0 and -0 then you’ll run into problems with file names that contain spaces so using them is a good habit.

Источник

How to search for strings inside files in a folder?

Is there any utility to make searches for a string inside ASCII files to avoid command line searches? How to make a command line search, for example for the string «test» inside all files in the directory /var/x/ ?

9 Answers 9

I assume that your first question is about a GUI alternative to the grep command. I can’t help you with that, I always find the command line very effective.

As for the command line, try

If you want to search recursively (i.e. not only in /var/x/ , but also in subdirectories thereof), do

To avoid grepping the files which grep thinks to be binary, use the -I option:

If grep thinks a file is binary (based on first few bytes of the file), it will assume it does not match instead of going through the whole file.

Well, grep will also try search for the string in a binary file, and report it if it matches: Binary file file.jpg matches

Читайте также:  Добавление репозитория oracle linux

@January When a binary file is read as text, it often has extremely long «lines» (because a character or character sequence that would be interpreted to designate the end of a line may not appear for a long time, or ever). Depending on the way a text search utility is implemented, this could cause performance problems if each «line» is read fully into memory and then checked to see if it matches the search string (which is a reasonable way for grep to be coded, though I don’t know if Ubuntu’s grep is written that way).

Источник

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.

Читайте также:  Linux mint with wayland

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 .

Источник

Оцените статью
Adblock
detector