8

Under following conditions-

  1. mv within the same HDD partition
  2. mv to a different partition in the same HDD
  3. mv to a different device (e.g. USB HDD)

Is the complete file moved or is it a very small change like some pointer reassignment ?

user13107
  • 5,335
  • That probably depends on the source and destination file systems, and as such there's probably no one answer. If source and destination are the same exact filesystem, usually only a few bookkeeping bits are shuffled around for most common Unix/Linux file systems. – jw013 Sep 13 '12 at 17:05
  • @jw013 Thanks for the comment. I edited the question. I'm not clear about difference between file-system and HDD partition. – user13107 Sep 13 '12 at 17:10
  • Filesystem is a kind of 'description' how the file is stored on a disk. Example of filesystems can be ext3, ext4 or NTFS. A partition is a part of the disk, that can contain a filesystem. – Paweł Rumian Sep 13 '12 at 17:38
  • @gorkypl So it seems a single disk can contain multiple partitions with same / different file-systems. – user13107 Sep 13 '12 at 17:44
  • 1
    Exactly what you said. Your question is not bad, but as jw013 said, the answer depends on the filesystem, as different filesystems may implement the mv procedure in different ways. – Paweł Rumian Sep 13 '12 at 17:50
  • @gorkypl To comply with the expected behaviour of a POSIX filesystem, a file must keep the same inode number if it is renamed. If it's going to keep the same inode anyway, it's very hard to imagine a reason why the file contents would move, no matter how weird the filesystem. So I'm tempted to upgrade your "it depends on the filesystem" to "it's always going to only move a directory entry without touching the file contents". – Celada Sep 13 '12 at 18:51
  • @Celada Are we talking about POSIX filesystems only? :) – Paweł Rumian Sep 13 '12 at 19:52
  • @gorkypl this is unix.stackexchange.com :-) – Celada Sep 13 '12 at 20:04
  • @Celada: oh, well, let it be ;) – Paweł Rumian Sep 13 '12 at 21:28
  • @user13107 Each partition can contain a different instance of the same or different file-system type. – ctrl-alt-delor Sep 01 '21 at 10:47

2 Answers2

7

It's up to each filesystem how to handle a move within the filesystem (also known as renaming a file), but filesystems pretty much universally handle it by updating directory entries without moving the inode or file contents.

A move between filesystems (it doesn't matter if it's on the same physical medium or not) is handled as a file copy followed by a delete. This is in fact exactly what the mv command does. Obviously that means that the destination filesystem has to make a new copy of the file.

Celada
  • 44,132
  • Thanks. So if mv is interrupted before deletion, does the file remain intact at the source ? – user13107 Sep 13 '12 at 17:54
  • 1
    Yes, you will certainly keep the source file if mv is interrupted. Depending on whether or not mv is given a chance to clean up after itself when it gets interrupted you may find a partially copied file at the destination too! – Celada Sep 13 '12 at 17:59
  • What about different partition, but same filesystem case? Here also copy->delete or just updating directory entries? – user13107 Sep 13 '12 at 18:03
  • 1
    Filesystems usually don't span partitions, so that case can't usually happen. – Jim Paris Sep 13 '12 at 18:12
  • @JimParis Sorry, I'm missing something. In Windows we can have C:/ drive, D:/ drive all containing same type of filesystem (say NTFS). In an analogous case, what happens in Linux ? – user13107 Sep 13 '12 at 18:25
  • 2
    @user13107 I don't know anything about MS Windows and "drives", but it sounds like your "C:/ drive" and "D:/ drive" may contain filesystems of the same type, but certainly don't both contain the actual same filesystem! Perhaps you are thinking of filesystem types like zfs and Btrfs, which actually can span multiple block devices (e.g. partitions)? In this case the answer is the same as always when renaming a file within a filesystem: only the directory entry will move. – Celada Sep 13 '12 at 18:46
3

man mv says:

 As the rename(2) call does not work across file systems, mv uses cp(1) and rm(1) to
 accomplish the move.  The effect is equivalent to:

       rm -f destination_path && \
       cp -pRP source_file destination && \
       rm -rf source_file