Linux cp Command: Copy Files and Directories with Examples
The cp command in Linux copies files to other files or to directories. The cp command can copy one file or multiple files.
The most common options are:
- -d: Preserves links when copying files;
- -p: Preserves all file attribute information, such as file owner, group, permissions, and date;
- -R: Copies files recursively. Useful for copying files and directories below the specified directory;
- -a: Does the same as the combined "-dpR" options;
- -f: Forces the copy by overwriting the destination;
- -i: Prompts the user before overwriting each file at the destination location;
- -v: Shows the name of each copied file.
Examples:
Copies arquivo1 to arquivo2 in the same directory:
$ cp arquivo1 arquivo2
Copies arquivo1 to the /tmp directory:
$ cp arquivo1 /tmp
Copies arquivo1 located in the /tmp directory to the local directory. The "." (dot) symbol specifies the local directory:
$ cp /tmp/arquivo1 .
Copies all the contents of the /var/log directory, including all subdirectories, to the "loglocal" directory:
$ cp –Rf /var/log/ loglocal
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Did you like it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.