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! 😅
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! 😅
23 Comments
geekworking@lemmy.world · 23 pts · 2y
Use
findinstead.caseyweederman@lemmy.ca · 5 pts · 2y
Seconded, but I always have to look up the syntax. --type=file --name="string"?
sighofannoyance@lemmy.world · 2 pts · 2y
Thank you! 🤩
JTskulk@lemmy.world · 17 pts · 2y
Bro you can just run
findsighofannoyance@lemmy.world · 4 pts · 2y
thanks dude
bjoern_tantau@swg-empire.de · 16 pts · 2y
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 fileinstead ofcat file | grep string. You can even search through multiple files withgrep string file1 file2 file*and grep will tell you in which file the string was found.litchralee@sh.itjust.works · 13 pts · 2y
Obligatory link to the Useless Use of Cat Awards
FuglyDuck@lemmy.world · 3 pts · 2y
for a moment, I thought OP was looking for cat photos or something.
sighofannoyance@lemmy.world · 4 pts · 2y
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!
JoeyJoeJoeJr@lemmy.ml · 6 pts · 2y
grep -r string .The flag should go before the pattern.
-rto 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 yourlscommand (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 (thefindcommands listed would also find the hidden files).EDIT: Should have mentioned that
-Ris also recursive, but will follow symlinks, where-rwill ignore them.notfromhere@lemmy.ml · 2 pts · 2y
TIL
Shadow@lemmy.ca · 15 pts · 2y
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.
018118055@sopuli.xyz · 6 pts · 2y
find -print0 | xargs -0 can handle spaces
Edit and you probably want xargs --exec instead of piping after
sighofannoyance@lemmy.world · 4 pts · 2y
thank you
shrugal@lemm.ee · 5 pts · 2y
I think you can just do
grep print **/*.sighofannoyance@lemmy.world · 1 pts · 2y
ty
surewhynotlem@lemmy.world · 4 pts · 2y
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.
sighofannoyance@lemmy.world · 1 pts · 2y
thank you
muix@lemmy.sdf.org · 3 pts · 2y
ripgrepdoes exactly what you wantvisnae@lemmy.world · 2 pts · 2y
grep -r print .I.e. Grep on
printrecursively 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).-Hto include filename in output.sighofannoyance@lemmy.world · 1 pts · 2y
this is great ty!
notfromhere@lemmy.ml · 2 pts · 2y
sighofannoyance@lemmy.world · 1 pts · 2y
ty
Toes@ani.social · 2 pts · 2y
I just pipe to
moreand filter with /