mv/cp 命令在 bash 中无法按预期使用 xargs
您好,我在 /tmp 下有 2 个包含这些内容的父目录:
注意父目录名称带有“;”在其中 - 不建议在类 Unix 系统中使用,但这些目录是由外部应用程序推送的,这就是我们必须处理它的方式。
我需要将这些父目录(及其内容)移动到 /tmp/archive - 在 RHEL 7.9 (Maipo) 机器上
我的简单代码:
ARCHIVE="/tmp/archive"
[ -d "${ARCHIVE}" ] && mkdir -p "${ARCHIVE}"
ls -lrth /tmp | awk "\$NF ~ /2021-.*/{print \$NF}" | xargs -I "{}" mv "{}" ${ARCHIVE}/
但是当我运行此脚本时,mv
复制其中之一父目录,但对于另一个目录,它只是移动父目录的内容,而不是目录本身:
当我在 Ubuntu 18 系统中运行相同的脚本时,行为符合预期,即父目录被移动到存档文件夹。
为什么 之间的行为存在这种差异Ubuntu
和 RHEL
系统,用于相同的脚本
Hi I have 2 parent directories with these contents, under /tmp:
Note parent directory names have ";" in it- not recommended in Unix like systems, but those directories are pushed by an external application, and that's the way we have to deal with it.
I need to move these parent directories (along with their contents) to /tmp/archive - on a RHEL 7.9 (Maipo) machine
My simple code:
ARCHIVE="/tmp/archive"
[ -d "${ARCHIVE}" ] && mkdir -p "${ARCHIVE}"
ls -lrth /tmp | awk "\$NF ~ /2021-.*/{print \$NF}" | xargs -I "{}" mv "{}" ${ARCHIVE}/
But when I run this script, mv
copies one of the parent directory as it is, but for the other one, it just moves the contents of the parent directory, not the directory itself:
I tried the same script with cp -pvr
command in place of mv
, and its the same behavior
When I run the same script in a Ubuntu 18
system, the behavior is as expected i.e - the parent directories get moved to archive folder.
Why is there this difference in behavior between a Ubuntu
and a RHEL
system, for the same script
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试更简单的方法:
Try a simpler approach: