1

How can I remove file and all links related to file to remove?

rm -f file.txt removes just the file. Futher more, if there any other hard links of file system the file will not be removed.

What is the most usefull way to do this?

Or may be there is a command to get full list of links of the file.

2

Use ls -li filename to get the inode of the file. Then find <the same filesystem> -inum <inode> to get the files with the same inode

This will ensure you find all the copies of the file (hardlinks). The softlinks are not interest because they just point to the original file

  • 1
    ... they just point to the original filename. But I agree with the answer. – rici Jan 19 '15 at 4:09
1

To find all links to a file, hard and symbolic, use the find command with the -L and -samefile options, like this:

find / -L -samefile file.txt

You could combine that with rm or with the find -delete action to delete those files in one command, but it would make me nervous to do that without reviewing the list of files to be deleted first. Also, if you remove file.txt before you've found all the links to it, it will be much more difficult to find the links.

  • 1
    The -L should precede the search path, but I don't recommend it, as symbolic links can take the search into other file systems. You are better to use find <File System root> -lname "*FileName" -exec ls -l {} \;. The * is because the link may be specified as a simple name or preceded by a directory path (absolute or relative), but it may give false finds - names with extra characters in front, or the same name in other directories: the ls will show you what the link contains, and the highlighting will show which are dead if you run the command after deleting the file and hard links. – AFH Jan 17 '15 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.