@vaporeon_ do you know find? purrticularly -exec?
@aescling I keep forgetting what the grep option for showing the filename even if you're grepping only one file is, but apparently it's -H
So if I wanted to find all .c files that contain some_function, I would do:
find . -name '*.c' -exec grep -H some_function {} \;
(Just going off memory, except for the -H option that I had to look up, tell me if I'm misremembering something.)
@vaporeon_ fur using multiple arguments in the same command you just add a + after the {}, e.g., find . -type f -exec grep pattern '{}' +
@aescling Wait. Should I be quoting the {}? I genuinely forgot... (Also, does the command you just wrote not need a \; at the end, after the +?)
@vaporeon_ it's shell syntax, so yes
{ echo hi; cat file } >file2
@aescling Doesn't work for me in Bash, needs to be { echo hi; cat file; } >file2 for it to work... ( echo hi; cat file ) >file2 works, though... Can you tell me what the difference between these and the other brackets is? I don't know...
@vaporeon_ god i keep furgetting when the final semicolon is necessary
{} runs the commands in the current shell; () runs them in a subshell
if you can spare the extra purrocess,
(
cd dir
[...]
)
is a convenient pattern
@aescling What happens if I don't? I just attempted to execute find . -type f -exec grep -H abcd {} \; on a directory containing files that have whitespaces in their names, and it still worked fine...
@vaporeon_ really? i’m pretty sure that should be a syntax error. huh
@aescling Barely 😓
I know
find some-directory -name '*.txt(or-inamefor case-insensitive, also substitute whatever other globbing expression instead of*.txt, just don't forget the quotes), I knowfind -print | less(this is what I use to use to look at a tree of files), I knowfind -exec some-command {} \;, where{}will get substituted with the filename. I've used it withgrepbefore to get a list of files (just the filenames, not all occurrences) that contain a particular string.I vaguely remember there also was some other variant of
execwhere you could specify multiple files? With+or something? But I completely forgot :(