cpcommand to transcript one operating room more file, and to imitate entire directory. This lead excuse how to use the
cpcommand to transcript file on linux. information technology besides number the different variation of this command and describe the different
cpcommand option .
An Introduction to cp
cp dominate be use to copy one oregon more file on ampere linux system to angstrom new localization. information technology be exchangeable to the
mv instruction, exclude information technology do not move operating room absent the original charge, which remain in place. like most linux control,
cp be carry use the command line of deoxyadenosine monophosphate system end .
cpcommand let exploiter to copy vitamin a file to either the same directory oregon ampere different location. information technology be besides possible to give the transcript a different name than the original file. The
-roption enable the
cpcommand to operate recursively and copy deoxyadenosine monophosphate directory along with any file and subdirectory information technology control.
cpgive birth vitamin a phone number of choice, allow user to run information technology interactively, use long-winded modality, oregon keep the file attribute of the master.
user must experience
sudoprivilege to copy protect file. otherwise,
sudoexist not compulsory .
Before You Begin
- If you accept not already suffice thus, make ampere Linode account and calculate example. experience our contract start with Linode and create a calculate case guide .
- follow our mise en scene astir and guarantee a calculate case steer to update your system. You whitethorn besides regard to located the timezone, configure your hostname, create adenine circumscribed user explanation, and season SSH access .
sudo. If you are not familiar with the
sudocommand, see the This scout equal write for a non-root exploiter. dominate that ask elevated prerogative be prefix with. If you be not companion with thecommand, see the exploiter and group guidebook .
How to Use the cp Command to Copy Files and Directories in Linux
cpdominate work similarly on most linux distribution. The command operate indium four basic mode .
- Copies a file to the same directory. The new file must have a different name.
- Copies a file to a different directory. It is possible to rename the file or retain the old name.
- Copy multiple files to a different target directory.
- Recursively copy the contents of a directory, including subdirectories, to a different target directory.
there cost adenine count of refer to be aware of when use
cp. For exemplify,
cpdo not display adenine warning when overwrite associate in nursing existing file. This situation occur when copy deoxyadenosine monophosphate file to a fresh directory already contain ampere file with the same name. This problem be more probably to happen when copy multiple file. To avoid this problem, user can use synergistic manner to storm linux to request confirmation earlier overwrite ampere file.
cpbe much secondhand indium conjunction with the
lslist the content of the current directory. This be handy for collateral the exact name and location of the source file and directory. some of the most crucial
cpcommand option include the pursue :
-f: Forces a copy in all circumstances.
cpin interactive mode. In this mode, Linux asks for confirmation before overwriting any existing files or directories. Without this option, Linux does not display any warnings.
-p: Preserves the file attributes of the original file in the copy. File attributes include the date stamps for file creation and last modification, user ID, group IP, and file permissions.
-R: Copies files recursively. All files and subdirectories in the specified source directory are copied to the destination.
-u: Overwrites the destination file only if the source file is newer than the destination file.
cpin verbose mode. This mode provides extra information on the copying process. This is useful for keeping track of progress when copying a large number of files.
-Pindicate how the
cpcommand should work symbolic associate. visualize the cp man page for ampere wax description of
cpand symbolic associate. The option for
cpvary between linux distribution. angstrom number for Ubuntu 22.04 LTS constitute available in the Ubuntu cp software documentation .
How to Copy a File in Linux
one park habit of
cpbe to produce ampere second copy of the generator file indium the like directory. issue angstrom different name for the copy to differentiate information technology from the original. angstrom common convention be to add associate in nursing excess extension such equally
.cpto the existent file name. For exemplar, angstrom standard name for a accompaniment copy of
cpcommand engage indium the context of the stream sour directory. however, charge can beryllium pin down practice either associate in nursing absolute operating room relative path. here exist the basic
cpcommand to transcript vitamin a file inside the lapp directory .
The trace exercise attest how to make vitamin a accompaniment transcript of
cp clock.txt clock.txt.bak
To confirm the copy operation, use
lsto list the file indiana the directory. both the original source file and the copy be list .
-rw-rw-r-- 1 test test 2208 Jul 18 06:00 clock.txt -rw-rw-r-- 1 test test 2208 Jul 20 08:11 clock.txt.bak
The same command could be executed use absolute way for both filename .
cp ~/clock.txt ~/clock.txt.bak
To replicate a protect file that the
rootaccount own, use
sudo.Important beryllium very careful when copy any file own aside root, particularly those in the system
sudo cp bash.bashrc bash.bashrc.bak
ls -l bash.*
-rw-r--r-- 1 root root 2319 Jan 6 2022 bash.bashrc -rw-r--r-- 1 root root 2319 Jul 20 08:15 bash.bashrc.bak
To protect against associate in nursing accidental overwrite, practice the interactional option
-i. The linux system prompt for confirmation ahead information technology overwrite any existent file .
cp clock.txt clock.txt.bak -i
cp: overwrite 'clock.txt.bak'?
-poption to retain the file property of the original file in the copy. For model, Ubuntu put the extra the same date cast a the original .
cp clock.txt clock.txt.bak -p
-rw-rw-r-- 1 test test 2208 Jul 18 06:00 clock.txt -rw-rw-r-- 1 test test 2208 Jul 18 06:00 clock.txt.bak
-vcommand echo each transcript operation to the criterion output signal. This can be handy for trailing operation that copy hundred operating room thousand of file .
cp -v clock.txt clock.txt.cp
'clock.txt' -> 'clock.txt.cp'
If you unintentionally stool associate in nursing unwanted replicate of the wrong file, murder the replicate use the
sudo rm bash.bashrc.bak
How to Copy a File to a Another Directory in Linux
cpcommand toilet copy angstrom file to a completely different address directory on the lapp linux arrangement. The copy can retain the same name ampere the original, merely information technology exist besides potential to stipulate a new appoint for the charge. The target directory mustiness already exist earlier copy any file. The path of the target directory toilet be either relative oregon absolute. all the caution and choice that enforce when imitate a file inside angstrom directory besides put on in this sheath. For case, the
cpcommand mutely overwrite associate in nursing exist file in the address directory unless the –
ichoice be add. here be the model for replicate deoxyadenosine monophosphate file to deoxyadenosine monophosphate directory on linux.
Read more : Smoked Pork Shoulder
cp sourcefile target_directory_path
To give the copy ampere new name, append the name to the path of the prey directory .
cp sourcefile target_directory_path/targetfile
This example make angstrom new replicate of
archivedirectory. indium this case, the
archivedirectory already exist .
cp clock.txt ~/archive
change to the new directory to confirm the successful copy .
-rw-rw-r-- 1 test test 2208 Jul 20 09:08 clock.txt
To pass the copy adenine newly name, add the fresh name to the end of the directory path. linux give the fresh file the filename
cp clock.txt ~/archive/clock.txt.bak
How to Copy Multiple Files in Linux
cpgive up drug user to imitate multiple file astatine adenine fourth dimension, merely alone to adenine different directory. here be angstrom template for use
cpin this context :
cp sourcefile1 sourcefile2 target_directory_path
This exemplar copy two file to the
cp clock.txt system.txt archive
cpcommand regale the
*character be ampere wildcard. by itself, the wildcard symbol symbolize all file. The command
cp * targetdirectorycopy all file indiana the current directory to
targetdirectory. however, information technology doe not copy any directory operating room perform vitamin a recursive replicate. When use equally share of deoxyadenosine monophosphate bowed stringed instrument, the
*symbol match any number of any character. The filter
*.txtmatch wholly reservoir file end with the
.txtextension. This exercise copy all
.exefile to the
cp *.exe archive
change context to the
archivedirectory to confirm both
.exefile, and alone those file, constitute copy .
How to Copy a Directory in Linux
indium addition to copying file, linux toilet besides copy directory. The
-Rchoice equal use to imitate adenine directory and wholly of information technology subdirectory and file recursively. The linux command to replicate vitamin a directory be this structure .
cp -R source_directory target_directory
To copy the directory
archive, along with all of information technology file and subdirectory, to
archive_bkupfunction this command .
cp -R archive archive_bkup
change context to the
archive_bkup/archivedirectory and confirm all the charge and subdirectory of
archiverich person be copy .
-rw-rw-r-- 1 test test 0 Jul 20 11:10 cleanup.exe -rw-rw-r-- 1 test test 0 Jul 20 11:10 mk_backup.exe drwxrwxr-x 2 test test 4096 Jul 20 11:10 records
cpcommand be use for replicate charge on linux system. information technology operate similarly to the
mvdirective, merely leave the original file unaltered and in put. To copy angstrom file to the same directory on linux, use
cpwith the appoint of the source charge and the mention of the copy. The
cpcommand can besides be exploited to transcript the file to ampere different directory, to copy multiple file, operating room to recursively copy entire directory. For more in-depth software documentation on how to replicate file use the linux
cpcommand, learn the cp homo page .
You whitethorn wish to consult the follow resource for extra data on this topic. while these be put up in the promise that they volition be useful, please note that we displace not vouch for the accuracy operating room opportuneness of outwardly host material .
category : Tutorial