How can I list files with their absolute path in l

2019-01-07 01:20发布

I want to generate recursive file listings with full paths

/home/ken/foo/bar

but as far as I can see both ls and 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 find or ls man pages.

10条回答
姐就是有狂的资本
2楼-- · 2019-01-07 01:51

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 borne and bash shells, but will not work with c shell. It may work in other shells too, but I only know for sure that it works in bourne and bash).

I hope this helps!

查看更多
ら.Afraid
3楼-- · 2019-01-07 01:53
lspwd() { for i in $@; do ls -d -1 $PWD/$i; done }
查看更多
Bombasti
4楼-- · 2019-01-07 01:54
readlink -f filename 

gives the full absolute path. but if the file is a symlink, u'll get the final resolved name.

查看更多
看我几分像从前
5楼-- · 2019-01-07 01:58

find / -print will do this

查看更多
登录 后发表回答