如何删除硬链接?

如何删除硬链接?

最近我创建了一个链接,内容如下:

sudo ln -n originalFileLocation

如何删除硬链接?

答案1

您可以rm照常使用以下命令删除它:rm NameOfFile。请注意,硬链接没有“原始文件”和“文件链接”之间的区别:您只有同一个文件的两个名称,删除其中一个名称不会删除另一个。

答案2

实际上rm不起作用:

[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

有效的是unlink AZP

答案3

我有这个脚本来删除多余的硬链接。但要小心——这很危险。

#!/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

答案4

如果您只想删除链接并保留原始文件,则必须使用取消链接。

相关内容