Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Linux's mv work internally?

I know in Linux, if we want to rename a folder from f_old to f_new, we can run:

mv f_old f_new

But this command actually confuses me. Does the system copy all stuff inside f_old, paste it to new folder f_new and delete f_old at last? Or does it just rename f_old to f_new?

like image 401
hackjutsu Avatar asked Mar 11 '15 21:03

hackjutsu


People also ask

How does the mv command work?

The mv command moves files and directories from one directory to another or renames a file or directory. If you move a file or directory to a new directory, it retains the base file name. When you move a file, all links to other files remain intact, except when you move it to a different file system.

Does mv move or copy?

Moving Files To move files, use the mv command (man mv), which is similar to the cp command, except that with mv the file is physically moved from one place to another, instead of being duplicated, as with cp.

What does mv do in Shell?

Use the mv command to move files and directories from one directory to another or to rename a file or directory. If you move a file or directory to a new directory without specifying a new name, it retains its original name.

How does mv work Unix?

mv is a Unix command that moves one or more files or directories from one place to another. If both filenames are on the same filesystem, this results in a simple file rename; otherwise the file content is copied to the new location and the old file is removed.


1 Answers

As long as the target location is on the same partition (filesystem) as the source, no data will get moved or even touched. Only the name in the directory entry gets changed.

If the target is on a separate partition, the data will first get copied to the target and then removed from source.

like image 105
hek2mgl Avatar answered Nov 13 '22 04:11

hek2mgl