Cp Command:
The cp command lets you copy a file from one location to another on a UNIX machine.The cp command overwrites an existing destination file if it is with file contents.
Another use of cp is to create a link of a file.
Syntex:
cp [option] source destination
Source – may be source file name, file with path or directory name, directory with path.
Destination – may be destination file name, file with path or destination directory name, and directory with path.
Copy a file or directory from source to destination
cp A.txt B.txt
File A.txt is a source file and B.txt is a destination file, both are inside current directory. So it is not required to mention a path if both source and destination exist inside the same directory.
cp /home/my_doc/a/f.txt /home/back_up/
(Absolute Path)
Here the source and destination path is mentioned with filename as both exist in different locations. Absolute path is a path which starts from root.
cp ./F.txt ./../../back_up/
(Relative path)
Relative path is a path which starts from current directory.
Here .(dot) means current directory and ..(Double dot) means we are moving one step back/up in directory structure as shown in Fig.
Some examples of cp command:
cp *.txt ./A/B/
here We have to copy all text file (file with extension .txt) inside current directory to the destination directory B which is inside A and A is inside current directory.
QS: Why sometimes it is not possible to copy a file?
When the file to be copied is read protected or the destination directory is write protected, and then it is not possible to copy a file.
QS: How to copy a whole directory structure?
cp –R <path_of_dir/*> <path_of_destination>
Ex: cp –R ./my_dir/* ./dir_bkp/
Qs: How to copy a file without changing its modification time?
cp -p <source> <destination>
Qs: How to create a link using CP command?
cp –l
cp -s
Qs: how to copy only when the source file is newer than the destination and destination file is missing?
cp -u

No comments:
Post a Comment