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.
To find the symbolic links in any Linux operating system, the syntax is as follows:
In the above command,
<path> 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:
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.
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.