首頁  >  問答  >  主體

python - 管道符和ssh传文件

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

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

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

阿神阿神2740 天前757

全部回覆(3)我來回復

  • 大家讲道理

    大家讲道理2017-04-18 10:21:20

    指令參數解釋:

    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不支援對目錄操作

    回覆
    0
  • 阿神

    阿神2017-04-18 10:21:20

    你要嘛完全換用netcat來收發。
    要么,本地tar壓縮一下,對端解壓縮。

    回覆
    0
  • PHP中文网

    PHP中文网2017-04-18 10:21:20

    管道是將前一個指令輸出作為輸入。 也可以先壓縮成一個臨時文件,再用scp

    回覆
    0
  • 取消回覆