Linux search all files grep

Содержание
  1. How to Perform Grep Search on All Files and in All Directories
  2. Grep search in all files of a directory
  3. Grep recursive search in all subdirectories of a directory
  4. Bonus tip: Exclude a certain directory from the recursive grep search
  5. To summarize
  6. How to perform grep operation on all files in a directory?
  7. 5 Answers 5
  8. Grep In a Directory
  9. [#grep-current-directory][.inline-code]grep[.inline-code] search current directory[#grep-current-directory]
  10. [#grep-current-directory-and-subdirectories][.inline-code]grep[.inline-code] search current directory and subdirectories[#grep-current-directory-and-subdirectories]
  11. [#grep-r-vs-R][.inline-code]grep -r[.inline-code] is different from [.inline-code]grep -R[.inline-code][#grep-r-vs-R]
  12. [#grep-specific-directory][.inline-code]grep[.inline-code] search specific directory[#grep-specific-directory]
  13. [#grep-multiple-files][.inline-code]grep[.inline-code] across multiple files[#grep-multiple-files]
  14. [#grep-all-files][.inline-code]grep[.inline-code] search all files[#grep-all-files]
  15. [#grep-modifiers-multiple-files]Modifiers to the [.inline-code]grep[.inline-code] command across multiple files[#grep-modifiers-multiple-files]
  16. [#grep-exclude-directories][.inline-code]grep[.inline-code] exclude directories[#grep-exclude-directories]
  17. [#grep-counting-multiple-files][.inline-code]grep[.inline-code] counting across multiple files[#grep-counting-multiple-files]
  18. [#grep-ignore-case][.inline-code]grep[.inline-code] ignore case[#grep-ignore-case]
  19. [#grep-return-line-number][.inline-code]grep[.inline-code] return line number[#grep-return-line-number]
  20. [#grep-show-file-name][.inline-code]grep[.inline-code] show file name[#grep-show-file-name]
  21. [#more-on-grep]Find out more about [.inline-code]grep[.inline-code][#more-on-grep]

How to Perform Grep Search on All Files and in All Directories

The versatile grep command lets you perform search for a text in all the files and all the subdirectories of a directory. Here’s how to do that.

Grep is an excellent tool when you have to search on the content of a file.

Usually, you run grep on a single file like this:

grep search_term filename

Grep is quite versatile. If you want to search all the files in a directory with grep, use it like this:

There is a problem with it. It only searches in all the files in the current directory. It won’t search in the subdirectories.

You can make grep search in all the files and all the subdirectories of the current directory using the -r recursive search option:

You may also specify the directory path if you are not in the directory where you want to perform the search:

grep -r search_term directory_path

That was a quick recap. Let me show you all this in details with proper examples so that it is easier for you to understand.

Grep search in all files of a directory

Here’s the directory structure I am going to use in this example. Except empty.txt, all files contain the term ‘simple’ on which I’ll perform the grep search.

[email protected]:~/scripts$ tree . ├── dir1 │ └── new_script.sh ├── dir2 │ └── your_script.sh ├── dir3 │ ├── empty.txt │ └── linked.txt -> ../../sample.txt ├── my_script.sh └── your_script.sh 3 directories, 6 files 

To search for the word ‘simple’ in all the files of the current directories, just use wild card (*). The wild card actually substitutes with the name of all the files and directories in the current directory.

This will search in all the files in the current directories, but it won’t enter the subdirectories. Since you cannot directly grep search on a directory, it will show «XYZ is a directory» error along with search results.

Grep search in all files of a folder

If you are not in the same directory where you want to perform, you can specify the directory path and end it with /*

grep search_term directory_path/*

Basically, you are using the wild card to expand on all the elements (files and directories) of the given directory.

Читайте также:  Все переменные окружения линукс

Now that you know that, let’s see how you can perform a recursive search with grep so that it also looks into the files in the subdirectories.

Grep recursive search in all subdirectories of a directory

Grep provides a -r option for the recursive search. With this option, grep will look into all the files in the current (or specified) directory and it will also look into all the files of all the subdirectories.

Here’s the recursive search I performed in the previous example to do a grep search in the current folder:

Grep recursive search in all files and all subdirectories

There is also a -R option for recursive search and it works almost the same as the -r option.

So, what’s the difference grep -r and grep -R ? Only one, actually. The -R is dereferenced search which means it will follow the symbolic links to go to the original file (which may be located in some other part of the system).

Take a look at the output of the -R search in the same example:

Recursive search with grep including the symbolic links

Did you notice that it gives an additional search result with the linked.txt which is basically a symbolic link and was omitted from the grep search with -r option?

If you are not in the directory where you want to perform the recursive search, just provide the absolute or relative path of the directory to grep command:

grep -r search_term path_to_directory

Bonus tip: Exclude a certain directory from the recursive grep search

Everything seems good but what if you want to exclude a certain directory from the recursive search? There is a provision for that too. I told you, grep is an extremely versatile command.

grep -r --exclude-dir=dir_name serach_term directory_path

That’s not it. You can exclude more than one subdirectory from the recursive search in the following fashion:

grep -r --exclude-dir= serach_term directory_path

Here’s what excluding directories look like in our example here:

Exclude directories from grep recursive search

And yes, as demonstrated by the example above, the exclusion works with both -r and -R recursive options.

To summarize

Here’s a quick summary of using grep search for multiple files and directories:

Grep Command Description
grep string * Searches in all the files in current directory
grep string dir Searches in all the files in dir directory
grep -r string . Recursive search in all the files in all the subdirectories
grep -r string dir Recursive search in all files in all the subdirectories of dir
grep -R string . Same as r but follows the symbolic links

I hope you like this quick grep tip. If you want more, you may read this detailed tutorial on using the grep command:

Let me know if you have any questions or suggestions on this topic.

Источник

How to perform grep operation on all files in a directory?

Working with xenserver, and I want to perform a command on each file that is in a directory, grepping some stuff out of the output of the command and appending it in a file. I’m clear on the command I want to use and how to grep out string(s) as needed. But what I’m not clear on is how do I have it perform this command on each file, going to the next, until no more files are found.

Читайте также:  Linux limit process number

5 Answers 5

In Linux, I normally use this command to recursively grep for a particular text within a directory:

  • r = recursive i.e, search subdirectories within the current directory
  • n = to print the line numbers to stdout
  • i = case insensitive search

grep $PATTERN * would be sufficient. By default, grep would skip all subdirectories. However, if you want to grep through them, grep -r $PATTERN * is the case.

@Tomáš Zato, just supply all your file patterns instead of *: grep $PATTERN *.cpp *.h . If you need more specific rules for what files should be grepped, use find command (check Rob’s answer).

@Chris it’s possible you don’t have *.scss files in current directory but somewhere deeper in subdirs so grep does not look in all the files you wanted. You should use —include option to tell grep to look recursively for files that matches specific patterns: grep -r x —include ‘*.scss’ . (note the quotes, they prevent the pattern from being expanded by the shell). Or just use find (see Rob’s answer).

You want grep -s so you don’t get a warning for each subdirectory that grep skips. You should probably double-quote «$PATTERN» here.

Источник

Grep In a Directory

[#grep-current-directory][.inline-code]grep[.inline-code] search current directory[#grep-current-directory]

When you want to search in all the files of the current directory, regardless of their name or extension, you can use the wildcard character after your [.inline-code]grep[.inline-code] command as follows:

Note that this command does not search in subdirectories. Instead, it tells you that [.inline-code]model1[.inline-code] and [.inline-code]model2[.inline-code] are directories and that they are not searched.

[#grep-current-directory-and-subdirectories][.inline-code]grep[.inline-code] search current directory and subdirectories[#grep-current-directory-and-subdirectories]

To search within the current directory and all subdirectories, you can use the recursive [.inline-code]-r[.inline-code] flag as follows:

As you can see, instead of stating that [.inline-code]model1[.inline-code] and [.inline-code]model2[.inline-code] are subdirectories, they have been searched and two results have appeared in files within each.

[#grep-r-vs-R][.inline-code]grep -r[.inline-code] is different from [.inline-code]grep -R[.inline-code][#grep-r-vs-R]

Note that the [.inline-code]-r[.inline-code] flag is slightly different from the [.inline-code]-R[.inline-code] flag. While [.inline-code]-r[.inline-code] searches all files that are present in the current directory and all subdirectories, [.inline-code]-R[.inline-code] will also follow symbolic links to go to the original file.

[#grep-specific-directory][.inline-code]grep[.inline-code] search specific directory[#grep-specific-directory]

If you want to recursively search a specific directory instead of the current one, you can replace the wildcard character with the name of the directory you want to search:

[#grep-multiple-files][.inline-code]grep[.inline-code] across multiple files[#grep-multiple-files]

If you are wanting to match a search string across multiple files that you know the name of, the [.inline-code]grep[.inline-code] command takes the form:

Читайте также:  Настройка libvirt astra linux

After the string you want to match, you specify the individual files separated by a space character.

If you don’t know the actual file names but instead you know the file extension, you can use the wildcard character ([.inline-code]*[.inline-code]) to specify all files with the given extension. This command would take the form:

Which searchers all files in the current directory with the given file extension. Note that this could also be used to search all files with the same name but different extensions by changing where the wildcard character appears.

[#grep-all-files][.inline-code]grep[.inline-code] search all files[#grep-all-files]

To search all files, you can run the commands identified above but from the root of your system. This is not recommended as you would get the results from folders that aren’t relevant to your search, such as your configuration settings. Instead, navigate to the root of where would be useful to search, such as [.inline-code]/home[.inline-code] or [.inline-code]/usr[.inline-code] or [.inline-code]/etc[.inline-code] and then run the grep command with the recursive search flag ([.inline-code]-r[.inline-code]).

[#grep-modifiers-multiple-files]Modifiers to the [.inline-code]grep[.inline-code] command across multiple files[#grep-modifiers-multiple-files]

[#grep-exclude-directories][.inline-code]grep[.inline-code] exclude directories[#grep-exclude-directories]

In some cases you may want to exclude certain directories from your search. In this case you add the [.inline-code]—exclude-dir[.inline-code] flag to the command:

This can also be modified to take a list of directories by replacing [.inline-code]dir[.inline-code] with [.inline-code][.inline-code].

[#grep-counting-multiple-files][.inline-code]grep[.inline-code] counting across multiple files[#grep-counting-multiple-files]

To match across multiple files and count the occurrences of the pattern you are searching for, you can use the following command, which will print out the occurrences as well as the file they appear in:

 $ grep -0 “string”  | cut -d ‘:’ -f 1 | uniq -c

[#grep-ignore-case][.inline-code]grep[.inline-code] ignore case[#grep-ignore-case]

The [.inline-code]grep[.inline-code] command is case sensitive. To ignore the case you can use the [.inline-code]-i[.inline-code] flag. For example, when searching for the [.inline-code]hello[.inline-code] string, the [.inline-code]grep[.inline-code] command will also match [.inline-code]Hello[.inline-code] and [.inline-code]hellO[.inline-code] within a file.

[#grep-return-line-number][.inline-code]grep[.inline-code] return line number[#grep-return-line-number]

When matching across multiple files you might want to print off the line number along with the output. In that case you can add the [.inline-code]-n[.inline-code] flag to your command.

[#grep-show-file-name][.inline-code]grep[.inline-code] show file name[#grep-show-file-name]

In some cases, given that a lot of files will be searched which may contain multiple matches, you only want to print out the file name. For this you can add the [.inline-code]-l[.inline-code] flag which stands for “show the file name, not the result itself”.

[#more-on-grep]Find out more about [.inline-code]grep[.inline-code][#more-on-grep]

As always if you want to find out more about how to use the [.inline-code]grep[.inline-code] tool you can use:

Which will print out all the options with explanations. Or:

Which will print out a short page of all the available options.

Источник

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