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

21,440

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
Share:
21,440
i'm PosSible
Author by

i'm PosSible

Updated on June 09, 2020

Comments

  • i'm PosSible
    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 and grep command but it travels only one folder.

    • fedorqui
      fedorqui over 10 years
      I wasn't aware of the grape command. It sounds juicy :) Funny enough, I guess you mean grep.
    • i'm PosSible
      i'm PosSible over 10 years
      yes there is spell mistake.there is grep not grape.thanks