In the copy command, the files will remain in the source folder along with moving to the destination folder. Rsync –a my_file.txt /new_directory/my_file_backup.txt ConclusionĬopy command in Linux is another commonly used command that will help in creating a copy of the existing file to another specific location. If you want to copy a single file, then you can run the following command. It is somehow similar to the cp Linux command but shows some differences. You can use the rsync command for synchronizing or transferring the data between two different locations. Where, “-R” stands for the recursive operation. If you want to copy the entire folder along with its sub-directory then you can use the “-R” option along with the cp command. jpg extension and then copy them to the destination path. The above command will search for all the files with the. You can even use the wildcard character for copying the files as shown below. The above command will not move the files but create a copy of these files to the destination path. You can mention multiple files separated by spaces followed by the path where you want to copy those files.Ĭp my_file_name.txt my_file2_name.txt my_file3_name.txt /new_directory Using the cp file command, you will also be able to copy multiple files at the same time. How to Copy Multiple Files from One Directory to Another in Linux This option is also useful if you want to create backups of configuration files, or for copying data to a storage device. If we want to rename and copy the file to a different path, you can run the following command as shown below.Ĭp my_file_name.txt /new_directory/my_file_name2.txt If the new_directory does not exist then the copy file command will create this directory. The above command will allow you to copy the file without changing the directory. You can even copy the file from a directory other than the current working directory by specifying the path of the file in the source and specifying the path to the target as shown below. If you want to copy a specific file from the current working directory to a different location other than the working directory, you can use the following copy command to do so. How to copy file in Linux to a different directory –u update: This option will allow to update only if the source is newer than the destination.–R recursive: This option will copy all files and subfolders within a directory.–i interactive: This option will provide a prompt for confirmation before moving the file.–f force: It will help in forcing the copy file by deleting the existing file first.–p preserve: It helps in keeping the same attributes of the copied file such as creation date and file permissions.–v verbose: This option will help in showing the progress of the running command that is copying the files.Copy Command Optionsīelow are some additional copy command options that you can use with the cp command to perform various operations. If you want to copy a file that already exists in the target location, you need to change the name of the file to differentiate the new file. however, two files with the same name cannot exist in the same directory. The above Linux command will help in creating a copy of the my_file_name and then renaming the new file to the my_file_name.īy default, the copy command in Linux will get executed in the same directory you are in. Below is the basic format of the cp command that you can use.Ĭp source_file target_file Well, every Linux distribution uses the cp command. It works showhome similar to the mv command that uses to move the file or directory from one location to another. You can use the cp command in Linux to copy files and directories from one location to another. Apart from this, you will get to know how to create system-wide backups for copying specific files. In this article, we will be learning various cp command in Linux for copying files along with various options. One of the commonly performed tasks is to copy files in Linux by running specific commands along with various options for additional functionality. Linux offers flexible and simple commands for performing various operations on the files and folders. List of content you will read in this article:
0 Comments
Leave a Reply. |