107

Is there a way to get absolute path of a file that is being searched?

For example:

find .. -name "filename"

It gives me results like ../filename but I want the full path.

What I need is to find in the parent directory and its children, for a specific file, that I will use in another script later.

Thanks

Radu Rădeanu
  • 174,089
  • 51
  • 332
  • 407
JorgeeFG
  • 2,098

10 Answers10

109

You can use bash's Tilde Expansion to get the absolute path of the current working directory, this way find prints the absolute path for the results as well:

find ~+ -type f -name "filename"

If executed in ~/Desktop, this is expanded to

find /home/yourusername/Desktop -type f -name "filename"

and prints results like:

/home/yourusername/Desktop/filename

If you want to use this approach with the current working directory’s parent directory you need to cd before calling find:

cd .. && find ~+ -type f -name "filename"
GcL
  • 103
dessert
  • 40,956
40

Try something like:

find "$(cd ..; pwd)" -name "filename"
30

The simplest way is

find "$(pwd -P)" -name "filename"
Vivek
  • 409
27

Try using the -exec option of find:

find .. -name "filename" -exec readlink -f {} \;

Note: readlink prints the value of a symbolic link or canonical file name.

13

This worked for me, but will only return the first occurrence.

realpath $(find . -type f -name filename -print -quit)

To get full paths for all occurrences (as suggested by Sergiy Kolodyazhnyy)

find . -type f -name filename -print0 | xargs -0 realpath
DawnSong
  • 113
Wyrmwood
  • 226
5

Try this way:

find $PWD -name "filename"
3

Unless I totally misunderstand, it is as simple as this:

find $(realpath .) -name 'river.jpg'

By specifying the full real path as a start, find will implicitly output this full path as a search result.

The bash command realpath converts the current (or any other directory as ./images) into its real path). the $(realpath .) converts the output to a variable, as if it was typed manually, e.g. /home/myusername

anonymous2
  • 4,325
Pelton
  • 31
2

Try with -printf. This also works with files with blank spaces.

find .. -name "filename" -printf $PWD/"%f\n"

1

Removing last directory component with parameter Expansion.

find "${PWD%/*}" -name 'filename'

An example of how you can use mapfile to save output from find to an indexed array for later use.

mapfile -t -d '' < <(find ${PWD%/*} -name 'filename' -print0)

(if no array name is specified, MAPFILE will be the default array name).

for i in "${MAPFILE[@]}"; do
    echo "$((n++)) $i"
done
1

Also using PWD can show you the full directory. Pwd will show you all your directorys you are in like the expanding of filename. Hope this helped.