Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if a file is in a folder or its subfolder using linux terminal

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 and grep command but it travels only one folder.

like image 957
i'm PosSible Avatar asked Mar 03 '14 13:03

i'm PosSible


3 Answers

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

like image 121
user3256147 Avatar answered Sep 25 '22 17:09

user3256147


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
like image 33
Andrew Stubbs Avatar answered Sep 23 '22 17:09

Andrew Stubbs


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 . 
like image 28
ludovico Avatar answered Sep 25 '22 17:09

ludovico