The grep (“go to regular expression and print”) command line utility is one of the most important programs in your Unix toolkit, and indispensable when managing your own private server. Grep is a powerful search tool that can help you find patterns in critical files (and directories). It can also search recursively within multiple files to return results based on regular expressions.
- Basics of Grep
- Why Is Grep Returning No Matches?
- Remember Case Sensitivity
- Is It a Directory?
Basics of Grep
When first starting out with grep
it can be confusing to figure out what kind of options and inputs it takes. What most people start out doing is concatenating (with cat
) file contents and piping (|
) then into grep
.
cat file | grep "search pattern"
But this is not actually necessary, because grep
is capable of performing its functions without the extra step of concatenating input.
grep "search pattern" file
However, if your goal is to concatenate (cat
) different files and then search the contents then piping into grep
is not a bad idea. However, you should note that grep can search all files within a directory recursively by using the -r
option. For example:
grep -r "search pattern" /directory/*
It can be frustrating to troubleshoot with grep
when you don’t know why it’s returning no matches. Here are a few troubleshooting steps you can take to isolate the issue.
Remember Case Sensitivity
Don’t forget that case sensitivity is a factor, especially when you are dealing with UNIX-like systems. But often, there may be situations in which you can’t recall the case of the character you are searching for, or you want to capture all instances of a word or string of text regardless of case. In that case, you should use the -i
option. For example:
grep -i "<case insensitive search pattern>" file
Is It a Directory?
When in doubt, check the error you are getting and try to work through what it is trying to communicate to you. Take a look at the following example. The first line of the example is the command, the second line is the error it generates:
[email protected]:~$ grep "search" ~/Downloads/ grep: /home/chris/Downloads/: Is a directory
The first part of the error merely prints out “grep” to let you know there is an error with grep
itself. Then it prints out the directory, followed by “Is a directory.” This error indicates that you are trying to run grep
on a directory. Grep is not meant to work that way. Grep can perform search operations on input, for example, when you are piping into, or it works upon a file or series of files as such.
If your intention in the above command example was to search instead for patterns within files inside of the “Downloads” directory, then all you need to do is add the *
in front of the directory path, as in the following example:
grep "search" ~/Downloads/*
To put the full command and output in context:
[email protected]:~$ grep "search" ~/Downloads/* search pattern
Notice now that the grep
output has come back with a match. Remember you can always find complete Linux tutorials available in the support center.
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:
grep search_term *
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:
grep -r search_term .
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.
grep simple *
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.
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 -r simple .
Here’s the result:
There is also a -R
option for recursive search and it works almost the same as the -r
option.
grep -R simple .
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:
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={dir1,dir2} serach_term directory_path
Here’s what excluding directories look like in our example here:
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:
10 Practical Grep Command Examples for Developers
The grep command is used to find patterns in files. This tutorial shows some of the most common grep command examples that would be specifically beneficial for software developers.
Linux HandbookSylvain Leroux
Let me know if you have any questions or suggestions on this topic.
Creator of Linux Handbook and It’s FOSS. An ardent Linux user & open source promoter. Huge fan of classic detective mysteries from Agatha Christie and Sherlock Holmes to Columbo & Ellery Queen.
Универсальная команда grep позволяет выполнять поиск текста во всех файлах и всех подкаталогах каталога.
Grep – это отличный инструмент, когда вам нужно выполнить поиск по содержимому файла.
Обычно вы запускаете grep на одном файле, например, так:
grep search_term filename
Grep достаточно универсален.
Если вы хотите найти все файлы в каталоге с помощью grep, используйте его следующим образом:
grep search_term *
С этим есть проблема.
Он ищет только во всех файлах в текущем каталоге.
Он не будет искать в подкаталогах.
Вы можете заставить grep искать во всех файлах и во всех подкаталогах текущего каталога, используя опцию -r рекурсивного поиска:
grep -r search_term .
Вы также можете указать путь к каталогу, если вы находитесь не в том каталоге, в котором хотите выполнить поиск:
grep -r search_term directory_path
Это был краткий обзор.
Позвольте мне показать вам все это в деталях с соответствующими примерами, чтобы вам было легче понять.
Поиск Grep во всех файлах каталога
Вот структура каталогов, которую я собираюсь использовать в этом примере.
За исключением файла empty.txt, все файлы содержат термин ‘simple’, по которому я буду выполнять поиск grep.
~/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
Для поиска слова ‘simple’ во всех файлах текущей директории достаточно использовать подстановочный знак (*).
grep simple *
Это приведет к поиску во всех файлах в текущих каталогах, но не зайдет в подкаталоги.
Поскольку вы не можете напрямую искать grep в каталоге, система выдаст ошибку “XYZ is a directory” вместе с результатами поиска.
Если вы находитесь не в том же каталоге, в котором хотите выполнить, вы можете указать путь к каталогу и закончить его символом /*
grep search_term directory_path/*
По сути, вы используете * для расширения всех элементов (файлов и каталогов) данного каталога.
Теперь, когда вы это знаете, давайте посмотрим, как можно выполнить рекурсивный поиск с помощью grep, чтобы он также просматривал файлы в подкаталогах.
Рекурсивный поиск Grep во всех подкаталогах каталога
Grep предоставляет опцию -r для рекурсивного поиска.
С этой опцией grep будет искать все файлы в текущем (или указанном) каталоге, а также все файлы во всех подкаталогах.
Вот рекурсивный поиск, который я выполнил в предыдущем примере:
grep -r simple .
Существует также опция -R для рекурсивного поиска, и она работает почти так же, как опция -r.
grep -R simple .
Итак, в чем разница между grep -r и grep -R?
На самом деле, только одна.
Поиск -R является поиском с разыменованием, что означает, что он будет следовать по символическим ссылкам, чтобы перейти к исходному файлу (который может находиться в какой-то другой части системы).
Если вы находитесь не в том каталоге, в котором хотите выполнить рекурсивный поиск, просто укажите команде grep абсолютный или относительный путь к каталогу:
grep -r search_term path_to_directory
Бонусный совет: Как исключить определенный каталог из рекурсивного поиска grep
Все вроде бы хорошо, но что если вы хотите исключить определенный каталог из рекурсивного поиска?
Для этого тоже есть соответствующая возможность.
Я уже говорил вам, что grep – чрезвычайно универсальная команда.
grep -r --exclude-dir=dir_name serach_term directory_path
Это еще не все.
Вы можете исключить из рекурсивного поиска более одного подкаталога следующим образом:
grep -r --exclude-dir={dir1,dir2} serach_term directory_path
Подведем итоги
Вот краткий обзор использования поиска grep для нескольких файлов и каталогов:
Команда Grep | Описание |
---|---|
grep string * | Поиск во всех файлах в текущем каталоге |
grep string dir | Поиск во всех файлах в каталоге dir |
grep -r string . | Рекурсивный поиск во всех файлах во всех подкаталогах |
grep -r string dir | Рекурсивный поиск во всех файлах во всех подкаталогах каталога dir |
grep -R string . | То же, что и r, но следует за символическими ссылками |
см. также:
- 🧾 Как скачать, установить и использовать GNU Grep в Windows?
- ngrep – Сетевой анализатор пакетов для Linux