About
Have you ever puzzled about how to deal with symbolic files when copying files? Here are some useful commands for you. You can also find them in this gist
Background
Given a directory structure like this:
.
├── a
│ └── a.txt
├── b
│ └── b.txt
├── c
├── s1 -> a
└── s2 -> b
3 directories
a
, b
and c
. 2 symbolic links s1
and s2
Local
Copy files / directories locally
Case 1: Copy with symbolic links preserved
Suppose you need to copy from
src
to dst
, you can run following:$ cp -r src/* dst/
$ tree dst
dst
├── a
│ └── a.txt
├── b
│ └── b.txt
├── c
├── s1 -> a
└── s2 -> b
Case 2: Copy without symbolic links but a real directory
Suppose you need to copy without a symbolic link but a real directory, you can run following:
$ cp -rL src/* dst/
$ tree dst
dst
├── a
│ └── a.txt
├── b
│ └── b.txt
├── c
├── s1
│ └── a.txt
└── s2
└── b.txt
Remote
Copy files / directories from host to host. We need command
scp
.Case 1: Copy with symbolic links preserved
Suppose you need to copy from
src
to dst
, you can try tar scp
combo:# Tar the entire folder
$ tar cf src.tar.gz src/
# Scp to the remote side
# Remember to replace `localhost` and `pwd` with your own variables
$ scp src.tar.gz localhost:`pwd`/subdir/
# Untar the zip in `subdir/src.tar.gz` in folder `subdir`
$ tar -x -C subdir/ -f subdir/src.tar.gz
# Rename the `subdir/src` to `subdir/dst`
$ mv subdir/src subdir/dst
$ tree subdir/dst
subdir/dst/
├── a
│ └── a.txt
├── b
│ └── b.txt
├── c
├── s1 -> a
└── s2 -> b
Or, you can do them on the fly:
# Open a new terminal for the destination host and run following command
# Use NC to listen on any port (EG: 12345) and untar it on the fly
$ nc -l 12345 | tar xf -
# Open another new terminal for the source host and run following command
# Tar the entire folder and send them through netcat
# Remember to replace `localhost` and `pwd` with your own variables
$ tar cf - src/ | nc localhost 12345
# Verify in destionation host
$ mv src dst
$ tree dst
dst
├── a
│ └── a.txt
├── b
│ └── b.txt
├── c
├── s1 -> a
└── s2 -> b
Case 2: Copy without symbolic links but a real directory
Suppose you need to copy without a symbolic link but a real directory, you can run following:
# Scp to the remote side
# Remember to replace `localhost` and `pwd` with your own variables
$ scp -r src/* localhost:`pwd`/dst
$ tree dst
dst/
├── a
│ └── a.txt
├── b
│ └── b.txt
├── c
├── s1
│ └── a.txt
└── s2
└── b.txt
沒有留言:
張貼留言