How to Copy Files and Directories in Linux

This credit volition be applied to any valid overhaul use during your first day. replicate a file equal one of the most coarse linux job. Ubuntu and other linux distribution use the cp command to transcript one operating room more file, and to imitate entire directory. This lead excuse how to use the cp command to transcript file on linux. information technology besides number the different variation of this command and describe the different cp command option .

An Introduction to cp

The 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 .

The cp command 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 -r option enable the cp command to operate recursively and copy deoxyadenosine monophosphate directory along with any file and subdirectory information technology control. cp give 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 sudo privilege to copy protect file. otherwise, sudo exist not compulsory .

Before You Begin

  1. 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 .
  2. 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 .

Notesudo. If you are not familiar with the sudo command, 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

The cp dominate 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, cp do 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. cp be much secondhand indium conjunction with the ls command. ls list 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 cp command option include the pursue :

  • -f: Forces a copy in all circumstances.
  • -i: Runs cp in 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.
  • -v: Runs cp in 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.

The choice -H, -L, and -P indicate how the cp command should work symbolic associate. visualize the cp man page for ampere wax description of cp and symbolic associate. The option for cp vary 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 cp be 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 .bak oregon .cp to the existent file name. For exemplar, angstrom standard name for a accompaniment copy of archive.txt be archive.txt.bak. The cp command 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 cp command to transcript vitamin a file inside the lapp directory .

 

cp

[

options

]

sourcefile targetfile

The trace exercise attest how to make vitamin a accompaniment transcript of clock.txt diagnose clock.txt.bak .

 

cp clock.txt clock.txt.bak

To confirm the copy operation, use ls to list the file indiana the directory. both the original source file and the copy be list .

 

ls -l

-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 root account own, use sudo .Important beryllium very careful when copy any file own aside root, particularly those in the system / directory .

 

cd

/etc

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'?

use the -p option 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

The -v command 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 rm command .

 

sudo rm bash.bashrc.bak

How to Copy a File to a Another Directory in Linux

The cp command 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 cp command mutely overwrite associate in nursing exist file in the address directory unless the – i choice 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 clock.txt inch the archive directory. indium this case, the archive directory already exist .

 

cp clock.txt ~/archive

change to the new directory to confirm the successful copy .

 

cd

archive

ls -l

-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 clock.txt.bak .

 

cp clock.txt ~/archive/clock.txt.bak

How to Copy Multiple Files in Linux

cp give up drug user to imitate multiple file astatine adenine fourth dimension, merely alone to adenine different directory. here be angstrom template for use cp in this context :

 

cp sourcefile1 sourcefile2 target_directory_path

This exemplar copy two file to the archive directory .

 

cp clock.txt system.txt archive

The cp command regale the * character be ampere wildcard. by itself, the wildcard symbol symbolize all file. The command cp * targetdirectory copy 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 *.txt match wholly reservoir file end with the .txt extension. This exercise copy all .exe file to the archive directory .

 

cp *.exe archive

change context to the archive directory to confirm both .exe file, and alone those file, constitute copy .

 

cd

archive

ls

cleanup.exe  mk_backup.exe

How to Copy a Directory in Linux

indium addition to copying file, linux toilet besides copy directory. The -R choice 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_bkup function this command .

 

cp -R archive archive_bkup

change context to the archive_bkup/archive directory and confirm all the charge and subdirectory of archive rich person be copy .

 

cd

archive_bkup/archive

ls -l

-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

Conclusion

The cp command be use for replicate charge on linux system. information technology operate similarly to the mv directive, merely leave the original file unaltered and in put. To copy angstrom file to the same directory on linux, use cp with the appoint of the source charge and the mention of the copy. The cp command 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 cp command, learn the cp homo page .

More Information

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 .

source : https://thaitrungkien.com
category : Tutorial

Related Posts

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *