![]() User accounts in Ubuntu are created with an alias that makes grep really run grep -color=auto (run alias grep to see). But you can easily get this with find, too. One of the immediate benefits of using globstar is, by default on Ubuntu, grep will produce colorized output. This is a different output format from the format produced by my method, Zanna's, and muru's. That method finds files and prints their paths, followed by matching lines if any. If there are many files, it might be slower than some other methods, but it works. Sudodus's way runs grep separately for each file.txt found. Like the globstar method covered here, this prints all matching lines, with paths prepended to each. In the case of greping for a string in multiple files, this produces the correct behavior. But if more files are found than can be listed in a single path, the +-terminated -exec action runs the command with some of the paths, then runs it again with some more paths, and so forth. Zanna's way builds and runs a grep command with potentially many path arguments. The methods that use find are not subject to this restriction, because: In practice you'd need (at least) thousands of such files, but it could happen. If you have so many files called file.txt that the resulting command would be too long for the system to execute, then the method above will fail. Sometimes you don't want to recurse such folders, but sometimes you do.Īs with an ordinary glob, the shell builds a list of all matching paths and passes them as arguments to your command ( grep) in place of the glob itself. The method above does not look inside directories whose names start with a. I like globstar, and sometimes it's more convenient, but globstar is not a general alternative to find. Anything you can do with globstar, you can do with the find command too. There are a few practical differences between globstar and find.įind is far more versatile than globstar. (It's safe in this command, which doesn't change any iles.) shopt -u globstar turns the globstar shell option back off. You should be careful with this, since you can run commands that modify or delete far more files than you intend, especially if you write ** when you meant to write *. If followed by a /, two adjacent *s will match only directories and Will match all files and zero or more directories and subdirectories. Pathname expansion context, two adjacent *s used as a single pattern When the globstar shell option is enabled, and * is used in a ![]() Turning on the globstar bash shell option makes ** match paths containing the directory separator ( /). For more information, see Stéphane Chazelas's excellent answer to The result of ls *, ls ** and ls *** on Unix.SE. Although popular shells usually support directory-traversing ** globs, they don't always work the same way. Bash is the default user shell in Ubuntu (and most other GNU/Linux operating systems), so if you're on Ubuntu and don't know what your shell is, it's almost certainly Bash. These instructions are specific to the Bash shell. It is the shell that recurses directories in expanding the glob pattern containing **, and not grep. But don't pass -r or -R when using this method. ![]() You can pass the -a, -i, and -n flags (from your example) to grep as well, if that's what you need. The -H flag makes grep show the filename even if only one matching file is found. But if you want to use just the grep command and your shell, there is another way to do it - you can make the shell itself perform the necessary recursion: shopt -s globstar # you can skip this if you already have globstar turned on And muru has posted an excellent approach of using grep's -include option. Building grep commands with find, as in Zanna's answer, is a highly robust, versatile, and portable way to do this (see also sudodus's answer). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |