Difference between cp and mv commands in UNIX

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-

  1. 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.

  2. 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.

  3. Speed: cp can be slower than mv when dealing with large files or directories, as cp has to read

Replies

  • Prasad Ajinkya
    Prasad Ajinkya
    Biggie,

    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 Katdare
    Kaustubh Katdare
    Yep, 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 Ajinkya
    Prasad Ajinkya
    Yes, 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 Katdare
    Kaustubh Katdare
    I see! Thanks a ton for your response.
  • sriramchandrk
    sriramchandrk
    Hi,

    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 Katdare
    Kaustubh Katdare
    That 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.

Related Posts

hi i m 1st year student of instrumentetion can any one provides me help.
i wd like to knw a brief description about following s​ introduction of system modelling​ design levels​ register design metodology​ processor level design queing models​ simulation​
Im persuing my final year b-tech program. my final year project wat i hav choosen is to design speed-control of seperately excited dc drive both above rated as well as...
😁​T32_GetMessage​Prototype​​​:​ int T32_GetMessage ( char message[128], word * pmode );​Parameters​​:​ message pointer to an array of 128 characters to catch the message pmode pointer to variable to chatch the message...
Here's a puzzle for those who still have brain intact:- Write a method that takes a string as input, and outputs that string with the words in reverse order. Punctuation...