187

Suppose I have a directory /dir inside which there are 3 symlinks to other directories /dir/dir11, /dir/dir12, and /dir/dir13. I want to list all the files in dir including the ones in dir11, dir12 and dir13.

To be more generic, I want to list all files including the ones in the directories which are symlinks. find ., ls -R, etc stop at the symlink without navigating into them to list further.

jww
  • 97,681
  • 90
  • 411
  • 885
CuriousDawg
  • 1,873
  • 2
  • 12
  • 4
  • Possible duplicate of [How to only get file name with linux \`find\`?](http://stackoverflow.com/questions/5456120/how-to-only-get-file-name-with-linux-find) – Eugen Konkov Nov 25 '16 at 11:15

8 Answers8

282

The -L option to ls will accomplish what you want. It dereferences symbolic links.

So your command would be:

ls -LR

You can also accomplish this with

find -follow

The -follow option directs find to follow symbolic links to directories.

On Mac OS X use

find -L

as -follow has been deprecated.

wxs
  • 5,617
  • 5
  • 36
  • 51
Michael Ridley
  • 10,378
  • 3
  • 22
  • 16
  • 16
    -follow is deprecated in favor of -L in newer versions of find. – pjz Sep 19 '08 at 20:41
  • @pjz: is there a cross-reference for '-follow deprecated; use -L'? Somewhat to my considerable surprise, I did find '-L' and '-H' listed in the POSIX / SUS standard at http://www.opengroup.org/onlinepubs/009695399/toc.htm, and even more to my surprise no '-follow', so I answered my own question. – Jonathan Leffler Oct 12 '08 at 23:23
  • this didn't work for me. first nothing happened then i tried with `-follow`- and it said it couln't find the folder `ollow` – smatthewenglish Apr 21 '15 at 08:04
  • 5
    On OS X 10.10 this works: `find -L .` — I was having the same problem as @S.Matthew_English – fregante May 20 '15 at 03:20
  • Your answer is good, but I want to `follow` only file paths(full path to file) and not directory path, how can I do it ? – Vicky Dev Jun 23 '16 at 06:53
  • Is there a way to list files only? No directories. – Souradeep Nanda Jun 07 '20 at 14:25
  • `ls -LR1` where -1 is added to print out one file per line – Bruce_Warrior Aug 05 '22 at 03:38
125

How about tree? tree -l will follow symlinks.

Disclaimer: I wrote this package.

jdotjdot
  • 16,134
  • 13
  • 66
  • 118
Steve Baker
  • 4,323
  • 1
  • 20
  • 15
60
find /dir -type f -follow -print

-type f means it will display real files (not symlinks)

-follow means it will follow your directory symlinks

-print will cause it to display the filenames.

If you want a ls type display, you can do the following

find /dir -type f -follow -print|xargs ls -l
Arnab Nandy
  • 6,472
  • 5
  • 44
  • 50
dvorak
  • 31,281
  • 4
  • 27
  • 29
11

Using ls:

  ls -LR

from 'man ls':

   -L, --dereference
          when showing file information for a symbolic link, show informa‐
          tion  for  the file the link references rather than for the link
          itself

Or, using find:

find -L .

From the find manpage:

-L     Follow symbolic links.

If you find you want to only follow a few symbolic links (like maybe just the toplevel ones you mentioned), you should look at the -H option, which only follows symlinks that you pass to it on the commandline.

pjz
  • 41,842
  • 6
  • 48
  • 60
5
find -L /var/www/ -type l

# man find
-L     Follow  symbolic links.  When find examines or prints information about files, the information used shall be taken from the

properties of the file to which the link points, not from the link itself (unless it is a broken symbolic link or find is unable to examine the file to which the link points). Use of this option implies -noleaf. If you later use the -P option, -noleaf will still be in effect. If -L is in effect and find discovers a symbolic link to a subdirectory during its search, the subdirectory pointed to by the symbolic link will be searched.

Arnab Nandy
  • 6,472
  • 5
  • 44
  • 50
Ashwin Muni
  • 144
  • 2
  • 4
5

I knew tree was an appropriate, but I didn't have tree installed. So, I got a pretty close alternate here

find ./ | sed -e 's/[^-][^\/]*\//--/g;s/--/ |-/'
divinedragon
  • 5,105
  • 13
  • 50
  • 97
3
ls -R -L

-L dereferences symbolic links. This will also make it impossible to see any symlinks to files, though - they'll look like the pointed-to file.

Arnab Nandy
  • 6,472
  • 5
  • 44
  • 50
Branan
  • 1,819
  • 15
  • 21
-1

in case you would like to print all file contents: find . -type f -exec cat {} +

droid192
  • 2,011
  • 26
  • 43