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
Keep or drop attributes
Links
Target and backups
Special cases
Niets past bij dit filter.
Voorbeelden 12
$ als gewone gebruiker# als root (of met sudo)
1Copy a file before you change it
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bakAlways keep a copy of a config file before an edit.
2Copy a folder with all its content
cp -r ~/project /tmp/Without -r, cp skips folders.
3Make a full backup that keeps owners and permissions
cp -a /var/www/html /backup/html-$(date +%F)-akeeps everything, also SELinux labels and links.4Copy the content of a folder, hidden files included
cp -a /etc/skel/. ~/newhome//.at the end means "the content", including dotfiles.5Copy many files into one folder
cp *.log /tmp/logs/Wildcards pick the files; the last argument is the folder.
6Copy only newer files
cp -ru ~/docs/* /mnt/usb/docs/Only files that changed are copied again.
7Never overwrite existing files
cp -n templates/* /etc/myapp/Keeps the files that are already there.
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.
9Copy all files of a user (common exam task)
find / -user student -type f -exec cp -a --parents {} /root/found/ \; 2>/dev/null--parentskeeps the paths, so files with the same name do not overwrite each other.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
mvwould keep the old, wrong label.11Keep numbered backups when overwriting
cp --backup=numbered nginx.conf /etc/nginx/nginx.confThe old file becomes nginx.conf.~1~, then .~2~ and so on.
12Fast copy of a big disk image on XFS
cp --reflink=always vm.qcow2 vm-clone.qcow2A reflink copy takes almost no time and no extra space until one copy changes.
Niets past bij dit filter.
Let op
cpoverwrites without a question for normal users. Use-ior-nwhen 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
-akeeps the old label. For web content that is usually wrong: userestoreconafter.
Tips om te onthouden
- File:
cp. Folder:cp -r. Backup:cp -a. - Before an edit:
cp file file.bak.