Copy Files and Folders

cp is a program for copying files and folders. (The original file/folder will unaffected).

Syntax

cp -r [source] [target]
  • -r -- (Optional) "Recursive" flag for coping files within a sub-directory.
  • [source] -- The filepath of the file or folder you wish to copy.
  • [target] -- The name and location of the new file or folder you want to create.

Examples

New filename, current directory

cp index.html homepage.html

To another directory

cp config.conf /var/apache2/sites-available/000-default.conf

To a new directory using its original filename

cp /root/latest.zip /var/www/html

Include subfolders

cp /root/* /var/www/html

Remember: The asterisk (*) is a wildcard that indicates all files and folders will be targeted. But files within folders will not be copies. Only empty folders will be created.

Copy a folder and all it files and subfolders

cp -r /root/* /var/www/html
  • -r -- "Recursive" flag for coping files within a sub-directory.