Copying Files in Bash

You will learn how to copy files via command line in this lesson.

cp

Definition:

This command is one of the most frequently used commands. It is used to copy files and directories. By default, cp copies files but not directories. Always be careful while choosing name for new files because the content might be lost if you use a file name that already exists. If you’re placing the copied version in the same directory then it must have a different name than the original name.

Syntax:

cp [option] [file_name] new_file_name

Options:

Option Meaning
-r or -R Means Recursive. It is used to copy directories including all its content
-i This option is only used to warn the user about overwrite issues
-b Used to make backup copies
-f Means Force. This option is used to force open the destination files
-u Means Update. As the name suggests, it only updates the file if any changes are made in the file
-x This option is to indicate cp to stay on the same file system
-s By using this option you can only make references rather than deep copying everything

Example:

  1. Copy three files in a different directory simultaneously with the same names

    cp file1 file2 file3 my_directory

Get hands-on with 1200+ tech skills courses.