glockeruser
glockeruser

Reputation: 71

Cost of rename, delete or change path a file

What is the cost of the delete, rename, and move file operations? Which one is the fastest?

I want to use java and the files are maintained by the linux operating system.

Upvotes: 3

Views: 2440

Answers (3)

Stephen C
Stephen C

Reputation: 719289

It is not possible to say which is faster in general, because the relative performance depends on a variety of factors. And it is probably irrelevant ... because they do different things and typically are not interchangeable.

However:

  • Rename and move are typically equivalent if the source and destination locations are in the same file system.

  • If move involves moving between file systems it is probably the most expensive. O(N) bytes must be copied.

  • Otherwise, delete is probably the most expensive. The OS needs to update the parent directory AND mark all of the disc blocks used by the file as free.

  • The actual costs also depend on the operating systems and the type of file system(s) involved, and (in some cases) on the size of the files involved - see above.

Upvotes: 4

Taky
Taky

Reputation: 5344

It is dependent on the implementation details of the file system. In most fileSystems it should be an order one, O(1), operation.

Upvotes: 1

fgysin
fgysin

Reputation: 11943

Renaming a file is basically just changing the path in a localized way, so it should be as fast as changing the path. Deleting really just means deleting a reference, so it should be fairly fast as well.

The only case where you should see a significant increase in operation cost is for copying the file or for changing the path to an other partition/disk. These cases would actually require the file system to copy the file block by block.

How long it actually takes will heavily depend on the file system you are using (ext3, ext4, FAT, ...) and of course on the speed of your hard disks and hard disk connections (i.e. your motherboard).

  • If you need a definitive answer on your question I don't think you could avoid benchmarking it yourself using your specific test setup.

Upvotes: 0

Related Questions