21

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

13

Try something like:

find "$(cd ..; pwd)" -name "filename"
  • 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 '14 at 15:52
  • Do you mean like a separate command? Then you would need to leave out the dollar sign: (cd ..; pwd) – Scrutinizer Apr 7 '14 at 15:53
  • Thanks, that was it. Any reference of why should I remove the $, how does it affect the subshell? – JorgeeFG Apr 7 '14 at 15:56
  • 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. – Scrutinizer Apr 7 '14 at 16:03
  • find / -name "filename" – Panther Apr 7 '14 at 18:48
11

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.

4

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
2

The simplest way is

find "$(pwd -P)" -name "filename"
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.

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.