Skip to main content
edited tags
Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 265
improved formatting
Source Link
Thomas
  • 6.6k
  • 8
  • 31
  • 34

How to find files with a specific pattern in the parent and child directory of my present working directory using a single command ?

Filename - test.txttest.txt, the file has the pattern "nslookup"nslookup

This file is present in 3 directories and they are /home/home, /home/1/home/1 and /home/1/2/home/1/2

I am currently at /home/1/home/1. I have tried below commands :

find ../ -type f -name "test.txt"

find ../ -type f -name "test.txt"

Output :

../test.txt

../home/1/test.txt

../home/1/2/test.txt

../test.txt
../home/1/test.txt
../home/1/2/test.txt

I was able to find the files, hence I tried the below command :

$ find ../ -type f -exec grep "nslookup" {} ;

nslookup

nslookup

nslookup

$ find ../ -type f -exec grep "nslookup" {} \;
nslookup
nslookup
nslookup

This doesn't display the file names.

Command : find . -type f -name "test.txt | xargs grep "nslookup'

find . -type f -name "test.txt" | xargs grep "nslookup"

==> gives me files in pwd and child directories :

./1/test.txt:nslookup

./test.txt:nslookup

./1/test.txt:nslookup
./test.txt:nslookup

but when I try to search in the parent directory as shown below the results are erroneous :

find ../ -type f -name "test.txt" | xargs grep "nslookup

User@User-PC ~/test $ uname -a CYGWIN_NT-6.1 User-PC 2.5.2(0.297/5/3) 2016-06-23 14:29 x86_64 Cygwin

find ../ -type f -name "test.txt" | xargs grep "nslookup"

User@User-PC ~/test
$ uname -a
CYGWIN_NT-6.1 User-PC 2.5.2(0.297/5/3) 2016-06-23 14:29 x86_64 Cygwin

How to find files with a specific pattern in the parent and child directory of my present working directory using a single command ?

Filename - test.txt, the file has the pattern "nslookup"

This file is present in 3 directories and they are /home, /home/1 and /home/1/2

I am currently at /home/1. I have tried below commands :

find ../ -type f -name "test.txt"

Output :

../test.txt

../home/1/test.txt

../home/1/2/test.txt

I was able to find the files, hence I tried the below command :

$ find ../ -type f -exec grep "nslookup" {} ;

nslookup

nslookup

nslookup

This doesn't display the file names.

Command : find . -type f -name "test.txt | xargs grep "nslookup' ==> gives me files in pwd and child directories :

./1/test.txt:nslookup

./test.txt:nslookup

but when I try to search in the parent directory as shown below the results are erroneous :

find ../ -type f -name "test.txt" | xargs grep "nslookup

User@User-PC ~/test $ uname -a CYGWIN_NT-6.1 User-PC 2.5.2(0.297/5/3) 2016-06-23 14:29 x86_64 Cygwin

How to find files with a specific pattern in the parent and child directory of my present working directory using a single command ?

Filename - test.txt, the file has the pattern nslookup

This file is present in 3 directories and they are /home, /home/1 and /home/1/2

I am currently at /home/1. I have tried below commands :

find ../ -type f -name "test.txt"

Output :

../test.txt
../home/1/test.txt
../home/1/2/test.txt

I was able to find the files, hence I tried the below command :

$ find ../ -type f -exec grep "nslookup" {} \;
nslookup
nslookup
nslookup

This doesn't display the file names.

Command :

find . -type f -name "test.txt" | xargs grep "nslookup"

==> gives me files in pwd and child directories :

./1/test.txt:nslookup
./test.txt:nslookup

but when I try to search in the parent directory as shown below the results are erroneous :

find ../ -type f -name "test.txt" | xargs grep "nslookup"

User@User-PC ~/test
$ uname -a
CYGWIN_NT-6.1 User-PC 2.5.2(0.297/5/3) 2016-06-23 14:29 x86_64 Cygwin
Source Link
Ashwin B
  • 31
  • 1
  • 2

How to find files with a specific pattern in the parent and child directory?

How to find files with a specific pattern in the parent and child directory of my present working directory using a single command ?

Filename - test.txt, the file has the pattern "nslookup"

This file is present in 3 directories and they are /home, /home/1 and /home/1/2

I am currently at /home/1. I have tried below commands :

find ../ -type f -name "test.txt"

Output :

../test.txt

../home/1/test.txt

../home/1/2/test.txt

I was able to find the files, hence I tried the below command :

$ find ../ -type f -exec grep "nslookup" {} ;

nslookup

nslookup

nslookup

This doesn't display the file names.

Command : find . -type f -name "test.txt | xargs grep "nslookup' ==> gives me files in pwd and child directories :

./1/test.txt:nslookup

./test.txt:nslookup

but when I try to search in the parent directory as shown below the results are erroneous :

find ../ -type f -name "test.txt" | xargs grep "nslookup

User@User-PC ~/test $ uname -a CYGWIN_NT-6.1 User-PC 2.5.2(0.297/5/3) 2016-06-23 14:29 x86_64 Cygwin