Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find full path given partial path

Tags:

find

shell

unix

I know how to find files using

find . -name "file_name"

But if I am given one part of a path, say "folder1/subfolder2/", how do I get all the full path that contains this partial path?

Example

partial path: folder1/subfolder2/

desire result:

/bob/folder1/subfolder2/yo/
/sandy/folder1/subfolder2/hi/
like image 921
James the Great Avatar asked Apr 25 '16 19:04

James the Great


People also ask

How do I find my full path?

Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document. Properties: Click this option to immediately view the full file path (location).

How do you get the full path in shell?

In this case, first, we need the current script's path, and from it, we use dirname to get the directory path of the script file. Once we have that, we cd into the folder and print the working directory. To get the full or absolute path, we attach the basename of the script file to the directory path or $DIR_PATH.

How do I find the full path in Linux?

To obtain the full path of a file, we use the readlink command. readlink prints the absolute path of a symbolic link, but as a side-effect, it also prints the absolute path for a relative path. In the case of the first command, readlink resolves the relative path of foo/ to the absolute path of /home/example/foo/.


2 Answers

Use the -path option:

find . -path '*/folder1/subfolder2/*'
like image 88
hek2mgl Avatar answered Sep 30 '22 02:09

hek2mgl


You may do it like below:

find . -path "*folder1/folder2" -prune -exec find {} -type f -name file.txt \;

With -prune you don't recurse after first match in a directory

like image 31
sjsam Avatar answered Sep 30 '22 02:09

sjsam