Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2272
  • Last Modified:

How to copy the whole directory structure in Linux

Hi Experts,

How can I copy a directory structure in Linux?
0
thanesh
Asked:
thanesh
1 Solution
 
sunnycoderCommented:
Hi thanesh,

Use cp command with -r option

cp -r source/dir destination/dir

Cheers!
0
 
ravenplCommented:
I suggest using: cp -av /source/dir /destination/dir
strip 'v' if You don't like the mess on the screen.
0
 
ahoffmannCommented:
cd dest ; (cd src && tar cf - dir)|tar xf -
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Duncan RoeSoftware DeveloperCommented:
I have a trecpy script:

if [ "$1" = "-v" ];then
  v=v
  shift
else
  v=
fi
if [ -z "$1" -o -z "$2" ];then
  echo 'Usage:-' $0 '[-v] <path> <target-dir>'
  exit 1
fi
if [ ! -d $2 ];then
  echo $2 is not a directory
  exit 1
fi
if [ ! \( -d $1 -o -f $1 \) ];then
  echo $1 is neither a file nor a directory
  exit 1
fi
tar -cf - --atime-preserve $1 | tar -C $2 -x${v}pUf -

I agree with ahoffman that tar is best. --atime-preserve lets you do a copy without disturbing access time of the source. In the destination, U deletes before writing so you won't be stymied by existing symbolic links. p ensures protections are preserved regardless of your umask.
0
 
yvovoldersCommented:
One can use 'find . -print' and pipe it to 'cpio -pd'

find . -print | cpio -pd /destinationpath

You can use 'man cpio' for the options of cpio.
0
 
wnrossCommented:
I personally use tar, but for completeness heres rsync

rsync -av /some/path/  /desth/path/

The v is verbose, the a means archive mode: all permisions, ownerships, timestamps are preserved, symbolic links are honored, just always remember the trailing slashes.

Cheers,
-Bill
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now