≡ Menu

Linux: HowTo Copy a Folder [ Command Line Option ]

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

You can use various command to copy a folder under Linux operating systems.

cp Command

cp 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

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 : Explain what is being done.
  • -r : Copy directories recursively.

Example

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

HowTO: Copy Folder Linux Terminal Command

Fig.01: cp command in action

rsync Command

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:

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:
$ 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

Share this tutorial on:

Your support makes a big difference:
I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:
Become a Supporter →    Make a contribution via Paypal/Bitcoin →   

Don't Miss Any Linux and Unix Tips

Get nixCraft in your inbox. It's free:



{ 20 comments… add one }
  • Nabil January 24, 2013, 2:12 pm

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

    This comes in handy behind slowish and unstable connections.

  • bernard February 17, 2013, 12:48 pm

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

  • cashdoller March 29, 2013, 12:03 am

    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 September 7, 2016, 8:14 pm

      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 April 24, 2013, 8:46 am

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

    • Yes April 27, 2013, 9:40 am

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

      • aaron July 20, 2013, 1:24 am

        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’

        ± |master ✗| → 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 August 22, 2013, 6:42 pm

    Thanks a lot. rsync commands were useful for me

  • SURAJ K January 10, 2014, 10:53 am

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

  • nabil March 8, 2014, 2:23 pm

    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 July 25, 2014, 10:48 pm

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

    • mdodge2 January 29, 2015, 3:26 am

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

  • jonathan November 18, 2014, 3:40 pm

    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 January 13, 2015, 5:39 am

    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 March 7, 2015, 6:30 pm

    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 July 3, 2015, 12:15 am
    #
    # dircp.sh	A script to copy directory structures
    # May also be accomplished with: cp -av source-dir/ /dest-dir
    # LGD Thu Jul  2 11:33:09 PDT 2015
    #
    
    # Debug
    # set -x
    # set -v
    # exec $0 2>&1|tee $0.debug	# Puts error messages into wireless.sh.debug file
    
    PROMPT_CONTINUE="\n\t\t\tPlease press any key to continue ...\c"	# Prompt to end WAIT function
    
    WAIT(){							# Wait for keyboard input
      STTY_PARAM='stty -g'					# Save the terminal parameters
      stty -icanon eof '^a' min 1				# Configure the terminal interface to receive a single input character
      CHOICE=`dd bs=1 count=1 2>/dev/null`			# Wait for keyboard input
      echo $STTY_PARAM | stty 2>/dev/null			# Reset the terminal parameters
    }
    
    USAGE(){
    case "${1}" in
        "not_enough") ERR="Not enough arguments"		;;
        "too_many") ERR="Too many arguments"		;;
          "tree_err") ERR="Destination-Directory may not be below the Source-Directory in the same directory tree"	;;
          "arg1_d") ERR="Argument 1 is not a directory"	;;
          "arg2_d") ERR="Argument 2 is not a directory"	;;
                 *) MAIN					;;
    esac
    echo -e "\n`basename $0`: ${ERR}\nUSAGE: `basename $0`  \n"
    exit 1
    }
    
    DOIT(){
    echo -e "\nAre you sure?  You will be overwriting any files of the same name in $2. (y/N): \c"
    WAIT
    echo							# Provide newline for PROMPT_CONTINUE
    case $CHOICE in
    #    [yY]*) rsync -nav $1 $2; exit $? 			;; # Remove the -n argument to rsync to actually copy the directory tree
        [yY]*) rsync -va $1 $2; exit $? 			;; # Copy the directory tree
            *) echo -e "`basename $0`: Aborting"; exit 1	;;
    esac
    }
    
    MAIN(){
    # Usage Trap
    [ ${#} -lt 2 ] && USAGE not_enough			# Trap for wrong number of arguments
    [ ${#} -gt 2 ] && USAGE too_many			# Trap for wrong number of arguments
    #[ ${1} = '.' ] && USAGE tree_err			# Trap for non-directory argument 1
    cd "$1"; TREE_ERR=`find . -type d -path "$2"`; cd -	# Trap for destination-directory below source-directory (BUG: same dirname!)
    [ "${TREE_ERR}" = "${2}" ] && USAGE tree_err		#
    [ -d ${1} ] || USAGE arg1_d				# Trap for non-directory argument 1
    [ -d ${2} ] || USAGE arg2_d				# Trap for non-directory argument 2
    # Copy the directory
    DOIT "$1" "$2"
    }
    
    MAIN "$1" "$2"
    
  • A S February 6, 2016, 8:20 pm

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

  • SOK Khemrin March 1, 2016, 7:19 am

    Good Job!

  • I July 20, 2016, 9:44 pm

    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 September 7, 2016, 8:21 pm

    -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.

Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">


   Tagged with: , , , , , , , , , , , , , , , ,