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
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/
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/
Code:
cp myfile.txt ../../..
Hidden Files (Dot Files)
Hidden files in Linux start with a dot (.). You can see them using ls -a:
Code:
ls -a
I hope this article helps clarify the differences between mv and cp.