You need to use the cp command to copies files and directories under Unix like operating systems. The following commands and common options should work with:
=> IBM AIX
Tutorial details | |
---|---|
Difficulty | Easy (rss) |
Root privileges | Yes/No |
Requirements | cp command |
Time | N/A |
=> 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:
- File modification time
- File access time
- File flags
- File mode
- File user ID and group ID
The syntax is:
cp -p file1 file2
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:
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.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 3 comments... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
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.
@Billy,
Yes, -p is a good option. Appreciate your comment.
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