cp [ options ] [ source ] [ destination ]
- File to be copied. You can specify more than one file to be copied using “Wildcards” (see [ch-bas.html # s-basics-Wildcards, Section 2.12]).
- The path or filename where it will be copied. If the destination is a directory, the source files are copied into the directory.
- i, –interactive
- Question before replacing an existing file.
- -f, –force
- No question, replace all the files if it already exists.
- Copies files from directories and subdirectories from source to destination. It is recommended to use -R instead of -r.
- -R, –recursive
- Copies files and sub-directories (such as the -r option) as well as special FIFO files and devices.
- -v, –verbose
- Displays files while copying.
- -s, –simbolic-link
- Create symbolic link instead of copy.
- -l, –link
- It links to the destination instead of copying the files.
- -p, –preserve
- Preserves attributes of the file, if possible.
- -u, -update
- Copies only if the source file is newer than the destination file or when the destination file does not exist.
- It does not copy files that are located on a file system other than where the copy started.
- Copy all artibutos (preserving protection, owner and dates)
- –preserve = timestamps
- It preserves, of the attributes, only those related to the date (we will see more below when to use)
cpcopies files from ORIGIN to DESTINATION. Both source and destination will have the same content after copying.
- cp test.txt test1.txt
- Copy the file
- cp test.txt / tmp
- Copies the file
teste.txtinto the directory
- cp * / tmp
- Copies all files from the current directory to
- cp / bin / *.
- Copies all files from the directory
/binto the directory in which we are currently located.
- cp -R / bin / tmp
- Copy the directory
/binand all existing files / sub-directories to the directory
- cp -R / bin / * / tmp
- Copies all files in the directory
/bin(except the directory
/bin) and all files / sub-directories within it to
- cp -R / bin / tmp
- Copies all files and directory
- cp -auv / home / user1 / folder1 / * / home / user2 / folder2 /
- Copies all files (and sub-folders) that are in a user’s account to a folder in another. Requires writing privileges on the target directory and probably on the source directory – since in preserving owner, the command will leave the destination with the original owner (user1) instead of the desired owner (user2)
- cp -uvR –preserve = timestamps / media / CDROM / meubackup / * / home / meubackup /
- As we saw above, the natural command to restore a CD or DVD backup would be cp -auv; but here a bug occurs : when starting the copy with the option to preserve the protection attributes, as the CDs and DVDs are read-only , these options are marked in the created folders, preventing the copying of the following files. So the solution is to ignore the attributes except the date (by changing the -a option to the –preserve = timestamps option ) and adding the -R option (to recursively sub-directories).