Home  >  Q&A  >  body text

python - 管道符和ssh传文件

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

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

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

阿神阿神2740 days ago759

reply all(3)I'll reply

  • 大家讲道理

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

    Explanation of command parameters:

    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>.

    I don’t know what’s going on, the help documents of the two commands are the same, the developer is lazy

    In other words, -c does not compress the file, but outputs it directly to standard output.

    gunzip -, instead of receiving a compressed file from a file, it receives it from standard input.

    |:管道
    >: redirect


    Analysis of the entire command

    gzip -c aa.txt | ssh root@192.168.1.1 "gunzip -c - > /home/bb.txt"
    • gzip -c aa.txt: Compress the aa.txt file and output the compressed result to standard output

    • ssh root@192.168.1.1 "命令":Execute commands

    • on the remote machine
    • Commandgunzip -c -: Decompress the file. The original compressed file is passed in from the standard input, and the output result is directly output to the standard output

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


    Such an operation on a folder will fail

    gzip does not support directory operations

    reply
    0
  • 阿神

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

    You can either completely switch to using netcat for sending and receiving.
    Or, compress it with local tar and decompress it on the opposite end.

    reply
    0
  • PHP中文网

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

    Piping takes the output of the previous command as input. You can also compress it into a temporary file first and then use scp

    reply
    0
  • Cancelreply