gpt4 book ai didi

Linux - 通过覆盖将文件从源移动到目标

转载 作者:太空宇宙 更新时间:2023-11-04 09:16:02 25 4
gpt4 key购买 nike

首先,如果我重复了一个已经存在的问题,我想道歉,但这个问题太重要了。

我的 Linux 服务器上有两个目录: - /tmp/tmp 作为源 - /var 作为目的地

/var 目录中已经有大约 500 个 txt 文件,我想将所有文件从 /tmp 目录(大约 200 个文件)移动到 /var 目录,替换那些已经存在的同名目录,但不要触及那些不在 /tmp 中的目录。

实际例子:/var 文件:a.txt、b.txt、c.txt、d.txt/tmp 文件: a.txt , b.txt结果:/var 文件:a.txt(from /tmp) , b.txt(from /tmp) , c.txt , d .txt

不确定 mv 是否是执行此操作的正确方法,所以提前谢谢你们! :)

最佳答案

这可以通过rsync来实现

引用rsync manual获取更多信息。

Local:  rsync [OPTION...] SRC... [DEST]
Access via remote shell:
Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
Access via rsync daemon:
Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST

对于你的情况,

rsync -avzh /tmp/ /var/

如果你想测试一下

rsync -avzh --dry-run /tmp/ /var/

-a, --archive archive mode;
-v, --verbose increase verbosity
-z, --compress compress file data during the transfer
-h, --human-readable output numbers in a human-readable format

详细解说

-v, --verbose This option increases the amount of information you are given during the transfer. By default, rsync works silently. A single -v will give you information about what files are being transferred and a brief summary at the end. Two -v options will give you information on what files are being skipped and slightly more information at the end. More than two -v options should only be used if you are debugging rsync. Note that the names of the transferred files that are output are done using a default --out-format of lq%n%Lrq, which tells you just the name of the file and, if the item is a link, where it points. At the single -v level of verbosity, this does not mention when a file gets its attributes changed. If you ask for an itemized list of changed attributes (either --itemize-changes or adding lq%irq to the --out-format setting), the output (on the client) increases to mention all items that are changed in any way. See the --out-format option for more details.

-a, --archive This is equivalent to -rlptgoD. It is a quick way of saying you want recursion and want to preserve almost everything (with -H being a notable omission). The only exception to the above equivalence is when --files-from is specified, in which case -r is not implied. Note that -a does not preserve hardlinks, because finding multiply-linked files is expensive. You must separately specify -H. --no-OPTION You may turn off one or more implied options by prefixing the option name with lqno-rq. Not all options may be prefixed with a lqno-rq: only options that are implied by other options (e.g. --no-D, --no-perms) or have different defaults in various circumstances (e.g. --no-whole-file, --no-blocking-io, --no-dirs). You may specify either the short or the long option name after the lqno-rq prefix (e.g. --no-R is the same as --no-relative). For example: if you want to use -a (--archive) but don't want -o (--owner), instead of converting -a into -rlptgD, you could specify -a --no-o (or -a --no-owner). The order of the options is important: if you specify --no-r -a, the -r option would end up being turned on, the opposite of -a --no-r. Note also that the side-effects of the --files-from option are NOT positional, as it affects the default state of several options and slightly changes the meaning of -a (see the --files-from option for more details).

-z, --compress With this option, rsync compresses the file data as it is sent to the destination machine, which reduces the amount of data being transmitted -- something that is useful over a slow connection. Note that this option typically achieves better compression ratios than can be achieved by using a compressing remote shell or a compressing transport because it takes advantage of the implicit information in the matching data blocks that are not explicitly sent over the connection.

-h, --human-readable Output numbers in a more human-readable format. This makes big numbers output using larger units, with a K, M, or G suffix. If this option was specified once, these units are K (1000), M (1000*1000), and G (1000*1000*1000); if the option is repeated, the units are powers of 1024 instead of 1000.

关于Linux - 通过覆盖将文件从源移动到目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47694469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com