Linux – How To Copy a Folder [ Command Line Option ]

I am a new Linux system user. How do I copy a directory or folder under Linux operating system using command line options and bash shell?

Introduction: A file is a collection of data items stored on disk. Alternatively, it’s device which can store the information, data, music (mp3/mp4 files), picture, movie, sound, book and more. A directory is a group of files. A directory divided into two types such as root and subdirectory. You can use the various command to copy a folder under Linux operating systems. This page shows how to copy the contents of a folder to another folder using Linux terminal.

ADVERTISEMENTS

How To Copy a Folder with cp Command

The cp command is a Linux command for copying files and directories. The syntax is as follows:

cp source destination
cp dir1 dir2
cp -option  source destination
cp -option1 -option2  source destination

Linux cp command examples

In this example copy /home/vivek/letters/ folder and all its files to /usb/backup/ directory:
cp -avr /home/vivek/letters /usb/backup
Where,

  • -a : Preserve the specified attributes such as directory an file mode, ownership, timestamps, if possible additional attributes: context, links, xattr, all.
  • -v : Verbose output.
  • -r : Copy directories recursively.

More examples of cp command to copy folders on Linux

Copy a folder called /tmp/conf/ to /tmp/backup/:
$ cp -avr /tmp/conf/ /tmp/backup/
Sample outputs:

Linux: How To Copy a Folder [ Command Line Option ]

Fig.01: cp command in action

All the files and subdirs/folders in a directory can be copied to another folder by using the star wildcard. For example, the following would copy all of the files in a folder named /home/vivek/Documents/ into another existing folder called /data/:
cp -v /home/vivek/Documents/* /data/

Use Linux rsync Command to copy a folder

You can also use rsync command which is a fast and extraordinarily versatile file copying tool. It can make copies across the network. The syntax is as follows for the rsync command

rsync -av /path/to/source/ /path/to/destination/
rsync -av /path/to/source/ /path/to/destination/source/

To backup my home directory, which consists of large files and mail folders to /media/backup, enter:
$ rsync -avz /home/vivek /media/backup
I can copy a folder to remote machine called server1.cyberciti.biz as follows:
$ rsync -avz /home/vivek/ server1.cyberciti.biz:/home/backups/vivek/
Where,

  • -a : Archive mode i.e. copy a folder with all its permission and other information including recursive copy.
  • -v : Verbose mode.
  • -z : With this option, rsync compresses the file data as it is sent to the destination machine, which reduces the amount of data being transmitted something that is useful over a slow connection.

You can show progress during transfer using –progress or -P option:
$ rsync -av --progress /path/to/source/ /path/to/dest
Sample outputs:

Copy Folder Linux Commands [ rsync ]

Fig.02: rsync command in action

Conclusion

You just learned how to copy a folder on a Linux like operating system using the cp command and rsync command. In conclusion, use rsync for a network folder transfer and cp for a local disk transfer.

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig 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

ADVERTISEMENTS
23 comments… add one
  • Nabil Jan 24, 2013 @ 14:12

    You can tell rsync to resume an unfinished copy by appending the –partial option.

    This comes in handy behind slowish and unstable connections.

  • bernard Feb 17, 2013 @ 12:48

    i am a beginner in computer repairs,i like u to update me also on linux usage nd repairs.

  • cashdoller Mar 29, 2013 @ 0:03

    Is there any benefit to using rsync without going over a remote network. Like from sdb1 to sdc1? If not, what if sdb1 was NTSC and sdc1 was ext4?

    • situationalawareness Sep 7, 2016 @ 20:14

      I know it’s like 3 years later but I figured I’d answer anyway.
      Since rsync is layers above the filesystem, it doesn’t matter. The nice part is the ability to ctrl-c out and restart with partial transfers. It’s a nice simple way to synchronize without needing to overthink things.

  • Someone Apr 24, 2013 @ 8:46

    “r” in “cp -ar” is redundant. -a implies -r.

    • Yes Apr 27, 2013 @ 9:40

      I confirm that cp does the same with or without r.

      • aaron Jul 20, 2013 @ 1:24

        Are you sure do not have an alias in place for cp? cp -r (recursive) allows for copying of folders recursively. Otherwise you will get an error trying to copy a folder without -r as an argument, UNLESS -r is set via an alias.

        You can confirm like this is a bash shell

        type without quotes ‘alias’

        alias dir='dir --color'
        alias l.='ls -d .*'
        alias ll='ls -l'
        alias ls='ls --color'
        alias reload='source ~/.bash_profile'
        alias vi='vim'
        alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
  • BP G Aug 22, 2013 @ 18:42

    Thanks a lot. rsync commands were useful for me

  • SURAJ K Jan 10, 2014 @ 10:53

    i would like to know how to copy files from a cluster network to my pc home directory

  • nabil Mar 8, 2014 @ 14:23

    How can we copy programs between two users or directories??
    We are stuck in our lab.
    Can someone please give the stps for copying …??

  • Bruce Jul 25, 2014 @ 22:48

    Unfortunately, “rsync -ar source destination” moved all my files/folders instead of copying. Why?

    • mdodge2 Jan 29, 2015 @ 3:26

      It’s -av for copying, rather than -ar. Simple typo…

  • jonathan Nov 18, 2014 @ 15:40

    you’re not explaining when to leave a trailing slash on the path – i can see you’re doing it, but not mentioning why.

  • Andy Jan 13, 2015 @ 5:39

    I have to take backup of my projects in 15 days interval, this command works for me, now i am going to set crontab. {crontab also learned from you thanks alot}
    Thanks

  • dhimes Mar 7, 2015 @ 18:30

    Note that if you are copying a directory with a different group-id, such as

    dhimes:www-data

    then you’ll want to use sudo:

    sudo cp -ar [source] [destination]

  • Larry Dighera Jul 3, 2015 @ 0:15

    11188870355f9bfc4682349_000002

  • A S Feb 6, 2016 @ 20:20

    I think the trailing “/” is for directories and no “/” is for files.

  • SOK Khemrin Mar 1, 2016 @ 7:19

    Good Job!

  • I Jul 20, 2016 @ 21:44

    Don’t believe this article. This won’t work:
    cp dir1 dir2
    For some reason to copy folders you need to do this like this:
    cp -r dir1 dir2

  • situationalawareness Sep 7, 2016 @ 20:21

    -r is a flag for recursive copying, meaning directories/files from subdirectories as well. cp will copy from the current directory without -r. Honest, it’s not a conspiracy.

  • diego Dec 3, 2017 @ 12:49

    hi,
    i want to use Rsync and cron job but i have a source folder that change the name every week.
    example :
    rsync /backup/full.20171104.0009.0069 /backup2/

    the folder full.20171104.0009.0069 change it’s name every week.

    what is the proper solution.

    thanks.

  • Chris Aug 5, 2020 @ 10:50

    Don’t use the * wildcard. Don’t do
    cp src/* dest

    You lose hidden files, and you could exceed the glob limit.

    Instead do on Linux:
    cp -T src dest
    or on BSD:
    cp src/ dest

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.