I’m working on a shell script that accepts file paths as an input.
As a result, I’ll need to provide recursive file listings with complete paths. For instance, the path in the file bar is:
However, both ls and find, as far as I can tell, only give relative path listings:
./foo/bar (from the folder ken)
It sounds like a no-brainer, yet neither the find nor the ls man pages mention it.
In the shell, how can I get a list of files with their absolute paths?
Asked by Ken
If you tell find to start with an absolute path, it will print absolute paths. To find all.htaccess files in the current directory, for example:
find "$(pwd)" -name .htaccess
or if $PWD expands to the current directory in your shell:
find "$PWD" -name .htaccess
find merely prepends the specified path to a relative path to the file from that path.
If you want to resolve symlinks in your current directory, Greg Hewgill suggests using pwd -P.
Answered by Matthew Scharley
readlink -f filename
delivers the absolute path in its entirety. If the file is a symlink, however, the final resolved name will be returned.
Answered by balki
Use this for dirs (bash requires the / after ** to limit it to directories):
ls -d -1 "$PWD/"**/
This is for files and folders with a. in their names that are directly under the current directory:
ls -d -1 "$PWD/"*.*
this for everything:
ls -d -1 "$PWD/"**/*
The following is an excerpt from http://www.zsh.org/mla/users/2002/msg00033.html.
In bash, ** is recursive if you enable shopt -s globstar.
Answered by user431529
You can use
Answered by Vinko Vrsalovic
ls -d "$PWD/"*
Only the current directory is searched. If “$PWD” contains spaces, it quotes it.
Answered by didi
Post is based on https://stackoverflow.com/questions/246215/how-can-i-generate-a-list-of-files-with-their-absolute-path-in-linux