cp Command

Copy command
Copy source to target, or multiple sources to directory.
Last one is target except changing with -t parameter.
-r parameter, copies directories recursively.

Basic Usage:
cp -r source target

Examples:
cp aaa.txt folder/
We open terminal at aaa.txt path. With this command aaa.txt copies to folder. If there is aaa.txt in folder, cp overrides.

cp /home/user/documents/bbb.txt /home/user/documents/ccc.txt /home/user
bbb.txt and ccc.txt will copy to user folder. If a file can't find, other file copies and shows an error message for unfinding file.

cp aaa.txt ../aaa.txt
aaa.txt copies one up directory except it's in root directory. Overrides, if there is a same file in directory.

cp ccc.txt ~/documents
ccc.txt file copies to documents folder at user's home directory. ~ signs user's home directory.

cp !(aa|bbtxt|cc.txt) aa
All files at same path copy too aa folder, except aa, bb.txt and cc.txt.

cp *.txt ??a?
All txt files at current directory copy to folder which has four character and its third character is a. If there is one more folder which is appropriate to description, cp doesn't copy and gives a warning.

cp * ../aaa1
All files in current directory will copy to aaa1 folder which in one up path. If there are directories, cp shows a warning on screen about they can't copy.

cp -a aa empty
-a or --archive parameter same as -dR --preserve=all.
-d parameter same as --no-dereference --preserve=links
-P or --no-dereference parameter never follow symbolic links in SOURCE
aa folder copies to inside of empty folder. Original properties and features are saved.

cp --attributes-only source.txt target.txt
--attributes-only, don't copy file data, just attributes. It can't change attributes of current file. It can't change attributes of current folder with -r parameter.
it makes target.txt named file attributes same as source.txt file but content is empty. If there is target.txt file already, cp command can not change any effect for this file.

cp -r --copy-contents source target
--copy-contents, effects with -r parameter. It uses to copy special files as FIFOs and device files etc. There is no its effect for copying of sembolic link.
Copy source named folder with all files and folders to target folder. Special files copies too.

cp -i bbb.txt asa/bbb.txt
-i or --interactive, asks before override. It uses with -r parameter for copying folder.
It asks for overriding for bbb.txt file copies to bbb.txt in asa folder and accepts answer.

cp ddd.txt ddd2.txt -l
-l and --link, hard link copy. If you change ddd2.txt file, ddd.txt file change automatically after copying process. If you change name of file feature can't fail feature, but can't change other's name. Interaction is about content. It can't make any effect for copying of folders.
This command makes ddd2.txt named a file has interaction at same path. Before copying if there were ddd2.txt named a file at same path, we would take an error message.

cp -L ddd.txt ddd2.txt
-L or --dereference, copies follows symbolic links source and copies it.
The file which ddd.txt's symbolic link copies as ddd2.txt

cp --parents aa/aa/333.txt empty
--parents, use full source file name under directory.
333.txt copies as empty/aa/aa/333.txt

cp --preserve=all asa.odt 12.odt
--preserve[=ATTR_LIST], preserve the specified attributes. (There are mode,ownership,timestamps attributes as default) If you wish you can add these attributes: context, links, xattr, all
asa.odt file copies as saving all attributes.

cp -r bbb asa/aaa
-R or -r or --recursive, copy directories recursively.
bbb folder and its all content copy to asa/aaa folder.

cp asa1.txt asa3.txt -s
-s or --symbolic-link, make symbolic links instead of copying. When symbolic links move to different directory, they follows same symbolic links.
cp command makes as a symbolic link asa3.txt named.

cp -t bbb ccc.txt ddd.txt
cp ccc.txt -t bbb ddd.txt
cp ccc.txt --target-directory=bbb ddd.txt
-t or --target-directory=DIRECTORY: Target file is -t parameter file. Target can't be a file for this parameter.
ccc.txt and ddd.txt files copy to bbb folder for above three commands.

cp -T bbb.txt ccc.txt
-T or --no-target-directory, treat target as a normal file. It removes content of target and overrides. If this parameter can't give with -r parameter, target can't be a folder.
content of bbb.txt file overrides content of ccc.txt file. It removes past content of ccc.txt file. If there isn't ccc.txt, it makes a new ccc.txt file.

cp -v bbb.txt asa/
We can ask for computer "explain what is being done" with -v parameter
If copying finish successfuly, it gives us info as `bbb.txt' -> `asa/bbb.txt'
If copying fails, there is not any warning message on screen.

cp --version
Show version information.
cp --help
Show help page.


Above process applied on "GNU bash, version 4.3.11(1)-release (i686-pc-linux-gnu)"
Date of Article: 21.02.2016




HOME GNU/LINUX Commands TURKISH