The cp command is used in Linux to copy files and directories from one location to another. Here is a tutorial on how to use the cp command:
- Open a terminal window: To access the Linux command line, you can use the terminal application, which is usually found in the “Utilities” or “System Tools” section of the application menu.
- Change to the directory that contains the files you want to copy: To change to a different directory, use the
cdcommand followed by the path to the directory you want to change to. For example, to change to the/homedirectory, you would run the following command:
cd /home
- Copy a file: To copy a file from one location to another, use the following syntax:
cp source destination
where source is the path to the file you want to copy and destination is the path to the location where you want to copy the file. For example, to copy a file named file.txt from the /home directory to the /tmp directory, you would run the following command:
cp /home/file.txt /tmp
- Copy a directory: To copy a directory, use the same syntax as for copying a file, but include the
-roption to indicate that you want to copy the directory and its contents recursively:
cp -r source destination
For example, to copy a directory named dir from the /home directory to the /tmp directory, you would run the following command:
cp -r /home/dir /tmp
- Overwrite existing files: By default, the
cpcommand will not overwrite existing files. To force thecpcommand to overwrite existing files, you can use the-foption:
cp -f source destination
- Preserve file attributes: By default, the
cpcommand will preserve some file attributes, such as the modification time and permissions, but not others. To preserve all file attributes, you can use the-poption:
cp -p source destination
These are just a few examples of how to use the cp command. There are many other options and variations that you can use to customize the behavior of the command. To learn more about the cp command and its options, you can consult the cp manual page by running the following command:
man cp
