Linux find file contain string

Find files containing string in file name and different string within file?

I would like to (recursively) find all files with «ABC» in their file name, which also contain «XYZ» in the file. I tried:

find . -name "*ABC*" | grep -R 'XYZ' 

4 Answers 4

That’s because grep can’t read file names to search through from standard input. What you’re doing is printing file names that contain XYZ . Use find ‘s -exec option instead:

find . -name "*ABC*" -exec grep -H 'XYZ' <> + 
 -exec command ; Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of `;' is encountered. The string `<>' is replaced by the current file name being processed everywhere it occurs in the arguments to the command, not just in arguments where it is alone, as in some versions of find. [. ] -exec command <> + This variant of the -exec action runs the specified command on the selected files, but the command line is built by appending each selected file name at the end; the total number of invoca‐ tions of the command will be much less than the number of matched files. The command line is built in much the same way that xargs builds its command lines. Only one instance of `<>' is allowed within the command. The command is executed in the starting directory. 

If you don’t need the actual matching lines but only the list of file names containing at least one occurrence of the string, use this instead:

find . -name "*ABC*" -exec grep -l 'XYZ' <> + 

Источник

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.

Читайте также:  Network manager linux bridge

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 

Источник

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/’

Читайте также:  Уорд брайан внутреннее устройство linux epub

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 .

Источник

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