Linux: HowTo Copy a Folder [ Command Line Option ]

by on April 10, 2012 · 9 comments· LAST UPDATED April 10, 2012

in

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

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 9 comments… read them below or add one }

1 Nabil January 24, 2013 at 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.

Reply

2 bernard February 17, 2013 at 12:48 pm

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

Reply

3 cashdoller March 29, 2013 at 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?

Reply

4 Someone April 24, 2013 at 8:46 am

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

Reply

5 Yes April 27, 2013 at 9:40 am

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

Reply

6 aaron July 20, 2013 at 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’

Reply

7 BP G August 22, 2013 at 6:42 pm

Thanks a lot. rsync commands were useful for me

Reply

8 SURAJ K January 10, 2014 at 10:53 am

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

Reply

9 nabil March 8, 2014 at 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 …??

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: