Monday, May 23, 2016

linux - What is the difference between unlink and rm?




Is unlink any faster than rm?


Answer



Both are a wrapper to the same fundamental function which is an unlink() system call.



To weigh up the differences between the userland utilies.



rm(1):




  • More options.


  • More feedback.

  • Sanity checking.

  • A bit slower for single calls as a result of the above.

  • Can be called with multiple arguments at the same time.



unlink(1):




  • Less sanity checking.


  • Unable to delete directories.

  • Unable to recurse.

  • Can only take one argument at a time.

  • Marginally leaner for single calls due to it's simplicity.

  • Slower when compared with giving rm(1) multiple arguments.



You could demonstrate the difference with:



$ touch $(seq 1 100)

$ unlink $(seq 1 100)
unlink: extra operand `2'

$ touch $(seq 1 100)
$ time rm $(seq 1 100)

real 0m0.048s
user 0m0.004s
sys 0m0.008s


$ touch $(seq 1 100)
$ time for i in $(seq 1 100); do rm $i; done

real 0m0.207s
user 0m0.044s
sys 0m0.112s

$ touch $(seq 1 100)
$ time for i in $(seq 1 100); do unlink $i; done


real 0m0.167s
user 0m0.048s
sys 0m0.120s


If however we're talking about an unadulterated call to the system unlink(2) function, which I now realise is probably not what you're accounting for.



You can perform a system unlink() on directories and files alike. But if the directory is a parent to other directories and files, then the link to that parent would be removed, but the children would be left dangling. Which is less than ideal.



Edit:




Sorry, clarified the difference between unlink(1) and unlink(2). Semantics are still going to differ between platform.


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...