Linux Copy File Command [ cp Command Examples ]

How do I copy files under Linux operating systems? How do I make 2nd copy of a file on a Linux bash shell? How can I copies files and directories on a Linux?

To copy files and directories use the cp command under a Linux, UNIX-like, and BSD like operating systems. cp is the command entered in a Unix and Linux shell to copy a file from one place to another, possibly on a different filesystem. The original file remains unchanged, and the new file may have the same or a different name.

cp Command Syntax

Tutorial details
Difficulty level Easy
Root privileges No
Requirements Terminal app/Shell prompt
Est. reading time 3 mintues
The syntax is as follows to copy files and directories using the cp command:

  • In the first and second syntax you copy SOURCE file to DEST file or DIRECTORY.
  • In the third syntax you copy multiple SOURCE(s) (files) to DIRECTORY.

Note: You need to type the cp command at the dollar sign ($) prompt. This prompt means that the shell is ready to accept your typed commands. Do not type the dollar ($) sign. You need to open the Terminal app to use cp command on a Linux.

Linux Copy File Examples

To make a copy of a file called file.doc in the current directory as newfile.doc, enter:
$ cp file.doc newfile.doc
$ ls -l *.doc

Sample outputs:

-rw-r--r--  1 veryv  wheel  20 Mar 20 17:42 file.doc
-rw-r--r--  1 veryv  wheel  20 Mar 20 17:43 newfile.doc

You can copy multiple files simultaneously into another directory. In this example, copy the files named main.c, demo.h and lib.c into a directory named backup:
$ cp main.c demo.h libc. backup
If backup is located in /home/project, enter:
$ cp main.c demo.h libc. /home/project backup

Copy a file to another directory

To copy a file from your current directory into another directory called /tmp/, enter:
$ cp filename /tmp
$ ls /tmp/filename
$ cd /tmp
$ ls
$ rm filename

Verbose option

To see files as they are copied pass the -v option as follows to the cp command:

$ cp -v filename.txt filename.bak
$ cp -v foo.txt /tmp

Sample outputs:

foo.txt -> /tmp/foo.txt

Preserve file attributes

To copy a file to a new file and preserve the modification date, time, and access control list associated with the source file, enter:
$ cp -p file.txt /dir1/dir2/
$ cp -p filename /path/to/new/location/myfile

This option (-p) forces cp to preserve the following attributes of each source file in the copy as allowed by permissions:

  1. Modification time/date
  2. Access time
  3. File flags
  4. File mode
  5. User ID (UID)
  6. Group ID (GID)
  7. Access Control Lists (ACLs)
  8. Extended Attributes (EAs)

Copying all files

The star wildcard represents anything i.e. all files. To copy all the files in a directory to a new directory, enter:
$ cp * /home/tom/backup

The star wildcard represents anything whose name ends with the .doc extension. So, to copy all the document files (*.doc) in a directory to a new directory, enter:
$ cp *.doc /home/tom/backup

Recursive copy

To copy a directory, including all its files and subdirectories, to another directory, enter (copy directories recursively):
$ cp -R * /home/tom/backup

Linux copy file command with interactive option

You can get prompt before overwriting file. For example, if it is desired to make a copy of a file called foo and call it bar and if a file named bar already exists, the following would prompt the user prior to replacing any files with identical names:
cp -i foo bar

Verbose output with cp command

If you pass the -v to the cp, it makes tells about what is going on. That is verbose output:
cp -v file1 file2
cp -avr dir2 /backups/


This page explained cp command that is used for copying files under Linux and Unix-like systems. For more info see man pages: ls(1).

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

🐧 30 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
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 glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep 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
30 comments… add one
  • Genami Lakau Jun 5, 2010 @ 23:33

    My Oracle runs on UNIX plat form. It could be best to know the basiz linus command.

    • Anonymous Jun 5, 2010 @ 23:34

      I would like to know the basic UNIX / LINUS commands.

  • loki2441 Sep 4, 2010 @ 8:27

    cp -v file.doc{,.bak} will also be good

  • milind dhuri Aug 22, 2011 @ 8:15


    i am having server where user are created in home partition i want to copy one file to eache users desktop wheather i.e. possible with one command of cp if somebody knows please mail me

  • adsf Mar 14, 2012 @ 22:52

    actually this is wrong. It doesn’t copy subdirectories

    To copy all the files in a directory to a new directory, enter:
    $ cp * /home/tom/backup

  • Magesh M Mar 20, 2013 @ 12:21

    Today i have copyed all the files and folder to new directory using below commands

    $ cp -R * /home/tom/backup

    $ cp * /home/tom/backup

  • sophea Oct 18, 2013 @ 4:53

    Dear all,
    I want to copy folder1 to folder2 folder3 folder4 folder5… so what the command line should i type in a single line?


    • 🐧 nixCraft Oct 18, 2013 @ 7:16
      cp -avr folder{1,2,3,4,5} /path/to/dest


      cp -avr folder* /path/to/dest


      cp -avr folder1 folder2 folderN /path/to/dest

      Read the cp syntax for more info.

      • sophea Feb 25, 2014 @ 3:38

        Dear NixCraf,
        Its not mean that. suppose i have one folder named folder1 but i need 6-more folder.
        usually i type command as bellow to do my job,
        cp folder1 folder2
        cp folder1 folder3
        cp folder2 folder3
        i don’t want to use for-loop so, have any option in cp command to do that?
        thanks for your reply

  • amol Feb 4, 2014 @ 6:39

    what is meanning of cp -b command??

    • ray Nov 4, 2014 @ 18:03

      If you need helping with the copy command (CP)….

      At the command line (CLI)… type:

      cp –help

      This will display the list of switches used on how to copy. It also shows the format on how to copy single, multiple files and directories.

  • sarmaji Apr 13, 2014 @ 16:14

    How to avoid the overwriting of the file while copying if the file is already existing ?

    • 🐧 Nix Craft Apr 14, 2014 @ 13:56

      cp -i file dest
      See -i option.

    • Enric Oct 30, 2015 @ 14:04

      cp -n files* /path

      this copies all the files who’s name differ and skips the already existing ones. It does neither ask nor raise an error if one is already there.

  • NHK Jul 16, 2014 @ 12:49

    Hi dear,
    I want to know how to create schedule copy script on red hat Linux. I mean database server to backup server.

  • Gil Oct 28, 2014 @ 15:32

    How does this command know what directory you are saving the newly copied file too? Where is that in the below example?

    $ cp filename /tmp
    $ ls /tmp/filename
    $ cd /tmp
    $ ls
    $ rm filename

  • Insider Mar 20, 2015 @ 11:34

    If i have stored the path in variable

    path= /folder/xyz/
    cp off.doc $path
    it is giving error..

    • 🐧 nixCraft Mar 20, 2015 @ 12:08

      No gap between variable and value. Also avoid using variable called path. Use something like p:

      ## assuming that you are using bash/sh/ksh shell ##
      echo "$p"
      cp -v off.doc "$p"
  • Sergei Apr 21, 2015 @ 15:29


    Can I copy file which is constantly written? Will it harm the original somehow?

  • Suresha Jan 8, 2016 @ 13:42

    Can we copy files (1.txt, 2.txt etc.,) from a all the sub directiries to one folder (see below).

    eg: Test—>test1, test2… each of test1 folder has *.txt file
    Main Directory = Test
    Sub Directorty Name within Test = test1, test2 etc.,
    Each Sub directory has a text file = 1.txt (in test1 folder) and 2.txt (in test2 folder)

  • Naresh Jan 16, 2017 @ 14:06

    Hi There,

    If i want to copy a single file into three locations, by using a single command. Would that be possible? If yes, please provide the command.


    • 🐧 Vivek Gite Jan 16, 2017 @ 15:58

      echo /location1/ /location2 /location3/ | xargs -n 1 cp myfileName

  • srishti Jan 24, 2017 @ 11:44

    i want to copy all file less then -800kb

  • Daniel Dec 15, 2017 @ 18:18

    I have two virtual linux machines. How to copy from one to another.. In other words how to refer the machinename/adress/filename of the source file

    • 🐧 Vivek Gite Dec 15, 2017 @ 18:21

      Use scp command to copy files between two machines/vms/servers. Copy a file named foo from server named bar using a user named vivek:
      laptop$ scp vivek@bar:/home/vivek/foo/ /tmp

  • Anupam Sep 21, 2020 @ 7:40

    Thanks – this was helpful

  • jenny Oct 6, 2020 @ 10:04

    Hi do you know how to copy a new file in a directory to another directory?only the new/different file

  • Ramy Jan 19, 2021 @ 10:37

    Im trying to copy a file content , /sbin into another file .
    it’s working .

  • wr19 Mar 23, 2021 @ 13:29

    how to copy file from another location like USB to home by command line on terminal? thx before

    • 🐧 Vivek Gite Mar 31, 2021 @ 23:01

      Say your USB mounted at /mnt/usb, then:
      cp /path/to/file /mnt/usb

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