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
stat
Absolute path of a single file:
stat -c %n "$PWD"/foo/bar
If you give the find command an absolute path, it will spit the results out with an absolute path. So, from the Ken directory if you were to type:
find /home/ken/foo/ -name bar -print
(instead of the relative path find . -name bar -print
)
You should get:
/home/ken/foo/bar
Therefore, if you want an ls -l
and have it return the absolute path, you can just tell the find command to execute an ls -l
on whatever it finds.
find /home/ken/foo -name bar -exec ls -l {} ;\
NOTE: There is a space between {}
and ;
You'll get something like this:
-rw-r--r-- 1 ken admin 181 Jan 27 15:49 /home/ken/foo/bar
If you aren't sure where the file is, you can always change the search location. As long as the search path starts with "/", you will get an absolute path in return. If you are searching a location (like /) where you are going to get a lot of permission denied errors, then I would recommend redirecting standard error so you can actually see the find results:
find / -name bar -exec ls -l {} ;\ 2> /dev/null
(2>
is the syntax for the Borne and Bash shells, but will not work with the C shell. It may work in other shells too, but I only know for sure that it works in Bourne and Bash).
lspwd() { for i in $@; do ls -d -1 $PWD/$i; done }
This worked for me. But it didn't list in alphabetical order.
find "$(pwd)" -maxdepth 1
This command lists alphabetically as well as lists hidden files too.
ls -d -1 "$PWD/".*; ls -d -1 "$PWD/"*;
Recursive files can be listed by many ways in linus. Here i am sharing one liner script to clear all logs of files(only files) from /var/log/ directory and second check recently which logs file has made an entry.
First:-
find /var/log/ -type f #listing file recursively
Second:-
for i in $(find $PWD -type f) ; do cat /dev/null > "$i" ; done #empty files recursively
Third use:-
ls -ltr $(find /var/log/ -type f ) # listing file used in recent
note: for directory location you can also pass $PWD instead of /var/log.
find / -print
will do this