How to search for both within filename (find) and file (grep) using a single line of code?

6,826

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

Share:
6,826

Related videos on Youtube

nutty about natty
Author by

nutty about natty

Updated on September 18, 2022

Comments

  • nutty about natty
    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 and find 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
      nutty about natty about 11 years
      meta: should this question be migrated to a different SE site, e.g. stackoverflow or superuser ?
  • nutty about natty
    nutty about natty about 11 years
    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) -- as I tried to make clear in my question above...