check if a file is in a folder or its subfolder using linux terminal
Solution 1
In order to search from your current directory, use
find . -name filename
In order to search from root directory use
find / -name filename
If you don't know the file extension try
find . -name filename.*
Also note that find command only displays the files in the path which you have permission to view. If you don't have permission for a/b/c path then it will just display a message mentioning that path can't be searched
Solution 2
By default, find will traverse all subdirectories, for example:
mkdir level1
mkdir level1/level2
touch level1/level2/file
find . -name "file"
Output:
./level1/level2/file
Solution 3
If you want to search for by filename, use find:
find /path -name "filename"
example:
find . -name myfile.txt
If need to find all files containing a specific string, use grep:
grep -r "string" /path
example:
grep -r foobar .
Solution 4
locate file name
This is the simple command
Solution 5
I also prefer using a combination of tree
and grep
. Something like
tree | grep filename
![i'm PosSible](https://i.stack.imgur.com/07o3e.jpg?s=256&g=1)
i'm PosSible
Updated on June 09, 2020Comments
-
i'm PosSible about 4 years
I want to check if the particular file is in a folder or its sub folder or not using Linux terminal.
Which should I use for this? I use
find
andgrep
command but it travels only one folder.-
fedorqui over 10 yearsI wasn't aware of the
grape
command. It sounds juicy :) Funny enough, I guess you meangrep
. -
i'm PosSible over 10 yearsyes there is spell mistake.there is grep not grape.thanks
-