Difference between cp and mv commands in UNIX

CEans,
I've a simple query related to UNIX. What is the difference between cp and mv commands in UNIX?
Are these commands similar to copy and 'cut' command operations that we do in Windows? Are there any other differences?
Answer:
The main difference between the cp
and mv
command is that the cp
command copies the files from one folder to other while the mv
command moves the file from one folder to other. When mv
is used, the file is removed from source while cp
retains the file at the source and creates a copy of it on the new location.
Let’s understand this in depth-
Understanding the Difference between cp
and mv
commands in UNIX
UNIX, and by extension its descendants such as Linux and MacOS, provide users with robust, powerful, and flexible tools for managing files and directories through command-line interfaces like the Bash shell. Two of these basic but incredibly versatile commands are cp
and mv
.
What is cp
command?
cp
, short for copy, is a UNIX command used to copy files or directories from one location to another. It's as simple as it sounds, but it provides more functionality than just duplicating a file or directory.
The general syntax of the cp
command is as follows:
cp [options] source destination
Here, source
is the file or directory that you want to copy, and destination
is the location where you want the copy to be placed.
For example, if you want to copy a file named file1.txt
from the current directory to another directory named /home/user/documents
, you would type:
cp file1.txt /home/user/documents
Options with cp
There are various options that you can use with the cp
command to customize its behavior, some of which include:
- -i
or --interactive
: With this option, the cp
command will ask for your confirmation before it overwrites a file.
- -r
or -R
or --recursive
: This option allows you to copy directories recursively, i.e., it copies the directory and its entire contents (including any subdirectories and their contents).
- -v
or --verbose
: This option will make cp
describe what is being done, which can be useful for debugging or for scripts where you want more information outputted.
What is mv
command?
mv
, short for move, is another UNIX command that's primarily used to move files and directories from one location to another. While the primary function is to move, it also can be used to rename files or directories.
The general syntax of the mv
command is:
mv [options] source destination
The source
is the file or directory you want to move, and the destination
is the location where you want the file or directory moved.
For instance, to move the file file1.txt
from the current directory to the directory /home/user/documents
, you would type:
mv file1.txt /home/user/documents
This command also works for renaming files. To rename file1.txt
to file2.txt
, you would type:
mv file1.txt file2.txt
Options with mv
As with cp
, there are various options that can be used with the mv
command, including:
- -i
or --interactive
: Like cp
, the mv
command will ask for your confirmation before overwriting any files when used with this option.
- -u
or --update
: This moves files only when the source file is newer than the destination file or when the destination file is missing.
- -v
or --verbose
: As with cp
, this makes mv
describe what is being done, which is useful for debugging or understanding what's happening.
Differences between cp
and mv
In summary, following are the main differences between cp and mv commands in UNIX-
Primary Operation:
cp
makes a duplicate of the file or directory at a new location while keeping the original intact. In contrast,mv
moves the file or directory to a new location or changes its name, but does not create a copy.Impact on Inodes: An inode in UNIX-based systems represents metadata about a file or directory. When you use
cp
, it creates a new inode for the copied file or directory.mv
, on the other hand, keeps the same inode as it only modifies the location or name without creating a new file.Speed:
cp
can be slower thanmv
when dealing with large files or directories, ascp
has to read
Replies
-
Prasad AjinkyaBiggie,
The cp command will copy one file to the another destination without deleting the original. The mv command will move the file to the other destination (copy the file to the destination and then delete the original). Same concept behind the difference of "Copy and Paste" v/s "Cut and Paste".
It gets more complex, when you consider these commands with the folders instead of files. -
Kaustubh KatdareYep, I'm aware of that difference. I want to know if there's more to cp & mv commands, while working with files?
Thanks for the response. -
Prasad AjinkyaYes, try man pages for detailed dope. But you start seeing the nuances when you do commands like
cp -r /home/kidakaka/mycrap/* /newfolder/newfolder/newfolder/newfolder
mv -r /home/kidakaka /home/prasad
Or in a different way ... eg. using mv as a renaming tool -
Kaustubh KatdareI see! Thanks a ton for your response.
-
sriramchandrkHi,
You need to know about unix file system.
We should know the data structure in it i.e., superblock, inodes, databolocks.
mv and cp are just operations on them!
Its long time since i read it, i have totally forgot, i can just give a link to the text book!
Design of Unix Operating System - Maurice J. Bach
Will read today and let you know the right answer.
Thanks & Regards
Sriram -
Kaustubh KatdareThat will be great. I only want to know if there's much to mv and cp than 'cut' and 'paste'.
You are reading an archived discussion.