A symlink or "symbolic link" is a Linux file that simply points at another file. If the referenced file is removed, the symlink will remain but not indicate there's a problem until you try to use it. Here are some easy ways to find and remove symlinks that point to files that have been moved or removed. Credit: Thinkstock Symbolic links play a very useful role on Linux systems. They can help you remember where important files are located on a system, make it easier for you to access those files and save you a good amount of disk space and trouble by making it unnecessary for you to copy large files just to make them a little more accessible. What exactly is a symbolic link? Generally referred to as a “symlink” or “soft link”, symbolic links are very small files. In fact, all a symlink really contains is the name of whatever file it points to, generally along with the file system path (relative to the current location or absolute). If a file named ref1 points to a file named /apps/refs/ref-2020, ref1 will be 19 characters long even if the ref-2020 file is 2 terabytes. If it points to ./ref-2020, it will be only 10 characters in length. If it points to ref-2020, only eight. If you issue a command like “vi ref1” (where ref1 is the name of a symlink), you will end up editing whatever file ref1 points to, not the contents of the symlink itself. Linux systems know how to work with symlinks and simply do the right thing. Similarly, if you use commands like cat, more, head or tail, you’ll be looking at the content of the referenced file. If you delete a symlink, on the other hand, you will be removing the link, never the referenced file. Again, Linux does what makes sense. Symlinks were meant to make using and sharing files easier — nothing more. When symlinks get broken When the file that a symbolic link points to is removed from the system or renamed, the symlink will no longer function as intended. Being little more than a reference stored in some particular directory, the symlink isn’t going to be updated or removed with changes to the file it points to. It keeps pointing at the referenced file, even after that file is long gone. If you try to use a symlink that points to a non-existent file, you will get an error like this: $ tail whassup tail: cannot open 'whassup' for reading: No such file or directory If you try to access a symlink that points to itself (yes, stranger things have happened), you will see something like this: $ cat loopy cat: loopy: Too many levels of symbolic links $ ls -l loopy lrwxrwxrwx 1 shs shs 5 May 28 18:07 loopy -> loopy And, just in case that first letter in the long listing didn’t catch your attention, it indicates that the file is a symbolic link. The rwxrwxrwx permissions are standard and don’t reflect the permissions on the file the symlink points at. Finding broken symlinks The find command has an option that allows you to locate symlinks that point to files that no longer exist. This command lists symlinks in the current directory: $ find . -type l The “l” (lowercase L) tells the find command to look for symbolic links. The command shown below, on the other hand, looks in the current directory for symlinks that point to files that don’t exist: $ find . -xtype l To avoid running into errors when the command tries to look into files or directories that you don’t have permission to examine, you can send all error output to /dev/null like this: $ find . -xtype l 2>/dev/null You can also find broken symlinks with a command like this one. It’s longer than the earlier one, but should do the same thing: $ find . -type l ! -exec test -e {} ; -print 2>/dev/null What to do with broken symlinks Unless you know that the file a symlink references is going to be replaced, the best move is to simply remove the broken link. In fact, you can find and remove broken symlinks in a single command if you want to, with a command like this one: $ find . -xtype l 2>/dev/null -exec rm {} ; The rm {} portion of that command turns into a “remove filename” command. If, instead, you want to associate the symlink with a different file, you will have to remove the symlink and then recreate it so that it points to the new file. Here’s an example: $ rm ref1 $ ln -s /apps/data/newfile ref1 Wrap-Up Symbolic links make the referenced files easier to find and use, but they sometimes evolve into little more than road signs that advertise a diner that closed last year. Finding commands can help you get rid of the broken symlinks or alert you to the absence of files that you might still require. Related content how-to How to find files on Linux There are many options you can use to find files on Linux, including searching by file name (or partial name), age, owner, group, size, type and inode number. By Sandra Henry Stocker Jun 24, 2024 8 mins Linux opinion Linux in your car: Red Hat’s milestone collaboration with exida With contributions from Red Hat and critical collaborators, the safety and security of automotive vehicles has reached a new level of reliability. By Sandra Henry Stocker Jun 17, 2024 5 mins Linux how-to How to print from the Linux command line: double-sided, landscape and more There's a lot more to printing from the Linux command line than the lp command. Check out some of the many available options. By Sandra Henry Stocker Jun 11, 2024 6 mins Linux how-to Converting between uppercase and lowercase on the Linux command line Converting text between uppercase and lowercase can be very tedious, especially when you want to avoid inadvertent misspellings. Fortunately, Linux provides a handful of commands that can make the job very easy. By Sandra Henry Stocker Jun 07, 2024 5 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe