How can I copy a hidden directory recursively and preserving its permissions?

13,651

Solution 1

Don't specify the files or Directory

Lets say you created the new folder (or are going to create one) and want to copy the files to it after the folder is created

mkdir /test/folder
cp -rp /path/to/copy/. /test/folder

This will copy all files/folder recursively from /path/from/copy in to the already existing folder created on the first line.

Another approach is tar. For example:

$cd foo
$tar cf - . | tar -C /path/to/bar -x

Using rsync :

rsync -av src dest

Solution 2

mkdir backupcache    
cp -rp .cache/. backupcache 

that way only the content (/.) of .cache gets copied, not the .cache part.

Share:
13,651

Related videos on Youtube

Ezequiel
Author by

Ezequiel

Updated on September 18, 2022

Comments

  • Ezequiel
    Ezequiel almost 2 years
    mkdir backupcache    
    cp -rp .cache backupcache # or cp -rp \.cache backupcache does not work
    

    nothing gets copied and directory backupcache remains empty

    • Jasen
      Jasen about 8 years
      are you sure nothing gets copied? I'd expect there to be backupcache/.cache etc
  • Rahul
    Rahul about 8 years
    @munish tars up the current directory to stdout then pipes it to second tar for untarring stdin on destination.
  • UncaAlby
    UncaAlby over 4 years
    My fave: (cd {source dir} && tar cpf - * ) | (cd {target dir} && tar xpvf - ) which works with almost any version of "tar"