To remove a symbolic link, use either the rm or unlink command followed by the name of the symlink as an argument. When removing a symbolic link that points to a directory do not append a trailing slash to the symlink name.
Remove a Symbolic Link using the unlink command
Replace symbolic_link with the name of the symbolic link you want to remove. After that, you can use the ls -l command to confirm if the symlink has been removed.
You can use rm (remove) or unlink command to remove or delete a file from the Linux command line. The rm command allows you to remove multiple files at once. With unlink command, you can delete only a single file.
How to Remove Directories (Folders)
- To remove an empty directory, use either rmdir or rm -d followed by the directory name: rm -d dirname rmdir dirname.
- To remove non-empty directories and all the files within them, use the rm command with the -r (recursive) option: rm -r dirname.
These are all safe:
- rm test2 (deletes the symlink only)
- rm -r test2 (deletes the symlink only)
- rm -rf test2 (deletes the symlink only)
- rm test2/ ( rm: cannot remove ‘test2/’: Is a directory — no action taken)
- rm -rf *2 (or any other glob matching the symlink — deletes the symlink only)
unlink() deletes a name from the filesystem. If that name was the last link to a file and no processes have the file open, the file is deleted and the space it was using is made available for reuse. … If the name referred to a symbolic link, the link is removed.
The unlink command performs the unlink subroutine on a specified file. The unlink command does not issue error messages when the associated subroutine is unsuccessful; you must check the exit value to determine if the command completed normally.
The syntax is as follows for the rm and unlink command to remove files on Ubuntu Linux:
- Open the Ubuntu terminal application (bash shell)
- Type any one of the following command to delete a file named ubuntu.nixcraft.txt in the current directory.
- rm ubuntu.nixcraft.txt. unlink ubuntu.nixcraft.txt.
To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.
Removing Directories with rm
To delete an empty directory, use the -d ( –dir ) option and to delete a non-empty directory, and all of its contents use the -r ( –recursive or -R ) option. The -i option tells rm to prompt you to confirm the deletion of each subdirectory and file.
As POSIX defined, rm and unlink both call to unlink() system call. In GNU rm , it calls to unlinkat() system call, which is equivalent to the unlink() or rmdir() function except in the case where path specifies a relative path. On some systems, unlink can also remove directory.
Deleting a symbolic link is the same as removing a real file or directory. ls -l command shows all links with second column value 1 and the link points to original file. Link contains the path for original file and not the contents.
No. rm -rf won’t follow symbolic links – it will simply remove them.
To determine whether the folder is a symbolic link you can use either of these methods.
- GUI Method: The folder icon will be different. The icon of the folder would have an arrow.
- CLI Method. The output of ls -l will clearly indicate that the folder is a symbolic link and it will also list the folder where it points to.