Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

cp

Copy files and folders.

Niveau
RHCSA-examen
Groep
Bestanden en mappen
Root nodig
Nee
Pakket
coreutils

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

Wat het doet

cp SOURCE TARGET copies a file. When the target is an existing folder, the copy keeps its name inside that folder. Several sources can go into one folder: cp a b c /dir/.

Folders need -r. For backups use -a (archive): it copies recursively and keeps permissions, owners, times, links and the SELinux context.

On RHEL, root has the alias cp='cp -i', so it asks before it overwrites a file.

Zo schrijf je het

cp [OPTION]... SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...

Opties 35

Meest gebruikt

Most used

-r, -R, --recursive
Copy folders with everything in them.
-a, --archive
Archive copy: recursive and keep everything (same as -dR --preserve=all).
-p
Keep mode, owner and times (same as --preserve=mode,ownership,timestamps).
-i, --interactive
Ask before overwriting a file.
-v, --verbose
Print each file that is copied.
-u
Copy only when the source is newer than the target (or the target is missing).
-n, --no-clobber
Never overwrite an existing file.
-f, --force
If the target cannot be opened, remove it and try again.

Keep or drop attributes

--preserve[=<list>]
Keep these attributes: mode, ownership, timestamps, context, links, xattr, all.
--no-preserve=list
Do not keep these attributes.
--attributes-only
Copy only the attributes, not the data.
-Z
Give the copy the default SELinux context of the target place.
--context[=<ctx>]
Like -Z, or set this exact context.

Links

-d
Copy symbolic links as links (same as --no-dereference --preserve=links).
-P, --no-dereference
Never follow symbolic links in the source.
-L, --dereference
Always follow symbolic links in the source (copy the target file).
-H
Follow symbolic links given on the command line only.

Target and backups

-t, --target-directory=dir
Copy all sources into this folder.
-T, --no-target-directory
Treat the target as a normal file, never as a folder.
--parents
Use the full source path under the target folder (cp --parents a/b/c /x → /x/a/b/c).
--backup[=<control>]
Make a backup of each target that would be overwritten (numbered, existing, simple, none).
-b
Like --backup, without an argument.
-S, --suffix=suffix
The ending of backup files (default ~).
--remove-destination
Remove each existing target before copying.
--update[=<which>]
Control which existing files are replaced: all, none, none-fail, older.

Special cases

-x, --one-file-system
Stay on this file system.
--sparse=when
Keep sparse files sparse: auto, always, never.
--copy-contents
Copy the content of special files (like devices) when recursive.
--strip-trailing-slashes
Remove the slash at the end of each source.
--debug
Explain how each file is copied (implies -v).
--help
Show help.
--version
Show the version.

Voorbeelden 12

$ als gewone gebruiker# als root (of met sudo)

  1. 1Copy a file before you change it

    cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak

    Always keep a copy of a config file before an edit.

  2. 2Copy a folder with all its content

    cp -r ~/project /tmp/

    Without -r, cp skips folders.

  3. 3Make a full backup that keeps owners and permissions

    cp -a /var/www/html /backup/html-$(date +%F)

    -a keeps everything, also SELinux labels and links.

  4. 4Copy the content of a folder, hidden files included

    cp -a /etc/skel/. ~/newhome/

    /. at the end means "the content", including dotfiles.

  5. 5Copy many files into one folder

    cp *.log /tmp/logs/

    Wildcards pick the files; the last argument is the folder.

  6. 6Copy only newer files

    cp -ru ~/docs/* /mnt/usb/docs/

    Only files that changed are copied again.

  7. 7Never overwrite existing files

    cp -n templates/* /etc/myapp/

    Keeps the files that are already there.

  8. 8Keep the folder path of the source

    cp --parents /etc/ssh/sshd_config /etc/sudoers /root/config-backup/

    Result: /root/config-backup/etc/ssh/sshd_config and /root/config-backup/etc/sudoers.

  9. 9Copy all files of a user (common exam task)

    find / -user student -type f -exec cp -a --parents {} /root/found/ \; 2>/dev/null

    --parents keeps the paths, so files with the same name do not overwrite each other.

  10. 10Copy and fix the SELinux label for the new place

    cp -Z index.html /var/www/html/

    The copy gets the label of /var/www/html. A mv would keep the old, wrong label.

  11. 11Keep numbered backups when overwriting

    cp --backup=numbered nginx.conf /etc/nginx/nginx.conf

    The old file becomes nginx.conf.~1~, then .~2~ and so on.

  12. 12Fast copy of a big disk image on XFS

    cp --reflink=always vm.qcow2 vm-clone.qcow2

    A reflink copy takes almost no time and no extra space until one copy changes.

Let op

  • cp overwrites without a question for normal users. Use -i or -n when you are not sure.
  • cp -r dir1 dir2: if dir2 exists, you get dir2/dir1; if not, dir2 becomes the copy.
  • A copy gets the SELinux label of its new place by default, but -a keeps the old label. For web content that is usually wrong: use restorecon after.

Tips om te onthouden

  • File: cp. Folder: cp -r. Backup: cp -a.
  • Before an edit: cp file file.bak.