Your comment on this question: Show
Your comment on this answer:
Your comment on this answer:Related Questions In Linux Administrationfind . -type f -exec grep -l check {} + You probably don't want to use the -R option which with modern versions of GNU grep follows symlinks when descending directories. Use the -r option instead there which since version 2.12 (April 2012) no longer follows symlinks. If your grep is not the GNU one, or is older than version 2.12, or if you need your code to be portable to non-bleeding-edge-GNU systems, use the find command above. Otherwise, you can do: grep -rl check .Don't use * (as that would omit hidden files in the current directory (and in the current directory only) and would cause problems for files whose name starts with a -), avoid passing options after arguments as that's not guaranteed to work depending on the environment and is not portable. When you have to find a file in Linux, it’s sometimes not as easy as finding a file in another operating system. This is especially true if you are running Linux without a graphical user interface and need to rely on the command line. This article covers the basics of how to find a file in Linux using the CLI. The find command in Linux is used to find a file (or files) by recursively filtering objects in the file system based on a simple conditional mechanism. You can use the find command to search for a file or directory on your file system. By using the -exec flag (find -exec), matches, which can be files, directories, symbolic links, system devices, etc., can be found and immediately processed within the same command. Use find from the command line to locate a specific file by name or extension. The following example searches for *.err files in the /home/username/ directory and all sub-directories: find /home/username/ -name "*.err"find expressions take the following form: find options starting/path expression
Consider the following example command: find -O3 -L /var/www/ -name "*.html"This command enables the maximum optimization level (-O3) and allows find to follow symbolic links (-L). find searches the entire directory tree beneath /var/www/ for files that end with .html.
The default configuration for find will ignore symbolic links (shortcut files). If you want find to follow and return symbolic links, you can add the -L option to the command, as shown in the example above. find optimizes its filtering strategy to increase performance. Three user-selectable optimization levels are specified as -O1, -O2, and -O3. The -O1 optimization is the default and forces find to filter based on filename before running all other tests. Optimization at the -O2 level prioritizes file name filters, as in -O1, and then runs all file-type filtering before proceeding with other more resource-intensive conditions. Level -O3 optimization allows find to perform the most severe optimization and reorders all tests based on their relative expense and the likelihood of their success.
The find command contains the ability to filter a directory hierarchy based on when the file was last modified: find / -name "*conf" -mtime -7 find /home/exampleuser/ -name "*conf" -mtime -3The first command returns a list of all files in the entire file system that end with the characters conf and modified in the last seven days. The second command filters exampleuser user’s home directory for files with names that end with the characters conf and modified in the previous three days. The find command can only filter the directory hierarchy based on a file’s name and metadata. If you need to search based on the file’s content, use a tool like grep. Consider the following example: find . -type f -exec grep "example" '{}' \; -printThis searches every object in the current directory hierarchy (.) that is a file (-type f) and then runs the command grep "example" for every file that satisfies the conditions. The files that match are printed on the screen (-print). The curly braces ({}) are a placeholder for the find match results. The {} are enclosed in single quotes (') to avoid handing grep a malformed file name. The -exec command is terminated with a semicolon (;), which should be escaped (\;) to avoid interpretation by the shell. The -exec option runs commands against every object that matches the find expression. Consider the following example: find . -name "rc.conf" -exec chmod o+r '{}' \;This filters every object in the current hierarchy (.) for files named rc.conf and runs the chmod o+r command to modify the find results’ file permissions. The commands run with the -exec are executed in the find process’s root directory. Use -execdir to perform the specified command in the directory where the match resides. This may alleviate security concerns and produce a more desirable performance for some operations. The -exec or -execdir options run without further prompts. If you prefer to be prompted before action is taken, replace -exec with -ok or -execdir with -okdir. Caution To delete the files that end up matching your search, you can add -delete at the end of the expression. Do this only when you are positive the results will only match the files you wish to delete. In the following example, find locates all files in the hierarchy starting at the current directory and fully recursing into the directory tree. In this example, find will delete all files that end with the characters .err: find . -name "*.bak" -deleteYou may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on Monday, October 25, 2010.
You can use grep tool to search recursively the current folder, like: grep -r "class foo" .Note: -r - Recursively search subdirectories. You can also use globbing syntax to search within specific files such as: grep "class foo" **/*.cNote: By using globbing option (**), it scans all the files recursively with specific extension or pattern. To enable this syntax, run: shopt -s globstar. You may also use **/*.* for all files (excluding hidden and without extension) or any other pattern. If you've the error that your argument is too long, consider narrowing down your search, or use find syntax instead such as: find . -name "*.php" -execdir grep -nH --color=auto foo {} ';'Alternatively, use ripgrep. ripgrepIf you're working on larger projects or big files, you should use ripgrep instead, like: rg "class foo" .Checkout the docs, installation steps or source code on the GitHub project page. It's much quicker than any other tool like GNU/BSD grep, ucg, ag, sift, ack, pt or similar, since it is built on top of Rust's regex engine which uses finite automata, SIMD and aggressive literal optimizations to make searching very fast. It supports ignore patterns specified in .gitignore files, so a single file path can be matched against multiple glob patterns simultaneously. You can use common parameters such as:
Page 2
While you should never replace (or alias) a system command with a different program, due to risk of mysterious breakage of scripts or other utilities, if you are running a text search manually or from your own scripts or programs you should consider the fastest suitable program when searching a large number of files a number of times. Ten minutes to half an hour time spent installing and familiarizing yourself with a better utility can be recovered after a few uses for the use-case you described. A webpage offering a "Feature comparison of ack, ag, git-grep, GNU grep and ripgrep" can assist you to decide which program offers the features you need.
For a combination of speed and power indexed query languages such as ElasticSearch or Solr can be a long term investment that pays off, but not if you want a quick and simple replacement for grep. OTOH both have an API which can be called from any program you write, adding powerful searches to your program. While it's possible to spawn an external program, execute a search, intercept its output and process it, calling an API is the way to go for power and performance.
While some answers suggest alternative ways to accomplish a search they don't explain why other than it's "free", "faster", "more sophisticated", "tons of features", etc. Don't try to sell it, just tell us "why your answer is right". I've attempted to teach how to choose what's best for the user, and why. This is why I offer yet another answer, when there are already so many. Otherwise I'd agree that there are already quite a few answers; I hope I've brought a lot new to the table. |