How can I find a specific file from a Linux terminal?
Find from root path find / -name "index.html"
Find from current path find . -name "index.html"
How to find all files containing specific text (string) on Linux
Do the following:
grep -rnw '/path/to/somewhere/' -e 'pattern'
-r
or-R
is recursive,-n
is line number, and-w
stands for match the whole word.-l
(lower-case L) can be added to just give the file name of matching files.-e
is the pattern used during the search
Along with these, --exclude
, --include
, --exclude-dir
flags could be used for efficient searching:
- This will only search through those files which have .c or .h extensions:
grep --include=\*.{c,h} -rnw '/path/to/somewhere/' -e "pattern"
- This will exclude searching all the files ending with .o extension:
grep --exclude=\*.o -rnw '/path/to/somewhere/' -e "pattern"
- For directories it's possible to exclude one or more directories using the
--exclude-dir
parameter. For example, this will exclude the dirs dir1/, dir2/ and all of them matching *.dst/:
grep --exclude-dir={dir1,dir2,*.dst} -rnw '/path/to/somewhere/' -e "pattern"
This works very well for me, to achieve almost the same purpose like yours.
For more options, see man grep
.
Find specific files in a directory through linux terminal
use find. Specify path to your x directory, specify that you want to find only files (type-f) and list several masks for files you want with -name:
find /path/to/x_directory -type f -name "*y*" -name "*z*"
How to search for a file in the CentOS command line
Try this command:
find / -name file.look
How can I find a file/directory that could be anywhere on linux command line?
"Unfortunately this seems to only check the current directory, not the entire folder". Presumably you mean it doesn't look in subdirectories. To fix this, use find -name "filename"
If the file in question is not in the current working directory, you can search your entire machine via
find / -name "filename"
This also works with stuff like find / -name "*.pdf"
, etc. Sometimes I like to pipe that into a grep statement as well (since, on my machine at least, it highlights the results), so I end up with something like
find / -name "*star*wars*" | grep star
Doing this or a similar method just helps me instantly find the filename and recognize if it is in fact the file I am looking for.
How to search for a text in specific files in unix
It might be better to use find
, since grep
's include/exclude can get a bit confusing:
find -type f -name "*.xml" -exec grep -l 'hello' {} +
This looks for files whose name finishes with .xml
and performs a grep 'hello'
on them. With -l
(L) we make the file name to be printed, without the matched line.
Explanation
find -type f
this finds files in the given directory structure.-name "*.xml"
selects those files whose name finishes with.xml
.-exec
execute a command on every result of thefind
command.-exec grep -l 'hello' {} +
executegrep -l 'hello'
on the given file. With{} +
we are refering to the matched name (it is like doinggrep 'hello' file
but refering to the name of the file provided by thefind
command). Also,grep -l
(L) returns the file name, not the match itself.
Linux terminal: Recursive search for string only in files w given file extension; display file name and absolute path
According to the grep
manual, you can do this using the --include
option (combined with the -l
option if you want only the name — I usually use -n
to show line numbers):
--include=
globSearch only files whose name matches glob, using wildcard matching as described under
--exclude
.
-l
--files-with-matches
Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning of each file stops on the first match. (
-l
is specified by POSIX.)
A suitable glob would be "*.doc" (ensure that it is quoted, to allow the shell to pass it to grep
).
GNU grep also has a recursive option -r
(not in POSIX grep). Together with the globbing, you can search a directory-tree of ".doc" files like this:
grep -r -l --include="*.doc" "mystring" .
If you wanted to make this portable, then find
is the place to start. But using grep
's extension makes searches much faster, and is available on any Linux platform.
Related Topics
Why Does the Wc Command Count One More Character Than Expected
Is Stack Memory Contiguous Physically in Linux
Linux. Sol_Netlink Not Defined
Remove File Coding Mark But Preserve Its Coding
How to Ssh Multiple Hops Without Putting the Local Rsa Key Everywhere
Makefile Export .O File to a Different Path Than .Cpp
Cmake Auto Header File Dependency
Tiny "Manually" Created Elf Giving Segmentation Fault
About the Memory Layout of Programs in Linux
Deleting String Up to the First Occurrence of Certain Character
Command Substitution Within Sed Expression
How to Tell Qt to Use Different Openssl
Execute Script as Another User Whilst Not Being Root
Simple Way to Convert Hh:Mm:Ss (Hours:Minutes:Seconds.Split Seconds) to Seconds