Live Chat Software by Kayako
Knowledgebase: Ubuntu
inux: HowTo Copy a Folder [ Command Line Option ]
Posted by Thang Le Toan on 04 September 2016 07:25 AM

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

(0 vote(s))
Helpful
Not helpful

Comments (2)
thang
04 September 2016 07:36 AM
What does “cp: omitting directory” mean?



I've issued the following command:

sudo cp ~/Transfers/ZendFramework-1.11.4-minimal/library/Zend/* ~/public_html/cmsk.dev/library/

When I do this, I start getting the following messages:

cp: omitting directory `Tag'
cp: omitting directory `Test'
cp: omitting directory `Text'
cp: omitting directory `TimeSync'
cp: omitting directory `Tool'
cp: omitting directory `Translate'
cp: omitting directory `Uri'
cp: omitting directory `Validate'

and so on...

Why do I get these messages ?
Ha Lan
04 September 2016 07:37 AM


By default, cp copies only the direct files in, and not subdirectories in the directory. The message cp: omitting directory 'directory' warns you that the mentioned directory is not copied.

To do so, specify the -r (or --recursive) option:

sudo cp -r ~/Transfers/ZendFramework-1.11.4-minimal/library/Zend/* ~/public_html/cmsk.dev/library/

The manual page (command: man cp) contains an overview of the available options.
Post a new comment
 
 
Full Name:
Email:
Comments:
CAPTCHA Verification 
 
Please enter the text you see in the image into the textbox below (we use this to prevent automated submissions).

Help Desk Software by Kayako