Problem
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:
/home/ken/foo/bar
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
Solution #1
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
Solution #2
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
Solution #3
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
Solution #4
You can use
find $PWD
in bash
Answered by Vinko Vrsalovic
Solution #5
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