Recently I created a link with the following:
sudo ln -n originalFileLocation
How do I delete a hard link?
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.
sudo
), if you created it with the command you provided (as super-user).
– Rafał Cieślak
Nov 05 '11 at 11:38
rm
ing a hard link do that's incorrect?
– Daniel Kaplan
Feb 05 '22 at 09:12
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
.
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 '16 at 17:04
AZP
is a symbolic link to a directory (or anything else) rm AZP/
will not work because rm
thinks its a directory (because of the /
at the end). However rm AZP
will work just fine. -1
– David Foerster
Nov 29 '16 at 11:40
I have this script to remove redundant hard links. But take care - it is quite dangerous.
#!/bin/bash
clear
echo Reduce redundant hardlinks in the current folder
echo ------------------------------------------------
echo
echo " $(basename $0) [-R]"
echo " -R means recursive"
echo
read -p "You can break by pressing Ctrl+C"
echo
ask=1
if [ a$1 == "a-R" ]; then recursive=" -R "; fi
for i in $(ls -i $recursive | awk '{print $1}' | uniq --repeated | sort);
do
echo "Inode with multiple hardlinked files: $i"
first=1
for foundfile in $(find . -xdev -inum $i);
do
if [ $first == 1 ]; then
echo " preserving the first file: $foundfile"
first=0
else
echo " deleting the redundant file: $foundfile"
#rm $foundfile
fi
done
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
fi
# read -p "pause for sure"
echo
done
echo "All redundant hardlins are removed."
echo
If you want to remove only the link and thus keep the original file, you have to use unlink.
unlink(1)
does? It's a shallow wrapper around the unlink(2)
system call, the same system call that rm(1)
uses for all files that aren't directories.
– David Foerster
Nov 29 '16 at 11:42
unlink
, despite it's name, will not separate a hardlinked into two separate files, but remove the "unlinked" directory entry (but not the file/content/inode, as long as the link count is > 1).
– Murphy
May 04 '17 at 22:29
ln -n /path/to/file
creates a file namedfile
in the current directory and is shorthand forln --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 05 '11 at 10:28rm <resulting hardlink>
will delete the inode referenced byunlink
to only remove the directory entry