Linux Cp or Copy Command

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-cp-or-copy-command/
PINTEREST
PINTEREST
INSTAGRAM

Linux popular command cp is used to copy files and folders. In this tutorial we will look different usage types of cp command.

Syntax

Syntax of cp command is like below.

Copy

We will just copy our source file to the destination file.

We have copied our source file cat into new file cat2

We have copied cat and cat2 files into the test directory

Copy File and Give New Name

While copying the source file name is used as destination filename. If we need to rename newly created/copied file. We have to explicitly specify new name like below.

In this example we will copy file named test and new file name will be test5

Verbose Mode See Details

While copying bulk and a lot of files we may want to see details or save the details into a log file. It can be achieved with -v option like below

LEARN MORE  Default Location of Linux History File ".bash_history"

Preserve File Attributes

While copying if we do not want to change modification data, time, access list and associated information about file we can use -p flag

Copying Multiple Files

Sometimes we need to copy all files in a given path to the other path. In these situations we can use glob * which is provided by Linux bash. This will copy all contents to the given new path. In this example we will copy all files in this current working directory to the new path named new/ . We can also specify full path like /home/poftut .

LEARN MORE  How To Use Virt-Manager, Libvirt With Normal User Without Root Privileges and Without Asking Password

Recursive Copy

Normally cp command copies only files in the current directory. If we have files and folders to copy cp command do not copies sub files and directories. We can enable recursive copy with -r option. This will copy all sub files and folders in given path.

Avoid Overwriting

By default cp command will overwrite same files on the destination path. If we are working in a critical folder we can avoid overwriting with -i . It will ask question is destination has same file or folder.

Copy Symbolic Links

Symbolic links are used to  bind differents paths. Normally cp command do not process the symbolic link contents and do not copy. We can enable copying symbolic link contents with -s option.

Create Hard Links

cp command can be used create hardlinks. Hard Link is link where source and destination files shares same data. Any change one of them will affect all source and destination file. We will provide -l which is used create hardlinks instead of copying them.

Create Soft Links

We can also use cp to create Soft Links. Soft links provides links to the source file and only source file change will affect all soft links.

LEARN MORE  How To Avoid Not Signed Error For Yum

Copy Only Newer and Unexisting Files

This feature can be used to copy only files those are newer in the source and unexisting files. This is very useful for backup or sync purposes. We will use -u option for this feature.

Prevent Overwrite or No Clobber

We can strictly prevent overwriting files in the destination. We will use n option in order to no clobber.

Preserve All File Attributes

Files and folders have some file system and operating system related attributes. While copying some of them are changed according to the destination path. We can preserve these attributes with --preserve=all option like below.

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-cp-or-copy-command/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)