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
This will give the canonical path (will resolve symlinks): realpath FILENAME
If you want canonical path to the symlink itself, then: realpath -s FILENAME
ls -d "$PWD/"*
This looks only in the current directory. It quotes "$PWD" in case it contains spaces.
Command: ls -1 -d "$PWD/"*
This will give the absolute paths of the file like below.
[root@kubenode1 ssl]# ls -1 -d "$PWD/"*
/etc/kubernetes/folder/file-test-config.txt
/etc/kubernetes/folder/file-test.txt
/etc/kubernetes/folder/file-client.txt
Just an alternative to
ls -d "$PWD/"*
to pinpoint that *
is shell expansion, so
echo "$PWD/"*
would do the same (the drawback you cannot use -1
to separate by new lines, not spaces).
Using fd
(alternative to find
), use the following syntax:
fd . foo -a
Where .
is the search pattern and foo
is the root directory.
E.g. to list all files in etc
recursively, run: fd . /etc -a
.
-a
,--absolute-path
Show absolute instead of relative 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.