Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Recently I created a link with the following:

sudo ln -n originalFileLocation

How do I delete a hard link?

share|improve this question
FYI: ln -n /path/to/file creates a file named file in the current directory and is shorthand for ln --no-dereference /path/to/file. This means that if /path/to/file is a symbolic link, the newly created hardlink will point to that symlink instead of the target of the symlink. – Lekensteyn Nov 5 '11 at 10:28
up vote 17 down vote accepted

You can delete it with rm as usual: rm NameOfFile. Note that with hard links there is no distinction between "the original file" and "the link to the file": you just have two names for the same file, and deleting just one of the names will not delete the other.

share|improve this answer
Also ote you will need to rm it as root (use sudo), if you created it with the command you provided (as super-user). – Rafał Cieślak Nov 5 '11 at 11:38

Actually rm doesn't work:

[user@localhost Products]$ rm AZP/
rm: cannot remove `AZP/': Is a directory
[user@localhost Products]$ rm -r AZP/
rm: cannot remove `AZP': Not a directory

What works is unlink AZP.

share|improve this answer
Are you sure yours was a hard link? Hard links are just like files iirc. – Seth Mar 12 '14 at 16:59
@Seth, actually I don't remember what was that, but it doesn't want to remove as you could see. Somebody told me to use unlink and it worked. :) – Bunyk Mar 26 '14 at 21:21
That's probably because AZP was a file, not a directory, but I couldn't be sure without more information. Unlink should always work though, so no problems there. – Seth Mar 26 '14 at 22:19
AZP/ looks like a directory, rm doesn't operate on directories without the recursive flag. Also according to the coreutills docs. >>> Most systems prohibit making a hard link to a directory; on those where it is allowed, only the super-user can do so (and with caution, since creating a cycle will cause problems to many other utilities). – ThorSummoner Mar 24 at 17:04

I have this script to remove redundant hard links. But take care - it is quite dangerous.

echo Reduce redundant hardlinks in the current folder
echo ------------------------------------------------
echo "  $(basename $0) [-R]"
echo "      -R means recursive"
read -p "You can break by pressing Ctrl+C"
if [ a$1 == "a-R" ]; then  recursive=" -R "; fi

for i in $(ls -i $recursive | awk '{print $1}' | uniq --repeated | sort); 
    echo "Inode with multiple hardlinked files: $i"
    for foundfile in $(find . -xdev -inum $i);
        if [ $first == 1 ]; then
            echo "  preserving the first file:  $foundfile"
            echo "  deleting the redundant file:    $foundfile"  
            #rm $foundfile  
    if [ $ask == 1 ]; then 
        read -p "Delete all the rest of redundant hardlinks without asking? y/N "
        if [ a${REPLY,,} == "ay" ]; then  ask=0; fi
#   read -p "pause for sure"
echo "All redundant hardlins are removed."
share|improve this answer

Your Answer


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.