
-h - output the line itself, without the line number or file.-n - returns the line number, but doesn't work with -l.For example, if we search for 'html', then somehtmltext would not match. When we write -rl, this means essentially -r -l, which means search recursively, and return only the file name.īelow is a list of all grep options or switches, which you can add to your query to get the results you need: When we say -r, for example, we mean 'recursive' - i.e. You can string other options together, to get different results.

#LINUX FIND FILE NAME CONTAINING STRING HOW TO#
views -e 'html' How To Use Grep on Linux/Mac Without it just the file name appears: printf "%s" ABC*Īssuming you run the command within the directory in which the files exist.Grep -r. "/path/to/files/" remains in the output if you entered it that way when you ran the 'printf' command. If you need line breaks after each instance: printf "%s\n" /path/to/files/ABC* Returns: /path/to/files/ABC /path/to/files/ABC123 If you going to use this in a script the output of 'printf' will not contain a new line character until the end of the output stream as such: printf "%s" /path/to/files/ABC* In that case using 'printf' with glob pattern matching is considered safe. From the command line using 'ls' in place of 'printf' here is a safe alternative however, depending on who's opinion you agree with, 'ls' is not safe for use in a script. This will match all occurrences of files starting with "ABC" such as "ABC", "ABC.txt", "ABC123", but not "xABC". This is glob pattern matching which is anchored at both ends.

Regex is an extremely powerful searching tool if you master it, and there are sites such as this which teach you about it in more depth, but note that grep is not a full-fledged regex engine and you can't do everything with it. Now the pattern itself is written in a particular syntax called regular expression, or regex for short.

Generally, if you want to just list them, you can do it in a terminal using: There are many ways to do it, depending on exactly what you want to do with them.
