Unix Copy Command Examples [ cp command ]

How do I copy files under Unix operating system using ksh or csh or bash shell prompt?

You need to use the cp command to copies files and directories under Unix like operating systems. This page explains Unix copy command with examples. The following commands and common options should work with:
Tutorial details
Difficulty level Easy
Root privileges No
Requirements cp command
Est. reading time 4 mintues
  • IBM AIX
  • Apple macOS (OS X)
  • Sun/Oracle Solaris
  • FreeBSD/OpenBSD/NetBSD
  • Linux
  • HP-UX
  • And other Unix like oses.

Unix cp command syntax

The syntax is:

cp SOURCE DEST
cp SOURCE DIRECTORY
cp file1 file2
cp file1 new-file2
cp [options] file1 new-file2

By default cp will only copies files.

Unix cp command examples

In this example, copy a file called data.txt in the current directory into another directory, called /tmp. Open a terminal and type:

cp data.txt /tmp

Verify that file has been copied in /tmp, enter:

ls -l /tmp/data.txt
ls -l data.txt

Please note that the file(s) copied into the directory /tmp will all have the same names as the originals. You can copy multiple files into another directory as follows:

cp data.txt foo.tbz image.png /tmp
ls -l /tmp

How do I copy recursively?

You need to pass the -r or -R option (i.e., recursive option). It allows directories including all of their contents to be copied:

cp -r dir1 dir2

To see copy progress pass -v option to cp command:

cp -v file1 file2

Sample outputs:

`file1' -> `file2'

The -v cause cp to be verbose, showing files as they are copied. You can pass the -v option along with other options:

cp -v -r file1 /tmp
cp -v -r dir1 dir2

How do I copy all *.c files?

Copy copy all c source code file to a directory called /projects/backup/foo, from /home/vivek/devel/foo, enter:

cp /home/vivek/devel/foo/*.c /projects/backup/foo/

OR

cp -v /home/vivek/devel/foo/*.c /projects/backup/foo/

All the files in a directory can be copied to another directory by using the shell wildcard such as start willdcard or question wild card:

## copy all perl (*.pl) files to /tmp ##
cp *.pl /tmp
 
## copy all perl (*.pl) files starting with character a to /tmp ##
cp a*.pl /tmp
 
## copy all perl (*.pl) files starting with a,b, and c to /tmp ##
cp [abc]*.pl /tmp
 
## copy all perl (*.pl) files starting with a single file name such as  a.pl, b.pl, .., z.pl to /tmp ##
cp ?.pl /tmp
 
## copy all .html files to /var/www/html ##
## The star wildcard represents anything whose name ends with the .html extension ##
cp -v /home/vivek/website/*.html /var/www/html

How do I confirm file overwriting?

You need to pass the -i option to cp. It will prompt the user if file already existing in a destination directory so that file would be overwritten with confirmation:

cp -i /etc/resolv.conf /tmp

Sample outputs:

cp: overwrite `/tmp/resolv.conf'? y

You need to enter the letter y (both lower case or upper case with work) in response to the prompt causes the command to continue. Any other answer prevents the command from overwriting the file called /tmp/resolv.conf. Some user put the following alias in ksh startup file called $HOME/.kshrc:
$ vi ~/.kshrc
Append the following alias:

## prvent overwriting by default for cp command
alias cp='cp -i'

Save and close the file. Source the shell startup file to have the changes take immediate effect, enter:

. .kshrc

Tip: Preserve the file permission and other attributes

You need to pass the -p option to save the following file attributes of each source file as allowed by permissions:

  1. File modification time
  2. Access time
  3. File flags
  4. Mode
  5. File user ID and group ID

The syntax is as follows:

cp -p file1 file2
cp -p /path/to/foo ~/bar/
# verify it #
ls -l /path/to/foo ~/bar/

Example: Copying a file without -p option

Type the following commands:

ls -l /etc/resolv.conf
cp /etc/resolv.conf $HOME
ls -l $HOME

Sample outputs:

Fig.01: Unix cp command without -p option

Example: Copying a file with -p option to save file attributes

Type the following commands:

ls -l /etc/resolv.conf
## Note: running as root to save attributes ##
sudo cp -p -v /etc/resolv.conf $HOME
ls -l $HOME

Sample outputs:

Fig.02: Preserve in the copy as many of the modification time, access time, file flags, file mode, user ID, and group ID as allowed by permissions.

Summing up

The cp command is used to copy files and directories on Linux, Unix, *BSD, and macOS command-line. Some options of the cp command may not be available on your Unix variant. Hence, I strongly suggest that you read the local man page by typing the man command:
man cp
cp --help


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 4 comments so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
4 comments… add one
  • Billy Larlad Jun 5, 2012 @ 13:45

    Important to note that the -p option (as in cp -p) will preserve a file’s timestamp and the other metadata. From the OpenBSD cp man page:

    “Preserve in the copy as many of the modification time, access time, file flags, file mode, user ID, and group ID as allowed by permissions.”

    If you’re using cp for backups, this is good to know.

  • 🐧 nixCraft Jun 6, 2012 @ 12:15

    @Billy,

    Yes, -p is a good option. Appreciate your comment.

  • JL Apr 11, 2016 @ 15:34

    It seems that in recent versions (5.8+, at least) of OpenBSD the verbose option isn’t available, as seen here: http://man.openbsd.org/OpenBSD-current/man1/cp.1

  • Ursula Clark May 26, 2021 @ 23:32

    Hey,

    I brought a new Apple M1 MacBook Air and learning PHP, Bash, MySQL, HTML/CSS, git, and JavaScript programming. These commands worked on my macOS too.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum