views:

9596

answers:

7

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.

+17  A: 

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.

Matthew Scharley
it was staring me in the face - thank you!
Ken
Note that if you also want to resolve symlinks, use `pwd -P`.
Greg Hewgill
+1  A: 

find / -print will do this

David Arno
+3  A: 

You can use

find $PWD

in bash

Vinko Vrsalovic
+1  A: 
ls -d $PWD/*
didi
doesn't work recursively
danio
+2  A: 

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!

Trudius
A: 
ls -1 | awk  -vpath=$PWD/ '{print path$1}'
Albert
Doesn't work recursively
danio
A: 

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