4 ms·
> In Grep grep -R 'some term' * , in Ack ack 'some term'. The -R tells grep to search through all directories and subdirectories while the * tells it what types
by ralph 14y ago
> In Grep grep -R 'some term' * , in Ack ack 'some term'. The -R tells grep to search through all directories and subdirectories while the * tells it what types of files.
No, that's not how it works with grep. The shell is expanding the glob * to all the things in the current directory that don't start with a dot and that's what grep sees. It then reads each of them in turn or if they're a directory it descends into them recursively; the * plays no further part in the descent.
- dhimes 14y agoI was under the impression that * expanded to all things not starting with a dot in all directories-- or better: in each directory that -R dictates. I understand that "* tells it what types of files," isn't correct, but I don't understand what you mean by "plays no further part in the descent."
- akkartik 14y ago$ mkdir x $ cd x $ echo aa > .y $ mkdir y $ echo aa > y/z $ echo aa > y/.z $ grep -r a * y/.z:aa y/z:aa Notice that hidden files in the top-level (.y) don't show up, but those in subdirectories (y/.z) do. It's kind of a gotcha. Fortunately one rarely finds dotfiles in subdirs.
- dhimes 14y agoholy crap - I didn't realize this. Thanks!
- noselasd 14y agojust run echo * in your directory. That's the same argument grep will get(instead of the * ) when you pass it * . grep will run through all those files, if any, and recurse through all the directories, if any. grep recursing down a directory will process all the files/subdirs in that directory. (which includes . files). If the shell can't expand the * , grep will receive a literal * and try to open a file/directory named * .
- deleted 14y ago[deleted]
- ralph 14y agoPersonally, I `shopt -s failglob' in my .bashrc so any glob that fails to expand is treated as an error by the shell and the command never gets started. I then quote all special globbing characters that I intend to pass to the command as it avoids the sometimes hard to spot errors where a glob expanded unintentionally and the command didn't see the wildcards. Relying on non-matching globbing to reach the command will bite one day.
- ralph 14y agoWhat do you think is doing the expansion of the asterisk? It's the shell, not grep, and is called globbing. http://en.wikipedia.org/wiki/Globbing http://en.wikipedia.org/wiki/Globbing The reason there are files that start with a dot is that ls(1) and globs starting with an asterisk ignore them by default. ls needs -a (all) to show them. Some shells, but not bash, provide a globbing syntax for the recursive expansion you think you require here. But if, as normal, you want to grep recursively down from the current directory then that's what you should specify; just pass grep one path to search, dot, the current directory. grep -r foo . This is very basic knowledge of the Unix command line for a programmer and I highly recommend the classic text _The Unix Programming Environment_ by Kernighan and Pike to learn the philosophy behind Unix. It has nothing on window systems, ssh, etc., nor should it have as that's modern-day noise that hides the essence. It and Kernighan and Ritchie's _The C Programming Language_ were the key books every starting-out Unix programmer read. http://www.amazon.com/exec/obidos/ASIN/013937681X/mqq-20 http://www.amazon.com/exec/obidos/ASIN/013937681X/mqq-20