Recursive search text linux

How can I do a recursive find/replace of a string with awk or sed?

oh my god this is exactly what I just did. But it worked and doesn’t seem to have done any harm. Whats the worst that could happen?

Quick tip for all the people using sed: It will add trailing newlines to your files. If you don’t want them, first do a find-replace that won’t match anything, and commit that to git. Then do the real one. Then rebase interactively and delete the first one.

You can exclude a directory, such as git, from the results by using -path ./.git -prune -o in find . -path ./.git -prune -o -type f -name ‘*matchThisText*’ -print0 before piping to xargs

37 Answers 37

find /home/www \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i 's/subdomainA\.example\.com/subdomainB.example.com/g' 

-print0 tells find to print each of the results separated by a null character, rather than a new line. In the unlikely event that your directory has files with newlines in the names, this still lets xargs work on the correct filenames.

\( -type d -name .git -prune \) is an expression which completely skips over all directories named .git . You could easily expand it, if you use SVN or have other folders you want to preserve — just match against more names. It’s roughly equivalent to -not -path .git , but more efficient, because rather than checking every file in the directory, it skips it entirely. The -o after it is required because of how -prune actually works.

For more information, see man find .

Источник

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:

Читайте также:  Linux подтверждение выполнения команды

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:

Читайте также:  Файл сервер linux debian

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.

Источник

Recursive String Search in Linux Command Line

Recursive search is a process by which you search for a specific string in a directory and all its subdirectories. This technique can be particularly useful when you need to search through large and complex file structures, where a simple search may not be sufficient. With recursive search, you can search for strings in all files within a directory, regardless of their location or type, making it an ideal solution for complex search tasks.

There are several ways to perform a recursive search in the Linux command line, and one of the most popular is the use of the ‘grep’ command.

Recursive String Search With grep Command

The grep command is a versatile tool that allows you to search for strings in files and directories, and it can be used in conjunction with other commands to perform more complex searches. To perform a recursive search with grep, you can use the ‘-r’ option, which tells grep to search for strings in all files and subdirectories within a directory.

For example, to search for the string “example” in all files within the current directory and its subdirectories, you would use the following command:

Note that the ‘.’ at the end of the command tells grep to start the search in the current directory. You can replace this with the path of any other directory you wish to search.

Recursive String Search With find Command

Another useful tool for recursive search is the find command. The find command allows you to search for files based on various criteria, including file name, type, and modification date. You can also use the find command to perform a recursive search for strings by using the ‘-exec’ option in conjunction with the ‘grep’ command.

Читайте также:  Sles linux appliance update blocked

For example, to search for the string “example” in all files within the current directory and its subdirectories, you would use the following command:

find . -type f -exec grep "example" <> \; 

Note that the ‘.’ at the beginning of the command tells find to start the search in the current directory, while the ‘-type f’ option restricts the search to files only. The ‘-exec’ option allows you to execute a command on each file found by find, in this case the ‘grep’ command.

Wrap Up

In conclusion, recursive search is a powerful technique for searching for strings in Linux command line. Whether you’re using the grep or find command, this technique allows you to search through large and complex file structures with ease, making it an ideal solution for complex search tasks. With the ability to search for strings in all files within a directory, regardless of their location or type, recursive search is a valuable tool for streamlining your workflow and maximizing your productivity in the Linux command line.

Источник

How to use «grep» command to find text including subdirectories

I want to find all files which contain a specific string of text. The grep command works, but I don’t know how to use it for every directory (I can only do it for my current directory). I tried reading man grep , but it didn’t yield any help.

grep -RIn * Will search from current directories down in all text files. Not sure how to do my search recursively in file patterns like *.C with only grep

Use the find and grep combination to recursively search files for a string in current and all sub directories. Check this wilddiary.com/find-files-containing-my-text

12 Answers 12

It would be better to use

  • -r (or —recursive ) option is used to traverse also all sub-directories of /path , whereas
  • -l (or —files-with-matches ) option is used to only print filenames of matching files, and not the matching lines (this could also improve the speed, given that grep stop reading a file at first match with this option).

Actually if «string» is a text pattern to find, it’s better to use that functionality, otherwise someone can face problems when the string contains dot or special character which has meaning in regular expressions and not just a dot which should be found as a string, as-is. Then I would use -rlF switches, -F for «fixed string» (and not regexp — for example). Of course, if the task was using regexps, then excuse me. Sure, the same theory without -r too, I often see that people assumes grep searches «text» and it can cause problems which special ones which mean something as a regexp.

Источник

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