How can I list files with their absolute path in l

2019-01-07 01:40发布

问题:

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.

回答1:

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

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.



回答2:

readlink -f filename 

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



回答3:

Use this for dirs:

ls -d -1 $PWD/**

this for files:

ls -d -1 $PWD/*.*

this for everything:

ls -d -1 $PWD/**/*

Taken from here http://www.zsh.org/mla/users/2002/msg00033.html



回答4:

You can use

find $PWD 

in bash



回答5:

ls -d $PWD/*


回答6:

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!



回答7:

The $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.



回答8:

lspwd() { for i in $@; do ls -d -1 $PWD/$i; done }


回答9:

ls -1 | awk  -vpath=$PWD/ '{print path$1}'


回答10:

find / -print will do this