75

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

1

9 Answers 9

60

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"
0
35

Try something like:

find "$(cd ..; pwd)" -name "filename"
9
  • Thanks, I was just testing $(cd ..; pwd). Here it works OK, but if I do it alone in a Terminal, I can't get the parent dir... I get "Bash: <dir>: Is a directory. And if I do $(cd ..; echo "something") I get "something: not a command"
    – JorgeeFG
    Apr 7, 2014 at 15:52
  • 1
    Do you mean like a separate command? Then you would need to leave out the dollar sign: (cd ..; pwd) Apr 7, 2014 at 15:53
  • 1
    Yes ( ... ) means execute in a subshell, the output gets written to stdout. $( ... ) stands for "command substitution". The latter can be used as if it were a variable expansion. Apr 7, 2014 at 16:03
  • 2
    @Scrutinizer - I think you misunderstand how find works. If you use the full path in your search, you get the full path in your output. find /home/your_user -name foo. As OP is using "../" , hard to guess the full path to give in an answer.
    – Panther
    Apr 8, 2014 at 13:02
  • 2
    @JorgeeFG $(builtin cd ..; pwd) will canel functions, aliases.
    – user986805
    Oct 16, 2019 at 17:37
24

The simplest way is

find "$(pwd -P)" -name "filename"
2
  • 1
    Yes! Or find `pwd -P` -name "filename" Jul 24, 2019 at 0:35
  • 1
    this will find files in the current directory not from parent.
    – user986805
    Oct 16, 2019 at 18:21
22

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.

2
  • 1
    this will call readlink on every file so it will be very ineffective.
    – user986805
    Oct 16, 2019 at 18:20
  • I like the simplicity of this solution but I found xargs to be much faster.............................................. find .. -name "filename" | xargs readlink -f Nov 13, 2020 at 16:39
8

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
5
  • Previous comment removed, +1, good job @Wyrmwood Oct 16, 2019 at 20:51
  • Exactly what I needed (just the first occurrence and to print the full path including the filename in the output. Thank you Sir Apr 3, 2020 at 18:09
  • 1
    this doesn't work for files with whitespaces
    – peixotorms
    May 21, 2020 at 11:19
  • @peixotorms -print0 does the trick, and I corrected the answer.
    – DawnSong
    Mar 20 at 17:37
  • xargs 0 is a feature I have used recently for values with embedded single quotes, Nice@DawnSong
    – Wyrmwood
    Mar 21 at 16:54
4

Try this way:

find $PWD -name "filename"
1
  • The question has .., not ..
    – Carsten S
    Jan 9 at 12:08
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.

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

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

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.