中国网管论坛's Archiver

aohanemail 发表于 2008-4-25 08:46

mv命令的覆盖问题

我想问个关于在linux中用mv命令将一个目录移动到另一个目录下,而另一个目录下已经存在同名子目录的覆盖问题。

比如有这样两个目录:/test1和/test2:现在我想要将/test1移动至/test2下:形成/test2/test1的目录结构:而/test2/test1这个目录已经存在:那么我在我使用命令:mv test1 test2时linux会提示错误信息:cannot overwrite directory 'test2/test1'。

请问应该怎么输入命令才可以达到我想要的效果呢?
mv -f test1 test2是不行的,一样会有错误信息。

iamshiyu 发表于 2008-4-25 09:19

mv test1 test2/

aohanemail 发表于 2008-4-25 10:20

还是不行啊,楼上的提供的命令跟mv test1 test2的执行结果是一样的,如果/test2/test1不存在的话,这两条命令都可以成功的把test1移动到test2下面去,但是在/test2/test1已经存在的情况下,两条命令都执行失败。

iamshiyu 发表于 2008-4-25 10:32

那干脆先rm -rf吧……

aohanemail 发表于 2008-4-25 11:36

问题是我就是想实现/test1对/test2/test1的覆盖啊,在windows下进行文件操作不是经常会碰到这样的情况么,将一个文件夹移动到另一个文件夹,而另一个文件夹下已经存在同名的文件夹,可以在确认操作后将两个文件夹的内容合并,难道linux下是无法做到的么?

iamshiyu 发表于 2008-4-25 13:11

能实现是肯定的,怎么实现还得研究……

isbasic 发表于 2008-4-25 13:19

man mv pages

MV(1)                            User Commands                           MV(1)



NAME
       mv - move (rename) files

SYNOPSIS
       mv [OPTION]... [-T] SOURCE DEST
       mv [OPTION]... SOURCE... DIRECTORY
       mv [OPTION]... -t DIRECTORY SOURCE...

DESCRIPTION
       Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

       Mandatory  arguments  to  long  options are mandatory for short options
       too.

       --backup[=CONTROL]
              make a backup of each existing destination file

       -b     like --backup but does not accept an argument

       -f, --force
              do not prompt before overwriting

       -i, --interactive
              prompt before overwrite

       --strip-trailing-slashes remove any trailing slashes from each SOURCE
              argument

       -S, --suffix=SUFFIX
              override the usual backup suffix

       -t, --target-directory=DIRECTORY
              move all SOURCE arguments into DIRECTORY

       -T, --no-target-directory
              treat DEST as a normal file

       -u, --update
              move only when the SOURCE file is  newer  than  the  destination
              file or when the destination file is missing

       -v, --verbose
              explain what is being done

       --help display this help and exit

       --version
              output version information and exit

       The   backup   suffix   is  `~',  unless  set  with  --suffix  or  SIM-
       PLE_BACKUP_SUFFIX.  The version control method may be selected via  the
       --backup  option  or  through the VERSION_CONTROL environment variable.
       Here are the values:

       none, off
              never make backups (even if --backup is given)

       numbered, t
              make numbered backups

       existing, nil
              numbered if numbered backups exist, simple otherwise

       simple, never
              always make simple backups

AUTHOR
       Written by Mike Parker, David MacKenzie, and Jim Meyering.

REPORTING BUGS
       Report bugs to <[email]bug-coreutils@gnu.org[/email]>.

COPYRIGHT
       Copyright (C) 2006 Free Software Foundation, Inc.
       This is free software.  You may redistribute copies  of  it  under  the
       terms       of       the      GNU      General      Public      License
       <[url]http://www.gnu.org/licenses/gpl.html[/url]>.  There is NO WARRANTY,  to  the
       extent permitted by law.

SEE ALSO
       The  full  documentation  for mv is maintained as a Texinfo manual.  If
       the info and mv programs are properly installed at your site, the  com-
       mand

              info mv

       should give you access to the complete manual.



mv 5.97                         September 2007                           MV(1)


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

从上面看来,-t,-f,-u参数都在选择的范围内,不过我还不太明白DEST是什么呵呵 ,希望有明白的朋友指点一下。

iamshiyu 发表于 2008-4-25 13:35

可能win和lin的文件夹覆盖方式真的不一样……

iamshiyu 发表于 2008-4-25 13:36

dest应该就是destination的简写了,目标。

aohanemail 发表于 2008-4-28 10:14

看来真的是无法实现了。。。。。。

我辗转几个论坛都没有得到答案,没想到linux下还有这么大一个bug啊!

iamshiyu 发表于 2008-4-28 10:28

这并不能算bug,文件系统的结构不同……只是你用惯了win而已……
这样的文件系统会极大减小出错的机会,安全性也比win高。
如果你想要覆盖掉,可以自己编一个shell实现,效率可能低一点,但是实现起来没有问题。

tanming 发表于 2008-4-29 11:21

你上面肯定有问题的,那有MV是不能覆盖的,你再重新做个实验看看 保证好的

iamshiyu 发表于 2008-4-29 12:10

[quote]原帖由 [i]tanming[/i] 于 2008-4-29 11:21 发表 [url=http://bbs.bitscn.com/redirect.php?goto=findpost&pid=1582406&ptid=167165][img]http://bbs.bitscn.com/images/common/back.gif[/img][/url]
你上面肯定有问题的,那有MV是不能覆盖的,你再重新做个实验看看 保证好的 [/quote]
没有特别深入研究过,但的确会有一些问题。

isbasic 发表于 2008-4-29 14:47

我个人认为这个不应该是用mv的,而且如果使用mv的话最多是一个类似剪切+粘贴的过程,不过建议如果是需要作一个整合的话可以考虑diff这个命令而不是一个mv,某种环境下,它更像一个重命名的命令,这一点我想还是有待斟酌的。

iamshiyu 发表于 2008-4-29 16:03

[quote]原帖由 [i]isbasic[/i] 于 2008-4-29 14:47 发表 [url=http://bbs.bitscn.com/redirect.php?goto=findpost&pid=1583180&ptid=167165][img]http://bbs.bitscn.com/images/common/back.gif[/img][/url]
我个人认为这个不应该是用mv的,而且如果使用mv的话最多是一个类似剪切+粘贴的过程,不过建议如果是需要作一个整合的话可以考虑diff这个命令而不是一个mv,某种环境下,它更像一个重命名的命令,这一点我想还是有待 ... [/quote]
你对系统还是比我熟悉多了,以后多指教。

isbasic 发表于 2008-4-29 16:10

我在努力摆脱GUI呵呵,当然,电影除外

页: [1]

Powered by Discuz! Archiver 6.1.0  © 1999-2008 bbs.bitsCN.com