Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is costlier "rename" or "link and unlink" in C

Tags:

c

linux

I have about 100,000 files in a directory. I have to move all these files to another directory. There are two ways of doing it.

  1. rename(oldfile, newfile)

  2. link(oldfile, newfile)
    unlink(oldfile)

Which approach is better? Are there any other better solutions?

like image 753
quartz Avatar asked Sep 06 '13 08:09

quartz


1 Answers

Short answer:

The first solution.


Using the rename call you only execute one system call, and it can be further optimized by the system and / or the implementation. You also state clearly what you intend to do, which is more important than simply searching ultimate performance on a single point of your program.

The link/unlink solution works also, but it's really not clear what you intend to do. Also, how do you manage the link on multiple filesystems ? And the link is not even possible on some filesystem or operating system.

like image 93
Geoffroy Avatar answered Oct 24 '22 08:10

Geoffroy