linux - Bash 脚本 : "Dump" directory using mv - current directory

标签 linux bash ubuntu scripting mv

我正在编写一个 bash 脚本,它将指定的目录转储到目标目录(如果未指定目标,则转储到当前目录。)如果指定了目标目录,该脚本可以正常工作,但是当我尝试使用当前目录(通过 $PWD)时,出现以下错误。

mv: `test' and `/home/user/programming/bash/test' are the same file

当前工作目录只是/home/user/programming/bash。由于某种原因,我想要转储的目录被附加到当前目录路径中。我为转储创建的函数如下。

dump_dir()
{
    echo "Dumping \"$1\" into \"$2\"..."
    mv -i $1 $2
}

我该如何使用 mv 来做到这一点?有更好的命令来执行此操作吗?

编辑:

运行命令 dump_dir test $PWD 会给出输出

Dumping "test" into "/home/user/programming/bash"...

然后是我上面所说的错误。由于某种原因,mv 命令采用 $PWD 并将“test”附加到其末尾。

编辑:

要复制错误,请打开终端并键入

mv some_directory $PWD

显然将 some_directory 替换为实际目录;)

编辑:再次...抱歉:)

当用户仅提供要转储的目录时,我将按如下方式调用该函数。

dump_dir $1 $PWD

最佳答案

所有这些答案都大错特错。您正在尝试做一些没有意义的事情 - mv test test。如何指定源和目标并不重要,这就是它的作用 - 尝试将目录“移动”到它已经所在的位置。

mv test test 是一个无操作,无论 test 是文件、目录、符号链接(symbolic link)、设备还是其他什么。该命令甚至不会“查看”您要移动的物体的内部以了解它是什么。

如果您想将 test 目录的内容移至当前目录,那就不同了 - 类似于 mv test/* .,尽管这不适用于与现有子目录同名的嵌套目录。

如果您想在文件系统中移动整个目录树,请使用tarrsync或其他东西。

关于linux - Bash 脚本 : "Dump" directory using mv - current directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23975802/

相关文章:

linux - 使用 iptables 打开端口

linux - 使用 Shell 脚本从长字符串中提取子字符串

linux - 为什么 "lea..and..push"汇编代码经常出现在函数的开头?

bash - systemctl 服务 systemd-notify 不适用于非 root 用户

bash - Bash脚本: log commands that produced errors?

qt - ubuntu下丑陋的Qt应用程序

linux - 压缩时排除选定的子目录

bash - 在ubuntu中通过命令行设置时间同步

ruby-on-rails - 在 ubuntu 中取消 bash 的源码

bash - 在 bash 中重用字符串