How to copy files from one directory/folder to other in linux?

The command cp is used to copy the files/folders from one place to other. Using multiple options, you can copy files/folders effectively.

The syntax for cp command is:
cp <source> <destination>

Below are the options used along with the cp command.

Option Description
-a To archive files by preserving their attributes.
-b To create a backup of each existing destination file instead of overwriting it.
-f To force the overwriting of existing destination files without prompting.
-i To prompt before overwriting destination files.
-l To create a file link instead of copying the files.
-p To preserve file attributes if possible.
-r To copy files recursively.
-R To copy directories recursively.
-s To create a symbolic link instead of copying the file.
-S To override the backup feature.
-u To copy the source file only if it has a newer date and time than the destination (update).
-v To list the process in verbose mode explaining what’s happening.
-x To restrict the copy to the current filestytem

 

Note : You can copy the files/folders from one directory to other directory if you have enough permissions.

In the below example, copied file100.txt into file101.txt

cp file100.txt file101.txt

To copy a directory from one path to other path, follow below format.

cp -pr <source directory path> <destination path>

For example,

cp -pr /test/directory/* /tmp/

In the above example, all the files under /test/directory/ will be copied recursively into /tmp/ directory with same file attributes.

Note :
1. After copying a file, the inode number of new file will not be same as original file.

2. To copy the file with metadata or preserve the permissions of original file, you can use the option “–p” with cp command.

To know how to move files or folders from one location to other, Click here!!

How do you feel about this post or want to suggest a new post? Drop your comments below..