- List symlinks in current directory?
- 4 Answers 4
- How to List All Symlinks in the Linux Directory
- List All Symbolic Links Using the Find Command
- Syntax
- Examples
- List All Symlinks From the Entire Filesystem
- List All Symlinks in the Current Working Directory
- List All Symlinks in Any Directory
- List All Symlinks in a Directory Using Maxdepth Flag
- Conclusion
- About the author
- Shehroz Azam
- How to list all symbolic links in a directory
- 10 Answers 10
- How do I find all of the symlinks in a directory tree?
- 8 Answers 8
List symlinks in current directory?
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
4 Answers 4
In zsh (add N inside the parentheses to include symlinks whose name begins with a . ):
With most find implementations:
find . -type d \! -name . -prune -o -type l -print
for x in * .*; do if [ -h "$x" ]; then echo "$x"; done done
For the -prune option, you need to use find topdir ! -name topdir -prune ; otherwise the starting directory is ignored as well.
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.
How to List All Symlinks in the Linux Directory
The symbolic link, also known as symlink or soft link, is the file type that can hold the location of a file or directory in any Linux file system. You have created a couple of Symbolic links in your Linux filesystem, and sometimes there comes a need to list all the symbolic links. This post provides you with a step-by-step guide on how to list all symlinks in a Linux filesystem or a specific Linux directory.
From a couple of ways to list all the symbolic links in a Linux directory, we will follow the reliable and best way using the find command.
List All Symbolic Links Using the Find Command
Find command comes in handy when finding any type of file or folder in a Linux operating system.
Syntax
To find the symbolic links in any Linux operating system, the syntax is as follows:
is the location or directory name in which you want to search for the symbolic link,
-type is referencing the file type,
while l is representing the link file type.
Alright, let’s have a look at the examples and see how can we get the symbolic links listed in different ways by going through a couple of examples:
Examples
Using the find command, we can list the symlinks from the entire filesystem or in a specific directory. Let’s take a look at each example:
List All Symlinks From the Entire Filesystem
To list all the symlinks from the entire filesystem, you can execute the following find command by providing the “/” as path:
The “/” in the above command represents the entire file system, and the find command will search for the symbolic links from all over the system and list them out in the terminal.
List All Symlinks in the Current Working Directory
Similarly, if you want to find and list all the symlinks in the current working directory, then simply provide the “.” as a path to the find command as shown below:
In the above command, the “.” tells the find command to find the symlinks in the current working directory.
List All Symlinks in Any Directory
To list all the symlinks in any directory, just provide the directory path to the find command as shown below:
The find command will look for the symbolic links in the /var/www/ directory only and list out all the symbolic links in that directory.
List All Symlinks in a Directory Using Maxdepth Flag
You might have noticed that all the above commands displayed the symbolic links in the desired directory and showed all the symbolic links from the subdirectories, as well.
So, what if you do not want to go into this much depth? You just want to have the symbolic links in the specified directory. The solution to that problem is not rocket science, and we can quickly mention the depth using the maxdepth flag.
For example, to set the search depth to level one, the find command would go like this:
You can witness the output shown in the screenshot given above. The find command has shown only the symbolic links of the current working directory instead of all the subdirectories.
Conclusion
This post has provided multiple ways and gives a brief explanation on how to list all the symbolic links in the Linux filesystem or a specific Linux directory. Using the find command, we have learned to find and list down all the symbolic links and set the maximum depth level using the maxdepth flag. If you want to learn and explore more about the find command, feel free to read the man page of find using the “man find” command.
About the author
Shehroz Azam
A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.
How to list all symbolic links in a directory
I have a symbolic link in my /var/www/ directory that links to WordPress. When I run the command ls -la from the /var/www/ directory the link to WordPress doesn’t show up. Is there a way to list all of the symbolic links that are in a directory?
10 Answers 10
Parsing ls is a Bad Idea®, prefer a simple find in that case:
To only process the current directory:
find . -maxdepth 1 -type l -ls
Great answer! I adjusted mine to not descend down directory path like this: find /
@GabrielStaples from man find: -ls True; list current file in ls -dils format on standard output. Useful to see ./os-release -> ../usr/lib/os-release in /etc rather than just ./os-release
You can use grep with ls command to list all the symbolic links present in the current directory.
This will list all the links present in the current directory.
Please, do not use ls for scripting. Also mentioned in other answers. More: mywiki.wooledge.org/ParsingLs
ls -lhaF | grep ^l # list links ls -lhaF | grep ^d # list directories ls -lhaF | grep ^- # list files
This will list lines starting with «l» which represent Links in the perms column in place of l use d for directories and — for files
Just don’t do anything with this method programatically since malicious filenames can end up injecting shell code. To be safe, one should use the find command with -exec , and if piping to xargs , use the null-character separator output flag of find combined with the null-character separator input flag of xargs .
@MtlDev ! negates the condition matching, here ! -name . means matching everything except current directory.
This returns all symbolically linked items (both dirs & fns) in a directory:
find . -maxdepth 1 -type l -print | cut -c3- | grep -v "\#"
However, in order to distinguish between actual symbolically linked item types:
ls -lhaF | grep ^l | grep -v "\#" | cut -c42- | grep -v "/" | cut -d' ' -f1
Returns symbolically linked filename items only. And,
ls -lhaF | grep ^l | grep -v "\#" | cut -c42- | grep "/" | cut -d' ' -f1
Returns symbolically linked dirname items only.
To view the symbolic links in a directory:
- Open a terminal and move to that directory.
- Type the command:
ls -l *(@) lrwxrwxrwx 1 david david 15 Nov 18 22:35 gvimrc -> /etc/vim/gvimrc lrwxrwxrwx 1 david david 13 Nov 18 22:19 mydomains.php -> mydomains.php
Type ls -lai ,it will list all the files and subdirectories with corresponding inode numbers.You know files with same inode number are the links(hard or soft) and this solution also works for the symbolic links.
ls -lai does not show the same inode number for a file and its symbolic links. Unlike hard links, symbolic links have their own separate inode entries. This is what it looks like.
Can be done with python as well:
$ python -c "import os,sys; print '\n'.join([os.path.join(sys.argv[1],i) for i in os.listdir(sys.argv[1]) if os.path.islink(os.path.join(sys.argv[1],i))])" /path/to/dir
$ python -c "import os,sys; print '\n'.join([os.path.join(sys.argv[1],i) for i in os.listdir(sys.argv[1]) if os.path.islink(os.path.join(sys.argv[1],i))])" /etc /etc/vtrgb /etc/printcap /etc/resolv.conf /etc/os-release /etc/mtab /etc/localtime
This can be extended to be recursive via os.walk function, but it’s sufficient to use simple list generation for listing links in a single directory as I showed above.
How do I find all of the symlinks in a directory tree?
I’m trying to find all of the symlinks within a directory tree for my website. I know that I can use find to do this but I can’t figure out how to recursively check the directories. I’ve tried this command:
it take a while to run, however I’m getting no matches. How do I get this to check subdirectories?
8 Answers 8
This will recursively traverse the /path/to/folder directory and list only the symbolic links:
ls -lR /path/to/folder | grep '^l'
If your intention is to follow the symbolic links too, you should use your find command but you should include the -L option; in fact the find man page says:
-L Follow symbolic links. When find examines or prints information about files, the information used shall be taken from the prop‐ erties 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 subdirec‐ tory pointed to by the symbolic link will be searched. When the -L option is in effect, the -type predicate will always match against the type of the file that a symbolic link points to rather than the link itself (unless the symbolic link is bro‐ ken). Using -L causes the -lname and -ilname predicates always to return false.
This will probably work: I found in the find man page this diamond: if you are using the -type option you have to change it to the -xtype option:
l symbolic link; this is never true if the -L option or the -follow option is in effect, unless the symbolic link is broken. If you want to search for symbolic links when -L is in effect, use -xtype.