Linux / Commands / cp - command to copy, mv - command to move
In Linux, cp copies files or directories, and mv moves files or directories or renames them. The outcome of both commands differs depending on whether the final path is an existing directory, so you should check the target path before executing.
Difference between copy and move
The basic formats of the two commands are cp source destination and mv source destination. Copying leaves a separate copy of the original, but moving or renaming removes the item from the original path after success. The following examples are based on GNU coreutils' cp and mv.
Existing files can be overwritten with the same name, so check the path and target content and prepare necessary backups before handling important data. The practice below is conducted in a newly created empty directory.
Preparing practice files
The following is an example when the cp-mv-practice directory does not yet exist in the current location. If a directory with the same name already exists, practice in a different empty location. If the first command fails, do not proceed with the subsequent commands.
mkdir cp-mv-practice
cd cp-mv-practice
printf 'Hello Linux\n' > note.txt
mkdir target
ls
The note.txt file and an empty target directory are displayed. The subsequent examples are executed in order in this directory. > of printf will overwrite contents if a file with the same name exists, so use it in a location without actual data.
Copying Files
The following command copies note.txt to copy.txt in the current directory. If successful, both the original and the copy will exist.
cp note.txt copy.txt
ls
cat copy.txt
cat shows the contents of the copy, Hello Linux. The following command copies it into the existing directory target with the original name.
cp note.txt target/
ls target
If the specified target name is not an existing directory, a file with that name may be created or an existing file may be overwritten. Therefore, if target is a directory, cp note.txt target copies the file into it, but if it does not exist, a file named target will be created. To copy it under a different name inside a directory, specify the full target path as shown in cp note.txt target/other.txt.
Copying Directories
Use the recursive option -r when copying a directory and its contents. The following copies the previously created target to target-copy, which does not yet exist.
cp -r target target-copy
ls target-copy
As a result, target-copy/note.txt is created and the original target/note.txt remains. Conversely, if the target directory already exists, the original directory is usually placed inside it. For example, executing cp -r target target-copy again may create target-copy/target. Check the target with ls before running.
GNU cp -a tries to preserve attributes such as links, permissions, and timestamps as much as possible when performing recursive copies. Not all attributes are guaranteed to be preserved depending on permissions or the support of the target file system. The handling of symbolic links also varies depending on options, so check important data after copying. For more details, refer to the GNU cp manual.
Moving and Renaming Files
The following command renames the previously created copy.txt to renamed.txt. If successful, copy.txt is gone and renamed.txt is displayed.
mv copy.txt renamed.txt
ls
Next, move the file to an existing directory. renamed.txt no longer exists at its original location, and target/renamed.txt is created.
mv renamed.txt target/
ls target
When moving or renaming a directory, use mv, and -r of cp is not required. If the target directory already exists, it moves into it, and if the target does not exist, it moves with the specified name. If it conflicts with an existing non-empty directory, it may fail, so do not assume it merges like copy.
Between different file systems, GNU mv can handle it by deleting the original after copying. Large data may take time, and if it fails in the middle, only part of it may move, so check the results on both sides. You can check the detailed conditions in the GNU mv manual.
Overwrite and path error checks
To receive confirmation before overwriting an existing file, use -i. The following is a practice scenario where a file with the same name already exists. When instructions appear, read them and approve only if you intend to overwrite.
cp -i note.txt target/note.txt
mv -i also asks before replacing an existing target file. Interactive confirmation does not substitute for conflict handling in automated scripts, so in scripts, separately check the intended behavior and exit status. The default behavior may differ in environments where commands are set as aliases.
- When there are spaces in the path: Enclose each path in quotation marks as in
cp "my file.txt" "target/my file.txt". - When a file or directory cannot be found: Check the current location, spelling, and case using
pwdandls. - When a permission error occurs: Verify that you have read permission for the source and write permission for the destination, and also check the required permissions in the source directory when moving. If you do not have permissions, request them from the administrator.
After checking the practical results, if necessary, you can remove the cp-mv-practice directory using a file management tool. There are also tools like Midnight Commander that allow copying and moving files while viewing the file list, but whether they are installed on the server and the usage permissions depend on the environment.




