管道 tar 命令中提供的文件名在哪里?
Where is the name of the file supplied from in piped tar command?
考虑命令
link to this。向下滚动一点,您会找到它。
(cd /source/directory && tar cf - . ) | (cd /dest/directory && tar xpvf -)
文档中如何解释此命令:
(cd /source/directory && tar cf - . ) | (cd /dest/directory && tar xpvf -)
# Move entire file tree from one directory to another
# [courtesy Alan Cox <a.cox@swansea.ac.uk>, with a minor change]
# 1) cd /source/directory
# Source directory, where the files to be moved are.
# 2) &&
# "And-list": if the 'cd' operation successful,
# then execute the next command.
# 3) tar cf - .
# The 'c' option 'tar' archiving command creates a new archive,
# the 'f' (file) option, followed by '-' designates the target file
# as stdout, and do it in current directory tree ('.').
# 4) |
# Piped to ...
# 5) ( ... )
# a subshell
# 6) cd /dest/directory
# Change to the destination directory.
# 7) &&
# "And-list", as above
# 8) tar xpvf -
# Unarchive ('x'), preserve ownership and file permissions ('p'),
# and send verbose messages to stdout ('v'),
# reading data from stdin ('f' followed by '-').
#
# Note that 'x' is a command, and 'p', 'v', 'f' are options.
#
# Whew!
上面的解释有几处我不明白-
- 在第三步中它指出
f -
将目标文件指定为标准输出,但现在在创建存档时输出中没有任何内容。从哪里提供的文件名?
- 在第八步它说它从标准输入读取数据,但我没有给出任何输入。流中是否还有输入?
这个命令工作正常,但我对它的工作原理有点困惑。
你从代码中引用的解释非常好。我希望我阅读(或编写!)的每一个脚本都得到很好的记录。
In the 3rd step it states f -
designates the target file as stdout, but there is nothing in output right now, while creating archive, from where is the name of file supplied?
没有文件名。存档数据被写入 stdout,进程的标准输出流。如果它没有通过管道传输到另一个程序中,那么它将显示在屏幕上。
In the 8th step it states, it reads data from stdin, but I didn't give any input, is there any input left in the stream?
第一个 tar
命令的输出(到其标准输出)通过管道传输到第二个 tar
命令的(标准输入),如文档的第 4 步所述。你不能直接给第二个tar
任何输入,因为它是从管道读取输入,而不是键盘或任何常规文件。
第一个括号执行以下操作:它将目录更改为 /source/directory/ 并生成一个 tar 文件,其内容为当前目录 "." 并将其发送到标准输出。
第二个括号将目录更改为 /dest/directory/ 并将其从标准输入中读取的存档提取到那里。
也就是说,你 tar “/source/directory” 的内容,你 untar 它在“/dest/directory”中没有使用中间文件来这样做,只是一个管道 ”|” 来建立两个命令之间的连接。
注意:括号创建了一个子进程,所以你有一个子进程执行 tar c 和另一个执行 tar f 运行 同时,一个子进程的输出被馈送到第二个子进程。
In the 3rd step it states f - designates the target file as stdout, but there is nothing in output right now, while creating archive, from where is the name of file supplied?
stdin
和stdout
是OS为每个进程创建的两个数据流(一个命令是运行)。该进程可能会也可能不会使用这些流,但 OS 无论如何都会创建它们。
这是第一个进程将其输出写入的位置。
内存数据流不需要文件名,它们不是磁盘上的常规文件。
In the 8th step it states, it reads data from stdin, but I didn't give any input, is there any input left in the stream?
有 2 个进程由管道分隔 |
,您可以将其可视化为:
(cd /source/directory && tar cf - . ) -> [stdout] -> | -> [stdin] -> (cd /dest/directory && tar xpvf -)
因此,左侧的第一个进程将输出写入它自己的 stdout
。管道 |
是 OS 级别的管道,它将数据从上一个进程的 stdout
泵送到下一个进程的 stdin
。右侧的进程从其 stdin
.
中读取数据
此外,与管道 |
模式一样,破折号 -
是一种常见的 cli 模式,用于代替文件,告诉命令读写数据 to/from stdin/stdout
而不是文件。
考虑命令
link to this。向下滚动一点,您会找到它。
(cd /source/directory && tar cf - . ) | (cd /dest/directory && tar xpvf -)
文档中如何解释此命令:
(cd /source/directory && tar cf - . ) | (cd /dest/directory && tar xpvf -)
# Move entire file tree from one directory to another
# [courtesy Alan Cox <a.cox@swansea.ac.uk>, with a minor change]
# 1) cd /source/directory
# Source directory, where the files to be moved are.
# 2) &&
# "And-list": if the 'cd' operation successful,
# then execute the next command.
# 3) tar cf - .
# The 'c' option 'tar' archiving command creates a new archive,
# the 'f' (file) option, followed by '-' designates the target file
# as stdout, and do it in current directory tree ('.').
# 4) |
# Piped to ...
# 5) ( ... )
# a subshell
# 6) cd /dest/directory
# Change to the destination directory.
# 7) &&
# "And-list", as above
# 8) tar xpvf -
# Unarchive ('x'), preserve ownership and file permissions ('p'),
# and send verbose messages to stdout ('v'),
# reading data from stdin ('f' followed by '-').
#
# Note that 'x' is a command, and 'p', 'v', 'f' are options.
#
# Whew!
上面的解释有几处我不明白-
- 在第三步中它指出
f -
将目标文件指定为标准输出,但现在在创建存档时输出中没有任何内容。从哪里提供的文件名? - 在第八步它说它从标准输入读取数据,但我没有给出任何输入。流中是否还有输入?
这个命令工作正常,但我对它的工作原理有点困惑。
你从代码中引用的解释非常好。我希望我阅读(或编写!)的每一个脚本都得到很好的记录。
In the 3rd step it states
f -
designates the target file as stdout, but there is nothing in output right now, while creating archive, from where is the name of file supplied?
没有文件名。存档数据被写入 stdout,进程的标准输出流。如果它没有通过管道传输到另一个程序中,那么它将显示在屏幕上。
In the 8th step it states, it reads data from stdin, but I didn't give any input, is there any input left in the stream?
第一个 tar
命令的输出(到其标准输出)通过管道传输到第二个 tar
命令的(标准输入),如文档的第 4 步所述。你不能直接给第二个tar
任何输入,因为它是从管道读取输入,而不是键盘或任何常规文件。
第一个括号执行以下操作:它将目录更改为 /source/directory/ 并生成一个 tar 文件,其内容为当前目录 "." 并将其发送到标准输出。
第二个括号将目录更改为 /dest/directory/ 并将其从标准输入中读取的存档提取到那里。
也就是说,你 tar “/source/directory” 的内容,你 untar 它在“/dest/directory”中没有使用中间文件来这样做,只是一个管道 ”|” 来建立两个命令之间的连接。
注意:括号创建了一个子进程,所以你有一个子进程执行 tar c 和另一个执行 tar f 运行 同时,一个子进程的输出被馈送到第二个子进程。
In the 3rd step it states f - designates the target file as stdout, but there is nothing in output right now, while creating archive, from where is the name of file supplied?
stdin
和stdout
是OS为每个进程创建的两个数据流(一个命令是运行)。该进程可能会也可能不会使用这些流,但 OS 无论如何都会创建它们。
这是第一个进程将其输出写入的位置。
内存数据流不需要文件名,它们不是磁盘上的常规文件。
In the 8th step it states, it reads data from stdin, but I didn't give any input, is there any input left in the stream?
有 2 个进程由管道分隔 |
,您可以将其可视化为:
(cd /source/directory && tar cf - . ) -> [stdout] -> | -> [stdin] -> (cd /dest/directory && tar xpvf -)
因此,左侧的第一个进程将输出写入它自己的 stdout
。管道 |
是 OS 级别的管道,它将数据从上一个进程的 stdout
泵送到下一个进程的 stdin
。右侧的进程从其 stdin
.
此外,与管道 |
模式一样,破折号 -
是一种常见的 cli 模式,用于代替文件,告诉命令读写数据 to/from stdin/stdout
而不是文件。