SnapOverflow

SnapOverflow Logo SnapOverflow Logo

SnapOverflow Navigation

  • Home
  • Server
  • Ubuntu

Mobile menu

Close
  • Home
  • System Administrators
    • Hot Questions
    • New Questions
    • Tags
  • Ubuntu
    • Hot Questions
    • New Questions
    • Tags
  • Help
Home / ubuntu / Questions / 86822
In Process
pandisvezia
pandisvezia
Asked: 2011-12-11 17:19:37 +0800 CST2011-12-11 17:19:37 +0800 CST 2011-12-11 17:19:37 +0800 CST

How can I copy the contents of a folder to another folder in a different directory using terminal?

  • 772

I am trying to copy the contents of a folder to another folder in a different directory using terminal.

Would somebody be able to provide me an example of the command line syntax required to achieve this?

command-line
  • 8 8 Answers
  • 3472336 Views

8 Answers

  • Voted
  1. enzotib
    2011-12-12T04:01:47+08:002011-12-12T04:01:47+08:00

    You can copy the content of a folder /source to another existing folder /dest with the command

    cp -a /source/. /dest/
    

    The -a option is an improved recursive option, that preserve all file attributes, and also preserve symlinks.

    The . at end of the source path is a specific cp syntax that allow to copy all files and folders, included hidden ones.

    • 1791
  2. Panther
    2011-12-11T19:59:05+08:002011-12-11T19:59:05+08:00

    An alternate is rsync:

    rsync -a source/ destination
    

    The advantages of rsync are:

    1. After the initial sync, it will then copy only the files that have changed.
    2. You can use it over a network, convenient for files in $HOME, especially config files.
    • 188
  3. Bruno Pereira
    2011-12-11T17:23:39+08:002011-12-11T17:23:39+08:00

    Lets say you have a folder called folder1 in your ~, inside folder1 is 1 file called file1 and 2 folders called sub1 and sub2 each with other files and folders inside them.

    To copy all the contents of ~/folder1 to ~/new_folder1 you would use

    cp -r ~/folder1/. ~/new_folder1
    

    new_folder1 would then contain all the files and folders from folder1.

    cp is the command to copy using a terminal, -r makes it recursively (so, current directory + further directories inside current) ~/folder1 is the origin folder, ~/new_folder1 is the destination folder for the files/folders inside the origin.

    • 113
  4. Karl Morrison
    2019-06-18T00:52:39+08:002019-06-18T00:52:39+08:00

    Simple example.

    Copy the directory dir_1 and its contents (files) into directory dir_2:

    cp -r ./dir_1 ./dir_2
    # or
    cp -r ./dir_1/ ./dir_2/
    # Results in: ./dir_2/dir_1/_files_
    

    Copy only the contents (files) of dir_1 into directory dir_2:

    cp -r ./dir_1/. ./dir_2
    # or
    cp -r ./dir_1/. ./dir_2/
    # Results in: ./dir_2/_files_
    

    _files_ is a placeholder for the actual files located in the directory.

    • 39
  5. Dilip Rajkumar
    2014-11-25T21:53:04+08:002014-11-25T21:53:04+08:00

    Check this http://www.cyberciti.biz/faq/copy-folder-linux-command-line/ for more information on copying folder. Hope this helps.

    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
    
    • 22
  6. zarpio
    2020-04-15T23:03:03+08:002020-04-15T23:03:03+08:00

    I like this command

    rsync -av --progress ~/code/project-source/. ~/code/project-destination --exclude .git --exclude node_modules
    

    Some of the commonly used options in rsync command are listed below:

    • -v, –verbose: Verbose output
    • -q, –quiet: suppress message output
    • -a, –archive: archive files and directory while synchronizing ( -an equal to following options -rlptgoD)
    • -r, –recursive: sync files and directories recursively
    • -b, –backup: take the backup during synchronization
    • -u, –update: don’t copy the files from source to destination if destination files are newer
    • -l, –links: copy symlinks as symlinks during the sync
    • -n, –dry-run: perform a trial run without synchronization
    • -e, –rsh=COMMAND: mention the remote shell to use in rsync
    • -z, –compress: compress file data during the transfer
    • -h, –human-readable: display the output numbers in a human-readable format
    • –progress: show the sync progress during transfer
    • 4
  7. vimal krishna
    2015-09-10T02:39:20+08:002015-09-10T02:39:20+08:00

    If there are two folders: (with write permission)

    drwxr-xr-x 4 vimal vimal  4096 Sep  9 12:17 .
    drwxr-xr-x 3 root  root   4096 Aug 18 14:35 ..
    drwxrwxrwx 6 vimal vimal  4096 Sep  9 12:15 DATA
    drwxrwxrwx 7 vimal vimal  4096 Sep  9 12:15 PORTAL
    

    If you are inside the folder called PORTAL where you want to copy all content of another folder say DATA at the same level then you will do

    vimal@vimal-D3H:/var/www/html/PORTAL$ cp -a ../DATA/. .

    You have to notice 2 dots. Last dot says copy here in present folder

    and

    one following /DATA/. says that all the CONTENTS inside DATA folder to be copied, and not the DATA folder itself.

    If you remove this trailing "." from /DATA/

    then whole DATA folder will be copied inside PORTAL(from where you are coping).

    • 3
  8. Kai - Kazuya Ito
    2021-07-18T06:26:14+08:002021-07-18T06:26:14+08:00

    This code with Flag "-R" copies perfectly all the contents of "folder1" to existing "folder2":

    cp -R folder1/. folder2
    

    Flag "-R" copies symbolic links as well but Flag "-r" skips symbolic links so Flag "-R" is better than Flag "-r".

    • The latest GNU Grep 3.7:
    -R, --dereference-recursive
    
    For each directory operand, read and process all files in that directory, 
    recursively, following all symbolic links.
    
    -r, --recursive
    
    For each directory operand, read and process all files in that directory, 
    recursively. Follow symbolic links on the command line, but skip symlinks 
    that are encountered recursively. Note that if no file operand is given, 
    grep searches the working directory. This is the same as the 
    ‘--directories=recurse’ option.
    
    • 3

Sidebar

Stats

  • Questions 681965
  • Answers 980273
  • Best Answers 280204
  • Users 287326
  • Popular
  • Answers
  • Marko Smith

    How to unzip a zip file from the Terminal?

    • 9 Answers
  • Marko Smith

    How can I copy the contents of a folder to another folder in a different directory using terminal?

    • 8 Answers
  • Marko Smith

    How do I install a .deb file via the command line?

    • 11 Answers
  • Marko Smith

    How do I run .sh scripts?

    • 16 Answers
  • Marko Smith

    How do I install a .tar.gz (or .tar.bz2) file?

    • 14 Answers
  • Marko Smith

    What command do I need to unzip/extract a .tar.gz file?

    • 8 Answers
  • Marko Smith

    How to list all installed packages

    • 24 Answers
  • Marko Smith

    Unable to lock the administration directory (/var/lib/dpkg/) is another process using it?

    • 25 Answers
  • Marko Smith

    How can I add a user as a new sudoer using the command line?

    • 7 Answers
  • Marko Smith

    Change folder permissions and ownership

    • 9 Answers
  • Martin Hope
    ubuntu-nerd How to unzip a zip file from the Terminal? 2011-12-11 20:37:54 +0800 CST
  • Martin Hope
    pandisvezia How can I copy the contents of a folder to another folder in a different directory using terminal? 2011-12-11 17:19:37 +0800 CST
  • Martin Hope
    Scott Szretter Where is the cron / crontab log? 2011-08-12 04:06:43 +0800 CST
  • Martin Hope
    TheXed How do I install a .deb file via the command line? 2011-05-07 09:40:28 +0800 CST
  • Martin Hope
    EmmyS What command do I need to unzip/extract a .tar.gz file? 2011-02-09 14:50:41 +0800 CST
  • Martin Hope
    Ivan How to list all installed packages 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra Unable to lock the administration directory (/var/lib/dpkg/) is another process using it? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry How do I determine the total size of a directory (folder) from the command line? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher "The following packages have been kept back:" Why and how do I solve it? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford How can PPAs be removed? 2010-07-30 01:09:42 +0800 CST

Related Questions

Trending Tags

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • Home
  • Questions
    • Hot Questions
    • New Questions
  • Tags
  • Help

Footer

SnapOverflow

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Privacy Policy

Help

© 2022 SOF-TR. All Rights Reserve