cp is a file management tool used to copy files and directories from one location to another. It can be used to create backups, duplicate files, move files between directories, and more.
Basic Syntax
cp [options] source destination
Here, source specifies the file or directory to be copied, and destination specifies the name and location of the copied file or directory. If the destination file already exists, it will be overwritten by default.
Commonly Used Options
-
-r: This recursively copies directories and their contents. -
-i: This prompts the user before overwriting an existing file. -
-u: This copies only files that are newer than the destination file. -
-p: This preserves the file attributes and permissions, such as the owner, group, timestamp, and mode. -
-v: This displays verbose output, showing the files being copied and their progress. -
-n: This does not overwrite an existing file, but skips it instead. -
-l: This creates a hard link to the source file instead of copying it.
Useful Examples
-
cp file1.txt file2.txt: This copies the contents offile1.txttofile2.txt. -
cp -r dir1 dir2: This recursively copies the contents ofdir1and its subdirectories todir2. -
cp -i file1.txt file2.txt: This prompts the user before overwritingfile2.txtwith the contents offile1.txt. -
cp -u file1.txt dir1: This copiesfile1.txttodir1only if it is newer than the existing file. -
cp -p file1.txt dir1: This copiesfile1.txttodir1while preserving its attributes and permissions. -
cp -v file1.txt dir1: This copiesfile1.txttodir1and displays verbose output. -
cp -n file1.txt dir1: This copiesfile1.txttodir1only if there is no existing file with the same name. -
cp -l file1.txt link1.txt: This creates a hard link tofile1.txtnamedlink1.txt.
Leave a Reply