Technical skill

Linux command: cp

Subject: .../LINUX

Make a second copy of a file

A file is information saved under a name. To make a second copy while keeping the original where it is, use the Linux cp command. The source is what you copy; the destination is where the new copy goes. For example, cp ./notes.txt ./notes-copy.txt makes a copy beside the original. Check both paths first: a command can succeed and still place a file somewhere you did not expect.

For a small backup, create a folder and copy a file into it, then copy a whole folder tree:

mkdir -p ./backup
cp -i -- ./app.conf ./backup/app.conf
cp -a -- ./site-config ./site-config.backup

mkdir -p creates the backup folder if needed. The first cp asks before replacing an existing destination because of -i. The last command makes an archive-style copy of a directory tree. In GNU Coreutils, -a copies directories recursively, preserves symbolic links, and asks to preserve file attributes such as permissions and timestamps.

Choose where and what to copy

The basic form is cp SOURCE DEST. If DEST names a directory, cp puts the source inside it using the source's basename, the last part of its path. If DEST names a file, cp creates or replaces that file. Multiple source paths require a directory destination. GNU cp replaces an existing destination by default; -i asks before doing so. Put -- before paths that may begin with a hyphen, as in cp -- ./-draft ./draft-copy, so the command treats them as names rather than options. Quote paths with spaces to keep each path as one argument to the command.

Use -R or -r to copy a directory tree. Symbolic links are path references that point to another file or directory. GNU cp has -H, -L, and -P to choose how it follows source links. Recursive copies use -P by default and preserve links found inside the tree. For a command-line source that is itself a link, GNU cp normally follows it for a non-recursive copy. -a also selects link and attribute behavior; check the manual when those details matter. Inspect links with ls -l or find.

Check the result

Before a large copy, run pwd to see the current directory and inspect the exact source and destination. Review wildcards and variables, since they can expand to paths you did not intend. For one regular file, cmp SOURCE DEST checks whether contents match. For a directory tree, compare relevant files or use a recursive diff where available. Check ownership, group, permissions, timestamps, and links separately when they matter.

A copy may stop partway through if the destination is read-only, permissions are missing, the source cannot be read, or the filesystem is full. Read the status and error messages, then check for partial results before treating a tree as complete. GNU-specific options may not exist in another Unix implementation of cp; use the local manual when portability matters.

Interactive exercises

Materials

Skillsets
Skillset Skyeng QA Automation Engineer preview
image descriptionSkyeng QA Automation Engineer
by Aleksandra Kulikova
4 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset Skyeng Senior Backend Developer preview
image descriptionSkyeng Senior Backend Developer
by Artem Naumenko (slack2)
39 users
Skillset Linux for backend developer preview
image descriptionLinux for backend developer
by Artem Naumenko (fb)
16 users
Skillset Skyeng QA Automation Hard Skillset v09.21 preview
image descriptionSkyeng QA Automation Hard Skillset v09.21
by Aleksandra Kulikova
16 users
Show more
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful