How to search for both within filename (find) and file (grep) using a single line of code?
Solution 1
You're right - you can use semicolon to run list of commands sequentially:
$ find . -name 'test'; grep test *
You can also group it to run as single command: http://www.gnu.org/software/bash/manual/html_node/Command-Grouping.html
Solution 2
Try this in your terminal(CTRL+ALT+T)
find <here your starting string > -type f -exec grep -l <here place the part of file name> {} \;
Description of the above command:
find snow -type f
-> find all files starting from snow.
find .... -exec
-> for each item found, do the following command. Where you normally place the filename in you command, put {} .
grep -l "String" something
-> returns the filename if string "String" is found in file something.
Credit:dbasupport.com
Related videos on Youtube
nutty about natty
Updated on September 18, 2022Comments
-
nutty about natty over 1 year
I'm pretty sure I've done this in the past (and that an answer is out there somewhere), but can't remember right now.
I'm looking for a one-liner to search for (and return / echo) matches in both the filename and file.
Probably the easiest would be to run
grep
andfind
separately after each other; but I'd prefer a one-liner......unless it were possible with a semicolon (?) or other to run these tools sequentially.
I don't think the solution I'm after would involve piping | because, to borrow from set theory, I'm interested in the union of the two sets A and B (A ∪ B), rather than their
intersection(A ∩ B).-
nutty about natty about 11 yearsmeta: should this question be migrated to a different SE site, e.g. stackoverflow or superuser ?
-
-
nutty about natty about 11 yearsTo borrow from set theory, I'm interested in the union of the two sets A and B (A ∪ B), rather than their intersection (A ∩ B) -- as I tried to make clear in my question above...