Since many files contain database references, you might get a lot of data on output, so if you are only interested in all the files containing matching text, you can use the grep -l option. This option of grep only shows filenames that contain matching text.
If you are using GNU grep, then you can use the following: grep -ir --include "*. cpp" "xyz" . The command above says to search recursively starting in current directory ignoring case on the pattern and to only search in files that match the glob pattern "*.
-H, --with-filename Print the file name for each match. This is the default when there is more than one file to search. l is just to list the name of the files.
Linux or UNIX-like system use the ls command to list files and directories. However, ls does not have an option to list only directories. You can use combination of ls command, find command, and grep command to list directory names only. You can use the find command too.
You need to use find instead of grep in this case.
You can also use find in combination with grep or egrep:
$ find | grep "f[[:alnum:]]\.frm"
Example
find <path> -name '*FileName*'
From manual:
find -name pattern
Base of file name (the path with the leading directories removed) matches shell pattern pattern. Because the leading directories are removed, the file names considered for a match with -name will never include a slash, so "-name a/b" will never match anything (you probably need to use -path instead). The metacharacters ("*", "?", and "[]") match a "." at the start of the base name (this is a change in find‐ utils-4.2.2; see section STANDARDS CONFORMANCE below). To ignore a directory and the files under it, use -prune; see an example in the description of -path. Braces are not recognised as being special, despite the fact that some shells including Bash imbue braces with a special meaning in shell patterns. The filename matching is performed with the use of the fnmatch(3) library function. Don't forget to enclose the pattern in quotes in order to protect it from expansion by the shell.
As Pablo said, you need to use find instead of grep, but there's no need to pipe find to grep. find has that functionality built in:
find . -regex 'f[[:alnum:]]\.frm'
find is a very powerful program for searching for files by name and supports searching by file type, depth limiting, combining different search terms with boolean operations, and executing arbitrary commands on found files. See the find man page for more information.
You can find the relative path of a file using tree. Just pipe the output to grep to filter down:
tree -f | grep filename
Here is a function you can put into your .bash_profile, .bashrc, .zshrc or other...
findfile(){ tree -f | grep $1; } # $1 = filename, -f is full path
The easiest way is
find . | grep test
Here find will list all the files in the (.), i.e., the current directory, recursively.
And then it is just a simple grep. All the files which name has "test" will appear.
You can play with grep as per your requirement. Note: As the grep is a generic string classification. It can result in giving you not only file names. But if a path has a directory ('/xyz_test_123/other.txt') it would also be part of the result set.
find -iname "file_name"
Syntax:
find -type type_descriptor file_name_here
type_descriptor types:
f: regular file
d: directory
l: symbolic link
c: character devices
b: block devices
You can also do:
tree | grep filename
This pipes the output of the tree command to grep for a search. This will only tell you whether the file exists though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With