Linux tools you NEED TO KNOW. FIND && GREP #linux #linuxcommands #programming #coding

preview_player
Показать описание

The find command is amazing, and when paired with grep, it's a superpower!
Рекомендации по теме
Комментарии
Автор

Very cool nerd but i think you can do that with just grep

UrAdversary
Автор

yeah like others pointed out this is possible with just grep -r (or rg -r if you're using ripgrep, which is better). But this does let you find strings in certain files, which is nice. You can also do the same with ripgrep by combining rg --files with regular rg.

hassan
Автор

Just do: "grep -rin" for Recursive, Ignore-case, Number of the matching line.

Автор

Ripgrep is literally built for that

It's used by one of the biggest neovim plugin, telescope, so it's most definitely good

no_name
Автор

Why not just "grep -r catppuchin *"? This will also print the filename(s) where the matches occoured.

If you really want to use find, don't use the "-exec" argument, but pipe the result into "xargs grep".
Advantage over "find -exec": it doesn't start a grep process for every single file.
Advantage over "grep *": it also works if the directory contains a huge amount of files, which might exceed the maximum command line length.

stephanweinberger
Автор

One issue is that you are going to create a new grep process for each file matched by the find command. You probably want to use '+' instead of ';' to allow each {} to be replaced by multiple matches (up to the maximum allowed by the system).

cynodont
Автор

To also print the file path (might want to know the file the match is in, not just the match):

find <dir> -type f -print0 | xargs -0n1 -I% bash -c 'echo grep -P "some.*regex" "%"'

Note that this will print e.g. even when there are no matches inside, but since you also see the matches it’s not a problem. Also, the "-print0" and "-0" flag mean that this command will work even if the path contains whitespaces!

AnthonyDentinger
Автор

I love his tutorial videos! They are amazing and educational!!! I wish I was his student, from Brazil.

ElderManhaniniFouraux
Автор

A more efficient way, if you only use the standard tools, is:

find . -type f -print0 | xargs -0 grep XXX

This cuts down on the number of times grep is executed by having xargs batch together as many arguments as possible.

The -print0 and -0 on xargs cause the output to be zero delimited (NULL) so it won’t have issues with whitespace.

Well, grep -r is more efficient if you only search for -type f, but this helps a lot of you add other predicates before the -print0, such as file name, size, modification time etc.

thorhallursverrisson
Автор

Lots of recommendations for grep -r and ripgrep. I just want to add git grep to the list. Recursive by default, searches just tracked files, and can search commits that aren't checked out.

infilt
Автор

find . -type f -exec grep -Hin something {} \;

ignore case, show line numbers, and file names. You can spruce up the find command with -name '*py' to only look for python files in current and all directories underneath.

alphmega
Автор

Another great video!
*Awesome replies with all the additional tools and options everyone is throwing in!!*

As an intermediate Linux user trying to transition from Windows to Linux ( I run Garuda btw :D ) these videos and replies are extremely useful. Thank you to everyone!

Rood
Автор

Very good! If you need the names of the files, you can add to the grep the option - H

davidhirschhorn
Автор

I would think both \; and ';' should work equally in bash and tcsh to protect the semicolon for find(1). And as others have written, `grep -r <search_string> <dir>` works equally in this case. I only use find(1) with -exec if I need to search files based on time stamps..

gregercolano
Автор

"Catppuccin" is such an elegant word

MayankYdvv
Автор

Add ‘-print’ to print the file it was found in. There are also grep options to do this, but these print the file name on every line.

tony-x-smith
Автор

Another use for find with -exec is to remove files above a certain age. We do this at my job when needing to clear space for core services (it will delete stale logs over 90 days old). Also useful in cron jobs or via systemd timers.

MagnumCarta
Автор

That’s a backslash, not a forward slash 😢

thekillingspoon
Автор

This dude is me when i learn something on the most basic level and think im an expert on it, only for me from 2 weeks in the future to come back in time and strangle me to death out of shame

dumbl
Автор

In most cases I find it useful to put the filename alongside the matches. You could have included that, but maybe it's in the full video. That you've linked?

PostNoteIt
welcome to shbcf.ru