LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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


Reply
  Search this Thread
Old Yesterday, 11:24 PM   #1
exerceo
Member
 
Registered: Oct 2022
Posts: 69

Rep: Reputation: 18
Question 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?
 
Old Today, 02:43 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,427

Rep: Reputation: 7751Reputation: 7751Reputation: 7751Reputation: 7751Reputation: 7751Reputation: 7751Reputation: 7751Reputation: 7751Reputation: 7751Reputation: 7751Reputation: 7751
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.
 
Old Today, 08:26 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 11,039
Blog Entries: 4

Rep: Reputation: 4062Reputation: 4062Reputation: 4062Reputation: 4062Reputation: 4062Reputation: 4062Reputation: 4062Reputation: 4062Reputation: 4062Reputation: 4062Reputation: 4062
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.
 
Old Today, 09:28 AM   #4
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 8,097
Blog Entries: 20

Rep: Reputation: 4700Reputation: 4700Reputation: 4700Reputation: 4700Reputation: 4700Reputation: 4700Reputation: 4700Reputation: 4700Reputation: 4700Reputation: 4700Reputation: 4700
Quote:
Originally Posted by sundialsvcs View Post
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.
 
Old Today, 09:33 AM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,799

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Quote:
Originally Posted by sundialsvcs View Post
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.
 
Old Today, 09:42 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,427

Rep: Reputation: 7751Reputation: 7751Reputation: 7751Reputation: 7751Reputation: 7751Reputation: 7751Reputation: 7751Reputation: 7751Reputation: 7751Reputation: 7751Reputation: 7751
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.
 
  


Reply

Tags
commands



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Kinds of links in Linux and use of unlink FilipN Linux - Newbie 2 04-18-2015 09:31 AM
when to use unlink port moonmoth Linux - Networking 1 07-20-2005 06:32 AM
cannot unlink cannot remove Rob Ellsworth Linux - General 1 10-01-2002 12:27 PM
cannot unlink simon Linux - General 1 07-31-2001 08:25 PM
How to unlink a directory? _TK_ Linux - General 4 04-25-2001 09:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration