mv and cp

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
3,490
Reaction score
3,224
Credits
31,265

Differences Between mv and cp in Linux​

In Linux, mv and cp are two fundamental commands used for moving and copying files and directories. While they may seem similar, they serve different purposes and have distinct behaviors.

Renaming Files with mv and rename​

Both mv and rename can be used to rename files. Here are examples of how to do this:

  • Using mv to rename a file:
    Code:
     mv oldname.txt newname.txt
  • Using rename to rename files with a pattern:
    Code:
     rename 's/oldname/newname/' oldname.txt

Copying Files with cp​

The cp command copies files or directories from one location to another, leaving the original file in its original location. For example:
Code:
 cp file.txt /path/to/destination/

Moving Files with mv​

The mv command moves files or directories from one location to another, removing the original file from its original location. For example:
Code:
 mv file.txt /path/to/destination/

Using mv to move a file is effectively the same as using cp to copy the file and then running rm to delete the original file, but it does this in one command:
Code:
 mv example.mp3 /home/user/Music
is equivalent to:
Code:
 cp example.mp3 /home/user/Music 
rm example.mp3

Destination Directory Naming​

When using mv or cp to move or copy files to a different directory, the destination must end with the directory name. Otherwise, it will rename your file to whatever you thought the name of the directory was. For example:
Code:
 mv file.txt /path/to/destination/
If /path/to/destination/ is a directory, file.txt will be moved there. If it's not, file.txt will be renamed to destination.

Recursive Copy with cp​

To copy directories recursively, use the -r flag:
Code:
 cp -r sourcedir /path/to/destination/

Using cp with -rvf Flags​

The -r, -v, and -f flags stand for recursive, verbose, and force, respectively:
Code:
 cp -rvf sourcedir /path/to/destination/

  • -r: Recursively copy directories.
  • -v: Verbose mode, showing files being copied.
  • -f: Force overwrite of existing files without prompting.

Clobbering and Overwriting Files​

When copying files, be aware of clobbering or overwriting files in the destination directory with the same name. Usually, these commands will prompt you and ask what you want to do.

Dangers of Recursive Copy with Symbolic Links​

Using the recursive cp flag on directories with symbolic links to other directories can lead to unintended copying of large amounts of data. Be cautious and consider using the -P flag to not follow symbolic links.

Installing rename​

To install rename on Debian-based systems:
Code:
 sudo apt-get install rename

On Red Hat-based systems:
Code:
 sudo yum install prename

Relative Paths and .. Shortcuts​

Relative paths allow you to navigate the filesystem without specifying the full path. The .. shortcut moves up one directory level. For example, if you are in /home/user/.local/share/myapp/mysubdir and want to copy a file to /home/user/.local, you can use:
Code:
 cp myfile.txt /home/user/.local/
or
Code:
 cp myfile.txt ../../..
Each .. moves up one directory level.

Hidden Files (Dot Files)​

Hidden files in Linux start with a dot (.). You can see them using ls -a:
Code:
 ls -a
Hidden files are often configuration files, and their hidden status helps protect them from accidental deletion. For example, running rm * in /home/user will not delete hidden files like .bash_history, .bashrc, .profile, etc.


I hope this article helps clarify the differences between mv and cp.
 


To create a hidden file, you simply type a period "." as the first character of the filename.
You can do this yourself.

Code:
cd ~
touch .hidden_file
ls

If you just use ls, you will not see the file.

Code:
ls -a

If you use the -a flag, then you will see it (and all the other hidden files as well ).
You can remove hidden files.

Code:
rm .hidden_file

But be careful not to remove anything important.
 
Last edited:
To create a hidden file, you simply type a period "." as the first character of the filename.
You can do this yourself.

Code:
cd ~
touch .hidden_file
ls

Don't learn people to make unnecessary use of the CD command;
use parameters for the commands you really want:

Code:
touch ~/.hidden_file
ls ~
 
The most interesting option for CP is the -P option as that preserves metadata like the last modif date

Maybe we call have some discussion about this as well but in my mind the metadata (such as last modif date) should also be copied when copying files, but in Linux that is not the default option. By copying it sets the date of copy (for the copied file) but that is technically incorrect as the content of the file was NOT changed and the Last Modif Date should also not change for a 100% identical copy of the file. In fact, it shouldn't even be possible to change the modif date, but for CP it's the default action unfortunately. If you want to change the modif date of any file, the TOUCH command was invented for that.
 


Members online


Latest posts

Top