this post was submitted on 18 Jan 2024
18 points (87.5% liked)

No Stupid Questions

35701 readers
1062 users here now

No such thing. Ask away!

!nostupidquestions is a community dedicated to being helpful and answering each others' questions on various topics.

The rules for posting and commenting, besides the rules defined here for lemmy.world, are as follows:

Rules (interactive)


Rule 1- All posts must be legitimate questions. All post titles must include a question.

All posts must be legitimate questions, and all post titles must include a question. Questions that are joke or trolling questions, memes, song lyrics as title, etc. are not allowed here. See Rule 6 for all exceptions.



Rule 2- Your question subject cannot be illegal or NSFW material.

Your question subject cannot be illegal or NSFW material. You will be warned first, banned second.



Rule 3- Do not seek mental, medical and professional help here.

Do not seek mental, medical and professional help here. Breaking this rule will not get you or your post removed, but it will put you at risk, and possibly in danger.



Rule 4- No self promotion or upvote-farming of any kind.

That's it.



Rule 5- No baiting or sealioning or promoting an agenda.

Questions which, instead of being of an innocuous nature, are specifically intended (based on reports and in the opinion of our crack moderation team) to bait users into ideological wars on charged political topics will be removed and the authors warned - or banned - depending on severity.



Rule 6- Regarding META posts and joke questions.

Provided it is about the community itself, you may post non-question posts using the [META] tag on your post title.

On fridays, you are allowed to post meme and troll questions, on the condition that it's in text format only, and conforms with our other rules. These posts MUST include the [NSQ Friday] tag in their title.

If you post a serious question on friday and are looking only for legitimate answers, then please include the [Serious] tag on your post. Irrelevant replies will then be removed by moderators.



Rule 7- You can't intentionally annoy, mock, or harass other members.

If you intentionally annoy, mock, harass, or discriminate against any individual member, you will be removed.

Likewise, if you are a member, sympathiser or a resemblant of a movement that is known to largely hate, mock, discriminate against, and/or want to take lives of a group of people, and you were provably vocal about your hate, then you will be banned on sight.



Rule 8- All comments should try to stay relevant to their parent content.



Rule 9- Reposts from other platforms are not allowed.

Let everyone have their own content.



Rule 10- Majority of bots aren't allowed to participate here.



Credits

Our breathtaking icon was bestowed upon us by @Cevilia!

The greatest banner of all time: by @TheOneWithTheHair!

founded 1 year ago
MODERATORS
 

Hi friends! ๐Ÿค“ I am on a gnulinux and trying to list all files in the active directory and it's subdirectories. I then want to pipe the output to "cat". I want to pipe the output from cat into grep.

Please help! ๐Ÿ˜…

all 21 comments
sorted by: hot top controversial new old
[โ€“] [email protected] 18 points 9 months ago (2 children)
[โ€“] [email protected] 3 points 9 months ago

Seconded, but I always have to look up the syntax. --type=file --name="string"?

[โ€“] [email protected] 1 points 9 months ago

Thank you! ๐Ÿคฉ

[โ€“] [email protected] 13 points 9 months ago (2 children)

Note, you almost never have to use cat. Just leaving it out would have been enough to find your file (although find is still better).

When you want to find a string in a file it's also enough to use grep string file instead of cat file | grep string. You can even search through multiple files with grep string file1 file2 file* and grep will tell you in which file the string was found.

[โ€“] litchralee 12 points 9 months ago (1 children)
[โ€“] [email protected] 3 points 9 months ago

for a moment, I thought OP was looking for cat photos or something.

[โ€“] [email protected] 3 points 9 months ago (1 children)

So I could use something like grep string -R * to find any occurrence of the string in any files in the folder and sub-folders.

thank you!

[โ€“] [email protected] 6 points 9 months ago* (last edited 9 months ago) (1 children)

grep -r string .

The flag should go before the pattern.

-r to search recursively, . refers to the current directory.

Why use . instead of *? Because on it's own, * will (typically) not match hidden files. See the last paragraph of the 'Origin' section of: https://en.m.wikipedia.org/wiki/Glob_(programming). Technically your ls command (lacking the -a) flag would also skip hidden files, but since your comment mentions finding the string in 'any files,' I figured hidden files should also be covered (the find commands listed would also find the hidden files).

EDIT: Should have mentioned that -R is also recursive, but will follow symlinks, where -r will ignore them.

[โ€“] [email protected] 7 points 9 months ago (2 children)

To answer your og question since it is a valuable tool to know about, xargs.

ls | xargs cat | grep print

Should do what you want. Unless your file names have spaces, then you should probably not use this.

[โ€“] [email protected] 6 points 9 months ago* (last edited 9 months ago)

find -print0 | xargs -0 can handle spaces

Edit and you probably want xargs --exec instead of piping after

[โ€“] [email protected] 2 points 9 months ago
[โ€“] [email protected] 3 points 9 months ago

ripgrep does exactly what you want

[โ€“] [email protected] 2 points 9 months ago* (last edited 9 months ago) (1 children)

grep -r print .

I.e. Grep on print recursively from . (current directory)

Or for more advance search find . -name "*.sh" -exec grep -H print {} \;

I.e find all files with sh extension and run grep on it ({} become the filename). -H to include filename in output.

[โ€“] [email protected] 1 points 9 months ago

this is great ty!

[โ€“] [email protected] 2 points 9 months ago (1 children)

It's valuable to learn how to do an inline loop

ls | while read A; do cat $A | grep print; done

This will read each line of ls into variable A, then it'll get and grep each one.

[โ€“] [email protected] 1 points 9 months ago
[โ€“] [email protected] 2 points 9 months ago

I just pipe to more and filter with /

[โ€“] [email protected] 1 points 9 months ago

I think you can just do grep print **/*.