How can I generate a list of files with their absolute path in Linux?
I am writing a shell script that takes file paths as input.
For this reason, I need to generate recursive file listings with full paths. For example, the file
bar has the path:
but, as far as I can see, both
find only give relative path listings:
./foo/bar (from the folder ken)
It seems like an obvious requirement, but I can't see anything in the
ls man pages.
How can I generate a list of files in the shell including their absolute paths?
If you give
find an absolute path to start with, it will print absolute paths. For instance, to find all .htaccess files in the current directory:
find "$(pwd)" -name .htaccess
or if your shell expands
$PWD to the current directory:
find "$PWD" -name .htaccess
find simply prepends the path it was given to a relative path to the file from that path.
Greg Hewgill also suggested using
pwd -P if you want to resolve symlinks in your current directory.
readlink -f filename
gives the full absolute path. but if the file is a symlink, u'll get the final resolved name.
Read more... Read less...
Use this for dirs (the
** is needed in bash to limit it to directories):
ls -d -1 "$PWD/"**/
this for files and directories directly under the current directory, whose names contain a
ls -d -1 "$PWD/"*.*
this for everything:
ls -d -1 "$PWD/"**/*
Taken from here http://www.zsh.org/mla/users/2002/msg00033.html
** is recursive if you enable
shopt -s globstar.
You can use
ls -d "$PWD/"*
This looks only in the current directory. It quotes "$PWD" in case it contains spaces.
$PWD is a good option by Matthew above. If you want find to only print files then you can also add the -type f option to search only normal files. Other options are "d" for directories only etc. So in your case it would be (if i want to search only for files with .c ext):
find $PWD -type f -name "*.c"
or if you want all files:
find $PWD -type f
Note: You can't make an alias for the above command, because $PWD gets auto-completed to your home directory when the alias is being set by bash.