Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question talks about finding directories in a current diretory. The solution is basically:

ls -d */

That's great but how can I easily list symlinks? Do I have to use something like

find . -xtype l -d 1
(intended to find symlinks max depth 1 - doesn't work)

Or is there an easier way? Can ls be used for this?

share|improve this question
    
On my Ubuntu system, a quick look at its man find shows that -d is a synonym for -depth (for compatibility with FreeBSD, NetBSD, MacOS X and OpenBSD.), ie. it is not the same as -maxdepth . . . -depth Process each directory's contents before the directory itself –  Peter.O Oct 4 '11 at 4:04
add comment

4 Answers

In zsh (add N inside the parentheses to include symlinks whose name begins with a .):

echo *(@)

With most find implementations:

find -maxdepth 1 -type l

POSIX-compliant:

find . -type d \! -name . -prune -o -type l -print

Or with a shell loop:

for x in * .*; do
  if [ -h "$x" ]; then echo "$x"; done
done
share|improve this answer
    
For the -prune option, you need to use find topdir ! -name topdir -prune; otherwise the starting directory is ignored as well. –  Arcege Oct 4 '11 at 10:55
add comment

You should be using -type and not -xtype:

   -xtype c
          The same as -type unless the file is a symbolic link.  For  sym‐
          bolic  links:  if the -H or -P option was specified, true if the
          file is a link to a file of type c; if the -L  option  has  been
          given,  true  if  c is `l'.  In other words, for symbolic links,
          -xtype checks the type of the file that -type does not check.

The default is -P, so the -xtype option will try to determine the resultant file, not the symlink itself. Actually, I get some positive results, which seems like a bug. The -P -xtype l should return true (on a symlink) iff the resultant is itself a symbolic link.

Can also use: ls -FA | sed -ne 's/@//p' which will display only the symlinks.

share|improve this answer
    
nice ls solution –  frogstarr78 Oct 4 '11 at 3:56
2  
sed -ne 's/@//p' (and even sed -ne 's/@$//p') is not a secure test, as the first version will give a false positive when @ occurs anywhere in the ls output, and the second will return a false postive when a filename actually ends in @ –  Peter.O Oct 4 '11 at 4:39
add comment

This isn't on a Mac, but

find . -maxdepth 1 -type l

works for me.

share|improve this answer
add comment

To find out only the files that are symlinks inside the current directory:

find . -type l -printf '%p -> %l\n'

This will recursively list all the symlink files. Also, it shows the actual files it points to.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.