Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find file by name up the directory tree, using bash

Tags:

find

bash

Using bash, how can I find a file with a specific name somewhere up the directory tree from the pwd?

To be more clear. I want to find a file that sits in my working directory's root, but I don't know where the root is, and my pwd might be anywhere below the root.

like image 514
Niel de Wet Avatar asked Feb 21 '12 11:02

Niel de Wet


People also ask

How do I search for a file in a directory?

To search for files in File Explorer, open File Explorer and use the search box to the right of the address bar. Tap or click to open File Explorer. Search looks in all folders and subfolders within the library or folder you're viewing. When you tap or click inside the search box, the Search Tools tab appears.

How do I search for a file in Bash?

You need to utilize the “-L” option and the path and “-name” option in your command. The “*” in the name specification is used for searching “all” the bash files with “.

How do I search for a directory in Bash?

Checking If a Directory Exists In a Bash Shell Script-h "/path/to/dir" ] && echo "Directory /path/to/dir exists." || echo "Error: Directory /path/to/dir exists but point to $(readlink -f /path/to/dir)." The cmd2 is executed if, and only if, cmd1 returns a non-zero exit status.


1 Answers

Find file.txt up to root

x=`pwd`
while [ "$x" != "/" ] ; do
    x=`dirname "$x"`
    find "$x" -maxdepth 1 -name file.txt
done
like image 57
kev Avatar answered Oct 04 '22 07:10

kev