[#]: subject: "Using cp Command in Linux" [#]: via: "https://itsfoss.com/cp-command/" [#]: author: "Abhishek Prakash https://itsfoss.com/author/abhishek/" [#]: collector: "lkxed" [#]: translator: "onionstalgia" [#]: reviewer: " " [#]: publisher: " " [#]: url: " " Using cp Command in Linux ====== The cp command is one of the essential Linux commands you probably will be using on a regular basis. As the name indicates, cp stands for copy and it is used for [copying files and directories in Linux command line][1]. It's one of the simpler commands with only a few options but that doesn't mean you cannot know more about it. Before you see some practical examples of the cp command, I advise getting familiar with the concept of absolute and relative paths because you'll need to use them while copying files from one place to another. ### Copy a file The simplest and most common use of the cp command is for copying files. For that, you just have to specify the source file and the destination where you want to 'paste' the file. ``` cp source_file destination_directory ``` ![Copy a file from the source directory to a destination][2] ### Rename the file while copying it You can also [rename the file][3] while copying it to another location. This is like those 'save as' options you see in text editors. For this, you must mention the new file name along with the path. ``` cp source_file destination_directory/new_filename ``` ![Rename the file while copying][4] ### Copy multiple files You can also copy multiple files to another location. ``` cp file1 file2 file3 destination_directory ``` ![Copy multiple files, with destination directory at the end][5] You cannot rename files in this case. Of course, you can use wildcard expansion and copy files of a certain type to another location: ``` cp *.txt destination_directory ``` ![Copy multiple files with Wildcard expansion, which is all files with .txt extension][6] ### Avoid overwriting while copying files If you are copying file1.txt to a directory where there already exists a file named file1.txt, it will be overwritten with the file you are copying. You may not always want that. This is why the cp command provides several options to deal with overwriting. The firstmost is the interactive mode with the option `-i`. In the interactive mode, it will ask you to confirm or deny the overwriting of the destination file. ``` cp -i source_file destination_directory cp: overwrite 'destination_directory/source_file'? ``` Press Y to overwrite and N to skip copying the file. ![Overwrite, but ask interactively (content overwritten)][7] ![Overwrite, but ask interactively (content is NOT overwritten)][8] The option `-n` negates overwriting completely. Destination files won't be overwritten with this option. ``` cp -n source_file destination_directory ``` ![Negated Overwriting (content of the file inside the directory has not changed)][9] There is also an option `-b` for automatically creating a backup if the destination file is going to be overwritten. B stands for backup, I presume. ``` cp -b source_file destination_directory ``` ![Overwriting a file, but with a backup file appended by '~'][10] And lastly, there is the 'update' option `-u` which will overwrite the destination file if it is older than the source file or if it destination file does not exist. ``` cp -u source_file destination_directory ``` ![The newer file is overwritten into the older file][11] ### Copy directories (folders) The cp command is also used for [copy directories in the Linux command line][12]. You need to use the recursive option `-r` for copying directories. ``` cp -r source_dir destination_dir ``` ![Copy a directory using cp command in Linux][13] You can also copy multiple directories to another location: ``` cp -r dir1 dir2 dir3 target_directory ``` ![Copying multiple directories using cp command][14] ### Preserve attributes while copying When you copy a file to another location, its [timestamp][15], [file permission][16] and even ownership gets changed. That's the normal behavior. But in some cases, you may want to preserve the original attribute even when you are copying the file. To preserve the attributes, use the option `-p`: ``` cp -p source_file destination_directory ``` ![][17] > 💡 There is also`-a`option for archive mode. It will preserve even the ACLs. ### 🏋️ Exercise time Want to practice the cp command a little? Here are some simple exercises for you. - Open a terminal and create a directory named `practice_cp` - Now, copy the /etc/services file in this newly created directory. - Make some minor changes to the copied services file in practice directory. - Now, copy /etc/services file again but in update mode. Does it change anything? Observe. - Look into /var/log directory and copy the log files that start with mail into your practice directory - Now, go back to your home directory and create a new directory named new_dir (well, I couldn't think of any better) - Copy the practice_cp directory to new_dir That should be good enough exercise for you. Enjoy learning Linux commands with It's FOSS. -------------------------------------------------------------------------------- via: https://itsfoss.com/cp-command/ 作者:[Abhishek Prakash][a] 选题:[lkxed][b] 译者:[译者ID](https://github.com/译者ID) 校对:[校对者ID](https://github.com/校对者ID) 本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 [a]: https://itsfoss.com/author/abhishek/ [b]: https://github.com/lkxed/ [1]: https://itsfoss.com/copy-files-directory-linux/ [2]: https://itsfoss.com/content/images/2023/07/cp_2.png [3]: https://learnubuntu.com:443/rename-files/ [4]: https://itsfoss.com/content/images/2023/07/cp_rename.png [5]: https://itsfoss.com/content/images/2023/07/cp_multiple_files-1.png [6]: https://itsfoss.com/content/images/2023/07/cp_multiple_files-wildcard_expansion-.png [7]: https://itsfoss.com/content/images/2023/07/cp_overwrite--i-.png [8]: https://itsfoss.com/content/images/2023/07/cp_overwrite--i-_unchanged.png [9]: https://itsfoss.com/content/images/2023/07/cp_overwrite_-n.png [10]: https://itsfoss.com/content/images/2023/07/cp_overwrite_-b-1.png [11]: https://itsfoss.com/content/images/2023/07/cp_overwrite--u--1.png [12]: https://linuxhandbook.com:443/copy-directory-linux/ [13]: https://itsfoss.com/content/images/2023/07/cp_directories_1-1.png [14]: https://itsfoss.com/content/images/2023/07/cp_directories_2-1.png [15]: https://linuxhandbook.com:443/file-timestamps/ [16]: https://linuxhandbook.com:443/linux-file-permissions/ [17]: https://itsfoss.com/content/images/2023/07/cp_preserve_attributes.png