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
You can use
bash
's Tilde Expansion to get the absolute path of the current working directory, this wayfind
prints the absolute path for the results as well:If executed in
~/Desktop
, this is expanded toand prints results like:
If you want to use this approach with the current working directory’s parent directory you need to
cd
before callingfind
:Try something like:
The simplest way is
Try using the
-exec
option offind
:Note:
readlink
prints the value of a symbolic link or canonical file name.This worked for me, but will only return the first occurrence.
To get full paths for all occurrences (as suggested by Sergiy Kolodyazhnyy)
Try this way:
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.
Removing last directory component with parameter Expansion.
An example of how you can use mapfile to save output from find to an indexed array for later use.
(if no array name is specified, MAPFILE will be the default array name).
Try with
-printf
. This also works with files with blank spaces.find .. -name "filename" -printf $PWD/"%f\n"
chaining through realpath: