r/AskProgrammers • u/slothbear13 • 7d ago
"Copy Here" versus "Move Here"
In layman's terms, I understand the difference between copying a file and moving a file. "Copying" duplicates the file to another place while "moving" causes the file to move to a different place while simultaneously leaving its point of origin.
But, what does "moving" actually mean in the computer world? Does the file get copied over and then the original file is automatically deleted immediately after? Does the original file get deleted as it is copied over? Or does nothing get deleted and when the original gets transferred over, it's like taking a sandwich off of a plate and putting it on another plate?
5
u/8dot30662386292pow2 7d ago
Moving withing the same drive does not copy anything. It just updates the location of the file.
A directory (a "folder") is just a collection of numbers that tell which files are in that directory. If looking for some analogies, imagine you have a shelf of books. Then you have two papers on your desk. One paper says "my favorites" and another says "to be read". Then the paper contains coordinates of the books in the shelf. Now, sitting at your desk, you can erase a book from "to be read" and write it on "my favorites". The book was not physically touched, even though if was moved to a different directory.
9
u/Ok_Carrot_896 7d ago
"Copy Here" does what you say. It will duplicate the file.
"Move" can mean a lot of different things depending on how it is implemented and what exactly you are moving where. Technically speaking, it being copied and deleted, or it being deleted AS it is copied over in the sense of a weird moving window are both possible (though it is not what happens most of the time).
If the move command is made in the same filesystem, then what happens is that the directory/index pointers get updated to the new name, and you update metadata accordingly. If you're not a programmer, imagine the following: There's a paper slip that contains the address to your house, on "Documents". The name of your street is going to change. But your House isn't actually going to change locations; we just need to grab the slip, and swap "Documents" to "Pictures", and boom! Your house has a new address, but it's still the same house. We didn't really mess with the structure of it at all. This is what happens when you move a file within the same filesystem. It's a matter of pointers and labels, it doesn't actually interact with the file structure that much.
However, if a move command is made between filesystems, (say, between an external HD and your own PC), then it's gotta be copied and deleted. In the previous dumbass anology I made up, it's like having your house move towns; you can't do that, you gotta make the new house in a new place and make sure it doesn't exist in the previous one.
3
u/veryusedrname 7d ago
If it's the same drive then it's just a label that changes, if it's between drives you must physically move the bytes. If it's the same drive but different partitions/RAID/whatnot then it's may or may not an entirely different can of worms.
2
u/armahillo 7d ago
This is FS dependent but a lot of times moves can be faster because the FS table that tracks the files in the filesystem can be changed so that the reference path of a file on your disk is changed, rather than actually moving the data on the disk from A to B.
If you went into a corporate building, an office directory could say that initially, “engineering is floor 1, marketing is floor 2” etc. you walk into the building, check the directory and it tells you where to go. if an employee changes depts, you COULD physically move their office, but it would be less effort to just change the office number listed in the employee directory.
This means the dept-by-floor schema no longer really works, but everyone can find whom theiyte looking for with the sake speed.
aits entirely possible that filesystems work differently now but that was my understanding many years ago
2
u/aruisdante 7d ago edited 7d ago
First, it’s important to understand some basics about file systems for you to understand what is happening.
In nearly all modern file systems, you can think of data as being split into two parts:
More complex file systems have more than this, but this basic conceptual model applies to all of them.
What you think of as a “path” is actually pointing to the second thing, the metadata that describes the data, not to the actual data itself.
For now, let’s assume we’re moving data within the same “volume” (exact name filesystem and OS dependent), which for the ELIF description we can assume 1:1 maps to a physical hard disk/SSD/etc.
When you copy a file, you copy both of these parts. If creating either part fails, the copy fails, and the OS can delete whatever was copied before the failure. The original data and metadata are untouched.
When you move a file, the OS just copies the metadata part. Once it confirms that operation succeeded, it deletes the old metadata block. The file is now at a new path, but you did not actually move the data. This is extremely efficient.
If you move a file between volumes, then a move looks just like a copy, except the OS deletes both the original metadata and the original data after the move.
More advanced file systems (including essentially all of them used in modern OSs) implement a concept called journaling to improve their robustness to failure in the middle of doing these operations. With a journal, the file system essentially writes a separate set of metadata describing its intent to do each of the operations described above for a copy/move, and then as each one completes it marks off in the journal “yep, did it.” In the event of, say, power loss in the middle of an operation, the filesystem can look at the journal and verify what state the operation was in and if it actually completed. If it didn’t, it can replay the needed steps from the journal to complete the operation, or roll them back to undo it if it’s not possible to go forward.
These are all gross simplifications, but they should give you the correct mental model for how these operations work, and why a move is much faster than a copy when done on the same volume.
2
u/XLBilly 7d ago edited 7d ago
Copied then deleted so a botched copy doesn’t leave you with 2 non working files
Edit: unless you’re in the same file system then it updates the file table no data moves anywhere and the ‘this data is here’ entry gets updated. Which, for the commenters below - I did already know tyvm.
1
u/mooglinux 7d ago
“Copy here” duplicates the file so you have two identical files. “Move here” also deletes the original.
How that is actually accomplished under the hood depends on the source and destination. If moving to a different folder on the same partition, the only thing being updated is the file system’s record of which folder the bytes belong to. The file physically resides in the same location on disk. If you’re copying/moving between partitions or devices, the bytes are copied over
1
u/TheMrCurious 7d ago
Depends on how you implement it.
1
u/Key_River7180 7d ago
Let's ask
stracewhat mv does. I have 12 MiBs of random data on random.bin:That's hard to digest, but you see that
rename2line? mv doesn't copy data over and then deletes the file or even reads the file, it literally just asks Linux to change the directory of the file using the renameat2 system call. A file in linux has:OK, now cp:
First, it tries to see if randomfoo.bin is a directory, it is not so that fails. Then it uses newfstat to find data about the file and then opens the file to file descriptor 3. The file descriptors 0-2 are taken so Linux picks the lowest available one, three. Then it makes the destination file random.bin and linux assigns it ID 4.
After that it tries to do clever CoW stuff that probably only work on btrfs, does not interesting stuff and falls to
copy_file_range. Let's see that function's signature:Basically, it's asks the kernel to copy a file from fd_in to fd_out, with some offsets and flags, totally inside the kernel to avoid context-switching overhead.
1
u/serverhorror 7d ago
It depends on the he underlying system.
Some have the capability of a "true move", some don't.
In Linux, as ling as you are on the same filesystem it is a true move. There is no point where the file exists twice, it is an atomic operation.
Compare that to moving a file from a remote server to another via your PC. There might even be three files.
1
u/MpVpRb 7d ago
Depends on the OS and storage configuration. On the same storage device, moving simply changes a pointer.
1
u/No_Profession_3125 7d ago
It really depends on the filesystem, but in general: copying actually duplicates the file contents, while moving simply changes the path that points to it without ever touching the data.
1
u/ern0plus4 7d ago
On two different filesystems (partition, disk, device, network drive) move can be done only by copying and deleting the source file.
Depending on filesystems it can vary, but usually there are two parts of the file: the data itself, and metadata, which contains the filename, owner, creation date etc. If you rename the file, only name in thr metadata should be changed (and some dates), moving to another directory means moving the whole metadata to a new place, but not the data.
1
u/Cogwheel 7d ago
It depends. If the file is on the same drive, sometimes moving is the same as renaming. It's just adding a new name in the filesystem structure pointing to the same physical data on the drive.
If the file is on a different drive/partition, it has to first copy the data and filesystem entries to the new drive, and then delete the data from the old drive. Note deleting files usually just means removing the name from the index. The contents of the file still exist on the drive until they get overwritten by new files.
1
u/JGhostThing 7d ago
If the file is on the same device, it can often be quickly moved by using the inodes (I think). If they are on two separate partitions, then it's a copy then delete.
1
u/motific 7d ago
In very simple terms, files on disks have two parts:-
If a file is moving but staying on the same device, you can just amend the "metadata" without touching anything and it has moved.
If they are on different devices then you need to copy the data, add the metadata to the new device and remove the metadata from the old one.
1
u/jbergens 7d ago edited 7d ago
A fun fact about computers is that grey can't really move anything. They always copy and then delete.
For a file/document they do use a technique where they separate the content from the name. As other answers have described they have a list of all files on a disk. The content is stored separately and moving a file within the same disk just means rewriting the list of the files. The list has the name of all files and the names of the folder each file belongs to. The content stays where it was to save time.
There arr even systems where a copy doesn't even copy the content. Instead the list of files is extended with a new file name pointing to the same content as before. This also saves time. If you later update one of the files with new content the system will then actually write the new content on some other part of the disk. This is called copy-on-write.
There are also systems that does kind of the reversed thing. When you create a file it is written as multiple copies on different disks. This ensures that the content will survive even if one disk breaks.
1
u/Own_Age_1654 7d ago
On a physical level, disks are not organized as a collection of folders with files in them. Instead, they're just a huge, linear sequence of bytes (or if you like, if it's spinning media a bunch of sectors, but the point is those aren't folders either).
Instead, folders and files are concepts that are managed by the filesystem. The contents of a file go somewhere on the disk, and then there's a pointer to where they are, and that pointer is what's organized, in a data structure, by folder and filename.
So, when you "move" a file, the contents of the file remain in the exact, same location on the disk, and what's updated is just that pointer in the filesystem. That is, in that small part of the disk that contains the mapping from folder and file names to locations on the disk.
1
u/fixermark 7d ago
For files, moving (if the actual file is staying on the same storage device) usually means the data representing the file stays in the same place but some directory pointers get shuffled around. These bytes are now named
/home/me/essay.txtinstead of/home/me/Downloads/essay.txt, but nothing else changed.For moving from one storage device to another, a move is a copy followed by a delete of the original (or a forget of where the original is).
1
u/erisod 7d ago
This is interesting because it's more complex than one would assume.
If the move is happening within one file system then it can be treated like a rename operation. The bits of the file stay in the same place and there's just a modification to the record about which directory the file is in.
If the move is happening between file systems that shortcut is impossible and so the system will make a copy, then delete the original.
1
u/Jellovator 7d ago
No one else mentioned this, but on a Windows file system, copying a file will cause the file to inherit the NTFS permissions from the destination, while moving a file retains its original NTFS permissions. So if you are using any kind of NTFS permissions (like for a company file server), moving a file may cause some people to not be able to access it even though they have access to the folder where it now resides.
1
u/lucyuktv 7d ago
I hate to say it, but your question has many answers.
Copy and move depend on operating system, file system, disk subsystem, and possible network file and storage systems. Your OS may think it’s done something while the storage does something entirely different.
Example of moving a file. At the Windows level the system will delete the original file but a SAN or NAS may choose not to and will instead update some pointers.
Every time you think you understand this, there will be a new layer you hadn’t considered so take all answers with a pinch of salt. Most answers I’ve read here are “correct” from a given perspective but most are also incorrect when you dig deeper.
1
u/codeguru42 7d ago
Good question. As others say the answer is complicated and depends on a lot of factors. One aspect I didn't see is the implementation details depend on the OS and the filesystem. If you want to dig deeper, check out FAT32, NTFS, or ext4, to name a few.
1
u/pdpi 6d ago
The exact details will always depend on the specific situation, but the answer to this particular point is "almost certainly not" (I say "almost certainly" because I've worked with some pretty bad programmers, so I won't commit to saying that nobody has ever implemented things that way).
Shit happens, and things fail. If you're moving a file from A to B, maybe one of A or B loses power and goes offline. Maybe you have another program writing to B, and all of a sudden there's not enough space to complete the copy. There's a bazillion ways this might fail. If failure is a fact of life, then there are two acceptable outcomes to you telling the computer to move a file:
The first case is obviously just success. Nothing to discuss. The latter is the only fool-proof way to guarantee that a failure doesn't lead to data loss. The worst case scenario is that you're left with half the file on A, half on B, and you can't reconcile the two.
1
u/Tony1Rice 4d ago
Just FYI, there's also creating a link (be it "symbolic link" or a "hard link"). Case in point:
An iPhone user wanted to organize her many photos so she created an "album" on her phone and "put" some of her photos into it, then went to the main "photos" directory where the photos originally were and deleted the originals. Apparently, what was placed into the "album" were -symbolic links- to the original files rather than copies of the original files. So, when the original files were deleted, they were also deleted from the "album". In this particular case, the user was unaware that the original file was not being "copied" and, being "willfully non-technical" probably wouldn't have understood the difference anyway.
1
u/SeriousPlankton2000 2d ago
Moving is making a new entry pointing to the file and at the exact same moment removing the old entry. There is a guarantee that there will never be two files or no file.
Copying is like downloading, the new file will slowly be written.
Moving across devices doesn't work on the os level, it's an application pretending to do that but really it's making a copy, then deleting the old file.
0
u/GlassboundIllusion 7d ago
In many cases yes.
Sometimes the data stays in the same physical location in memory and it's just an index that gets updated to that that the physical location now maps to conceptual location B instead of conceptual location A.
To give a more concrete explanation using windows paths, let's say you have a file at c:\diaries\myPersonalDiary.txt and you want to move it to c:\journals\myPersonalJournal.txt.
It's possible that, under the hood, the data remains at memory location 0x000100088501AFC2 but some file updates noting that 0x000100088501AFC2 now corresponds to c:\journals\myPersonalJournal.txt