---
# System prepended metadata

title: Linux 遠端↔本地 傳送檔案同時壓縮/解壓縮
tags: [筆記]

---

# 壓縮本地檔案直接傳到遠端

```bash
tar cvpf - <directory_to_be_compressed> --use-compress-program=lbzip2 | ssh <remote-user>@<server-ip> dd of=</path_to_your_directory/file_name.tar.bz2>
```

- `'-'`：表示將結果丟到stdout
    - 利用｜(pipe)
        - tar的stdout當作ssh dd的stdin
- `lbzip2`
    - 平行化壓縮
- `c`：compress
- `v`：show process
- `dd`: 將前面的pipe過來的東西複製到後面的地址
- `of=`:後面接的就是要複製到的地方


# 從遠端傳送壓縮檔到本地同時解壓縮

```bash
ssh remote-user@server-ip "cat <file_to_be_decompressed>" | tar xvf - --use-compress-program=lbzip2
```

- `cat file_to_be_decompressed`：將結果丟到stdout
- `｜`：cat的stdout當作tar的stdin
- `-`：從stdin接收input
- `x`：decompress

---

# Reference:

- 壓縮後送到到遠端：[https://wenchiching.wordpress.com/2009/11/22/tar-ssh-遠端異地備份/](https://wenchiching.wordpress.com/2009/11/22/tar-ssh-%E9%81%A0%E7%AB%AF%E7%95%B0%E5%9C%B0%E5%82%99%E4%BB%BD/)
- 從遠端下載同時解壓縮：[https://stackoverflow.com/questions/30795466/extract-file-from-remote-ssh-host](https://stackoverflow.com/questions/30795466/extract-file-from-remote-ssh-host)
- 平行化壓縮/解壓縮：[https://www.cnx-software.com/2016/12/16/compress-decompress-files-faster-with-lbzip2-multi-threaded-version-of-bzip2/](https://www.cnx-software.com/2016/12/16/compress-decompress-files-faster-with-lbzip2-multi-threaded-version-of-bzip2/)