cp Command to Copy a File in Linux
cp [source] [destination]
Description of cp Command to Copy a File in Linux
You can use the cp command to copy a file in Linux as well as a folder of any kind to the same working directory of any other destination directory. The use of absolute and relative path is also necessary as per requirement. In the subsequent content of this article, we will scrutinize the cp Command according to the following state of the home directory with the help of numerous examples. Following color coding is used to describe better command to copy a file on Linux.
- Bold Font with blue color – commands and options
- Italic Font with red color – Folder or directory
- Bold Italic Font with green color – Files
As per snapshot above, present working directory is /home/bilwadal. And, the list of folders and files are displayed under ls (list) command.
Copying a Single File in a Folder within same Location
cp accounts pune/
Description : In the above command line, a file named “accounts” has been copied to a directory named “pune“. On listing of directory “pune“, we can see the file “accounts” also placed in it. Relative path may be used in this case.
Copying a Single File in Folder of Another Root Location
cp audio /tmp/delete/
Description : A file called “audio” is copying in the directory named “delete” placed in root path “/tmp/delete“. Absolute path must be given in this case.
Copying Multiple Files to a Folder of same Location
cp accounts bill billo bwc letter.txt office/
Description : Copy all the files “accounts“, “bill“, “billo“, “bwc” and “letter.txt” into the folder named “office“. Only the last word will be assumed as destination folder or file.
Copying Multiple Files to a Folder of another Root Location
cp accounts bill billo bwc letter.txt /tmp/delete
Description : Copy the files “accounts“, “bill“, “billo“, “bwc” and “letter.txt” into the folder “delete” of a route directory “/tmp/delete“.
Copying all the Text files into Folder of same Location
cp *.txt raipur/
Descripiton : Copy all the txt files into the folder named “raipur“. Here, sign * denotes all the files and “.txt” denotes the files having extension of “txt”. Hence, only all those files with ‘txt’ extension have been copied.
Copying a File into another file
cp linux profile
Description : File “linux” copied into file “profile“. The content of file “profile” have been erased and content of file “linux” have been written in this file “profile“. The content of the new file will be replaced by the older file.
Copying a File into another File
cp profile accounts
Description : There was separate content in both the files “accounts” and “profile“. But, after copying file “profile” into file “accounts“, the content of “accounts” file have been replaced with content of “profile” file. It means, after copying, the original content of the file will be removed.
Creating a Backup File when Copying a File into another File
cp -b profile ticket
Description : Here, “-b” option used with “cp” command to create an automatic back file of “ticket“, because, the content of file “ticket” is being replaced with content of file “profile“. On checking of “list“, you can see that a backup file “ticket~” has been created automatically as backup file of “ticket“.
Copying a Folder into another Folder
cp -r ajay mohan/
Description : -r is a option of “cp” command. “-r” always used for copying a folder to another location. All the files and folders in the copying folder will also be copied.
Copying all the files starting with letter “s” into folder “ajay“
cp -r s* ajay/
Description : There is a folder named “ship” also. So, to copy all files and folders starting with letter “s”, “-r” option used with “cp” command. s* means all the files whose names start with letter “s”.
Copying all Files and Folders into a Backup Folder
cp -r * backup/
Description : “-r” used for copying folders, “*” used for all the files and folders. There is a error report shown but actually, the folder has been created.
Getting an Alert before Overwriting a File
cp -i profile lotery
Description : On using this “-i” option with “cp” command, an aleart message will be produced by the system and asked you to confirm if actually you want to overwrite the file “lotery“. You can submit your answer in Y or N. Accordingly the file will be copied.
Preparing a New File which make links with an Old File
cp -l audio myaudio
Description : There was a file named “audio” in the home directory. With use of “-l” (small L) option with “cp” command, a new file created as “myaudio” which will be a linked file of file “audio“. Means, whatever we will add in the new created file “myaudio“, the content automatically ammed or added with the older file “audio“.
Visual Confirmation of Copying Each Files
cp -r -v office pune
Description : There are five files in a folder “office” and no file exists in the folder “pune“. After apply “-r” and “-v” option with “cp” command, the folder “office” with its all files coppied into folder “pune“. Also the system visually confirmed how all the files of folder “office” copied into folder “pune“.
Copying Files without Changing its Property
cp -p letter.txt sample2.txt
Description : Before copying, the property of file “letter.txt” shown as its creation date 25 Apr at 15:40 hr. After applying simple “cp” command to copy this file into “sample1.txt“, the property of the file “sample1.txt” shown as creation date 27 Apr at 21:31 hr. But, after applying “-p” or “–preserve” option with “cp” command, property of the newly created file “sample2.txt” not changed. It means the property of the file has been preserved.
Getting Help on the cp Command in Linux
Description: Use of cp command in Linux with all its options have been well explained in this command. For detailed information about the cp Command in Linux, you can go with this option to elaborate the cp Command deeply.
To know all about mkdir command in Linux, you can visit here.