Linux Copy Command Examples

Posted on in Categories last updated December 15, 2009

How do I copy files under Linux operating systems?

You need to use the cp command to make copies of existing file. The syntax is as follows:

 cp source.file destination.file

OR

 cp /path/to/file1 /path/to/file2

For example, make a copy of /etc/passwd as /tmp/password.file, enter:

 cp /etc/passwd /tmp/password.file
cat /tmp/password.file

To copy file to another system, use scp command:

scp /path/to/file1 user@server1:/tmp

For example, copy /home/vivek/data.txt to server called server1 and into a directory called /home/sales, enter:

scp /home/vivek/data.txt roja@server1:/home/sales

You must know roja users’ password in order to copy a file.

Copy All Files

To copy all files from /data/sales/*.txt to /backup directory, enter:

cp -avr /data/sales/*.txt /backup

Where,

  • -a : Archive mode and save file permission
  • -v : Verbose mode (show progress)
  • -r : Copy directories recursively

scp vs cp command

  • The scp copies files between hosts on a network.
  • The cp copies files between two directories on same host.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

Leave a Comment