Get absolute path of files using 'find' command
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
19 Answers
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/filenameIf 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 Try something like:
find "$(cd ..; pwd)" -name "filename" 9 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.
The simplest way is
find "$(pwd -P)" -name "filename" 2 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 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.
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 Try this way:
find $PWD -name "filename" 1 Try with -printf.
This also works with files with blank spaces.
find .. -name "filename" -printf $PWD/"%f\n"