0

In Advanced Programming in the UNIX Environment (book) Section 4.14, interpret the i-node and directory, there is a paragraph:

When renaming a file without changing file systems, the actual contents of the file need not be moved--all that needs to be done is to add a new directory entry that points to the existing i-node and then unlink the old directory entry. For example, to rename the file /usr/lib/foo to /usr/foo, the contents of the foo needn't be moved if the directories /usr/lib and /usr are on the same filesystem. This is how the mv command usually operates.

According to that, a filename of a file should include the path to that file, and "mv" doesn't really move the file. But why the time it takes to move the two files under the same directory differs according to their size?

  • 5
    "But why the time it takes to move the two files under the same directory differs according to their size?" You need to provide some empirical evidence of this, because it certainly doesn't seem true to me. It happens instantly regardless of file size. You can also confirm that mv actually uses rename() when that's all it has to do by watching it in strace. – goldilocks Jul 13 '15 at 12:57
  • This is my error. – tian tong Jul 13 '15 at 13:33
1
  1. The path name is inferred from the route from the file's directory entry back to the root, /.
  2. A file's name is stored in its directory, so the same "file" can be called two different things simultaneously.
  3. The time taken to move a file within the same filesystem is independent of the file size.

In response to your statements:

a filename of a file should include the path to that file

The paragraph does not say that, nor anything resembling that. Perhaps it's in a part that you didn't quote?

mv doesn't really move the file

Yes it does: the file is put into a new directory - possibly with a new name - and removed from the old. This a move, but not a copy (the file's contents are not moved, only its location in the filesystem).

For example, to rename the file /usr/lib/foo to /usr/foo, the contents of the foo needn't be moved if the directories /usr/lib and /usr are on the same filesystem.

The path /usr/lib/foo is decomposed to the filename foo within the directory /usr/lib. Similarly /usr/foo is decomposed to foo under /usr. If /usr and /usr/lib are on the same filesystem then the rename becomes a link-and-unlink operation (the position of the file within the filesystem is moved; the file's contents remain unmoved).

  • 1: "a filename of a file should include the path to that file" is my infer according to "rename the file /usr/lib/foo to /usr/foo", why it says "rename"? ............2: my meaning about "mv doesn't really move the file" is that "mv" doesn't move the place of the files' data. Does "mv" move the file's data from one place to another? – tian tong Jul 13 '15 at 13:05
  • @tiantong I've tried to add clarifications for you in my Answer – roaima Jul 13 '15 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.