However, sometimes we want to overwrite the file destination in a safer way. In the previous example, we saw how to avoid the overwriting of the destination file. How to Overwrite the File with Confirmation In this example, we have used the -v option to illustrate that the file-2.txt file hasn’t been overwritten. To understand this, let’s try to overwrite the existing file: $ cp -n -v file-1.txt file-2.txt ![]() However, we can disable this default behavior using the -n option. How to Avoid Overwriting the Fileīy default, the cp command replaces the destination file, which means it will overwrite the file if it exists at the destination with the same name. Also, it is important to note that, to use this alternative syntax the directory must be present already and it must be the last argument of the command. The above output shows that all files have been copied to the dir-1 directory. Now, let’s copy all three files into the dir-1 directory using the single command: $ cp -v file-1.txt file-2.txt file-3.txt dir-1 So, first, create a new directory with the name dir-1: $ mkdir dir-1 To perform such an operation effectively, we can use an alternative syntax of the cp command. Obviously, we can execute the cp command multiple times to achieve it, but that won’t be the most effective way. One of the common use cases in such environments is to copy multiple files into a single directory. However, in the real production environment, we have to work with a large number of files. So far we worked with a single file and the current working directory only. How to Copy Multiple Files to the Directory The left side argument is the source file whereas the right side argument is the destination file. ![]() In the above output, the arrow represents the file that is being copied. Let’s make a copy of the file-1.txt file using the following command: $ cp -v file-1.txt file-3.txt In such cases, we can enable the verbose mode using the -v option, which provides diagnostics for every processed file. However, it doesn’t make sense to use one more command just to verify the result of the previous commands. ![]() In the previous example, we used the ls command to verify whether or not the file copy operation succeeded. Next, create a copy of the file using the following command: $ cp file-1.txt file-2.txtĬopy File in Linux 2. To understand the usage, let’s create a simple file: $ touch file-1.txt Most of the time users perform this operation to take a backup of the important configuration.įor example, we often create a backup copy of /etc/ssh/sshd_config file before updating the SSH configuration. One of the basic use of the cp command is to copy a file into a current directory. In the above syntax, the square brackets () represent the optional arguments whereas angular brackets () represent the mandatory arguments. At a high level, it is divided into two parts – options and arguments: $ cp The syntax of the cp command is identical to other Linux commands.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |