python - 管道符和ssh传文件
阿神
阿神 2017-04-18 10:19:20
0
3
766

看到可以用一条命令传输文件

gzip -c aa.txt | ssh root@192.168.1.1 " gunzip -c - > /home/bb.txt"

请问这条命令怎么理解?
还有,发现对文件夹进行这样的操作会失败,有什么办法传输文件夹么?
求指教

阿神
阿神

闭关修行中......

全部回复(3)
大家讲道理

命令参数解释:

gzip -h

Usage: gzip [OPTION]... [FILE]...
Compress or uncompress FILEs (by default, compress FILES in-place).

Mandatory arguments to long options are mandatory for short options too.

  -c, --stdout      write on standard output, keep original files unchanged
  -d, --decompress  decompress
  -f, --force       force overwrite of output file and compress links
  -h, --help        give this help
  -l, --list        list compressed file contents
  -L, --license     display software license
  -n, --no-name     do not save or restore the original name and time stamp
  -N, --name        save or restore the original name and time stamp
  -q, --quiet       suppress all warnings
  -r, --recursive   operate recursively on directories
  -S, --suffix=SUF  use suffix SUF on compressed files
  -t, --test        test compressed file integrity
  -v, --verbose     verbose mode
  -V, --version     display version number
  -1, --fast        compress faster
  -9, --best        compress better
    --rsyncable   Make rsync-friendly archive

With no FILE, or when FILE is -, read standard input.

gunzip -h

Usage: gzip [OPTION]... [FILE]...
Compress or uncompress FILEs (by default, compress FILES in-place).

Mandatory arguments to long options are mandatory for short options too.

  -c, --stdout      write on standard output, keep original files unchanged
  -d, --decompress  decompress
  -f, --force       force overwrite of output file and compress links
  -h, --help        give this help
  -l, --list        list compressed file contents
  -L, --license     display software license
  -n, --no-name     do not save or restore the original name and time stamp
  -N, --name        save or restore the original name and time stamp
  -q, --quiet       suppress all warnings
  -r, --recursive   operate recursively on directories
  -S, --suffix=SUF  use suffix SUF on compressed files
  -t, --test        test compressed file integrity
  -v, --verbose     verbose mode
  -V, --version     display version number
  -1, --fast        compress faster
  -9, --best        compress better
    --rsyncable   Make rsync-friendly archive

With no FILE, or when FILE is -, read standard input.

Report bugs to <bug-gzip@gnu.org>.

不知道闹哪样,两条命令的帮助文档是一样的,开发者偷懒了

也就是说 -c 不压缩文件,而是直接输出到标准输出。

gunzip -,不是从文件接收压缩文件,而是从标准输入接收。

|:管道
>:重定向


整条命令分析

gzip -c aa.txt | ssh root@192.168.1.1 "gunzip -c - > /home/bb.txt"
  • gzip -c aa.txt:压缩aa.txt文件,并将压缩后的结果输出到标准输出

  • ssh root@192.168.1.1 "命令":在远程机器上执行命令

  • 命令gunzip -c -:解压文件,原压缩文件是由标准输入传入的,输出结果直接输出到标准输出

  • > /home/bb.txt:将标准输出重定向到文件/home/bb.txt


对文件夹进行这样的操作会失败

gzip不支持对目录操作

阿神

你要么完全换用netcat来收发。
要么,本地tar压缩一下,对端解压缩。

洪涛

管道是将前一个命令输出作为输入。 也可以先压缩成一个临时文件,再用scp

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!