Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
Yesterday, 11:24 PM
|
#1
|
|
Member
Registered: Oct 2022
Posts: 69
Rep:
|
When would one use unlink instead of rm?
[ Log in to get rid of this advertisement]
The "unlink" command can delete up to one file at a time, whereas the "rm" file can delete multiple files at once.
As far as I know, the "unlink" command uses fewer syscalls so it is better for performance. Also, "unlink" doesn't refuse to delete read-only files (chmod -w readonlyfile) whereas "rm" asks first.
Are there any other reasons I am not aware of?
|
|
|
|
Today, 02:43 AM
|
#2
|
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,427
|
https://www.gnu.org/software/coreuti...ink-invocation
unlink is the low level system call.
The unlink command will just call it. the rm command has a few options to make it more convenient, at the end it will call the same unlink function.
|
|
|
|
Today, 08:26 AM
|
#3
|
|
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 11,039
|
AFAIK, the only situation where you would actually need to use this command is when you have created "hard links." This is the case where more-than-one directory entry refers to the same underlying file ("inode"). And now you want to remove that "additional link," without necessarily removing the entity.
Also AFAIK, the much-more-flexible "symbolic link" has replaced this feature in common practice. It is: "a tiny file containing the name of another file."
Last edited by sundialsvcs; Today at 08:28 AM.
|
|
|
|
Today, 09:28 AM
|
#4
|
|
LQ Guru
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 8,097
|
Quote:
Originally Posted by sundialsvcs
AFAIK, the only situation where you would actually need to use this command is when you have created "hard links." This is the case where more-than-one directory entry refers to the same underlying file ("inode"). And now you want to remove that "additional link," without necessarily removing the entity.
|
But surely deleting the name wouldn't affect the content of the file if there was another filename hard-linked to it. As I understand it, delete reduces the link count of the inode by one and then checks if it is zero now. If so, it goes on to delete the file content. If not, it bails out at that point. A file in use would not have its content deleted and nor would one still hard-linked to a different name.
Last edited by hazel; Today at 09:31 AM.
|
|
|
|
Today, 09:33 AM
|
#5
|
|
Senior Member
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,799
|
Quote:
Originally Posted by sundialsvcs
AFAIK, the only situation where you would actually need to use this command is when you have created "hard links." This is the case where more-than-one directory entry refers to the same underlying file ("inode"). And now you want to remove that "additional link," without necessarily removing the entity.
|
No, removing a link is all that the rm command does. Actually removing a file (i.e., freeing the storage) is something that the kernel does when the last hard link to a file is removed and the file is not currenly held open by some process. Using unlink instead of rm just bypasses some warnings that the rm command would give for things like files for which you do not have write permission.
Quote:
|
Also AFAIK, the much-more-flexible "symbolic link" has replaced this feature in common practice. It is: "a tiny file containing the name of another file."
|
Use of symbolic links is absolutely unrelated to rm vs. unlink. Both rm and unlink treat symbolic links exactly the same as the would treat anything else. When asked to operate on a symbolic link, neither one would affect the target of the symbolic link.
|
|
|
|
Today, 09:42 AM
|
#6
|
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,427
|
again, unlink uses the low level system call unlink, and rm uses the underlying low level system call unlink.
The difference is only the possibilities provided by the available command line options.
|
|
|
|
All times are GMT -5. The time now is 02:13 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|