

Replace /new_directory/ with the destination.



–R recursive: copies all files and subfolders in a directory.–i interactive: prompts for confirmation, highly advised.–f force: force the copy by deleting an existing file first.–p preserve: keeps the same attributes, like creation date and file permissions.–v verbose: shows the progress of multiple copied files.Additional OptionsĪdditional options can be used in combination with the cp command: You may not get a warning before Linux overwrites your file – be careful, or see below for the –ioption. Some users will add _old, some will add a number, and some will even change the three-letter extension (e.g. You’ll need to change the name of the target file to copy in the same location. However, the same file cannot exist twice in the same directory. This Linux command creates a copy of the my_file.txt file and renames the new file to my_file2.txt.īy default, the cpcommand runs in the same directory you are working in. The basic format of the command is: cp source_file target_file Virtually all Linux distributions can use cp. The cp command is the primary method for copying files and directories in Linux. Using the cp Command to Copy Files and Directories in Linux If your version of Linux boots to a desktop graphical interface, launch a terminal window by pressing CTRL-ALT-F2 or CTRL-ALT-T. It's important to read the man pages to know any idiosyncrasies that may arise (this is a good use case to show why POSIX standards are useful).Note: These Linux commands can only be run from a terminal window. POSIX compliance, you would want to use -R because of some implementation-dependent differences with -r. There is an important distinction between Linux and Unix in the answer because for Linux (GNU and BusyBox) -R, -r, and -recursive are all equivalent, as mentioned in this answer.
#LINUX COPY DIRECTORY AND CONTENTS HOW TO#
When you don't know how to use a Linux command, there is a manual page that you can refer to by typing: man Īlso, to auto complete long file paths when typing in the terminal, you can hit Tab after you've started typing the path and you will either be presented with choices, or it will insert the remaining part of the path. If you need to know what directory you are in, use the command pwd. ~ is a special character in the terminal that automatically evaluates to the current user's home directory. Linux is case-sensitive and also needs the / after each directory to know that it isn't a file. If the directory you're copying is called dir1 and you want to copy it to your /home/Pictures folder: cp -r dir1/ ~/Pictures/ You need to change directories so that you are outside of the directory you are trying to copy.
