我想移到父目录的子目录中有大量文件和目录。目标目录中已经有一些文件和目录需要覆盖。仅存在于目标中的文件应保持不变。我可以强迫mv这样做吗?(mv * ..)抱怨
mv: cannot move `xyz' to `../xyz': Directory not empty我想念什么?
mv -f答案不正确。
                我想移到父目录的子目录中有大量文件和目录。目标目录中已经有一些文件和目录需要覆盖。仅存在于目标中的文件应保持不变。我可以强迫mv这样做吗?(mv * ..)抱怨
mv: cannot move `xyz' to `../xyz': Directory not empty我想念什么?
mv -f答案不正确。
                Answers:
你必须将它们复制到目标,然后删除源,使用命令cp -r * ..之后rm -rf *。
我认为您不能使用来“合并”目录mv。
mv是因为您在同一文件系统上,所以速度更快?如果cp -l用于创建硬链接而不是实际移动文件怎么办?
                    cp -a而不是cp -r来保留文件属性(时间戳,权限等)。
                    cp -rl source destination && rm -r source。
                    rsync可能是一个更好的选择。就像一样简单rsync -a subdir/ ./。
我的测试树filename:contents格式:
./file1:root
./file2:root
./dir/file3:dir
./dir/file4:dir
./subdir/dir/file3:subdir
./subdir/file1:subdir
正在运行rsync:
$ rsync -a -v subdir/ ./
sending incremental file list
./
file1
dir/
dir/file3
给出:
./file1:subdir
./file2:root
./dir/file3:subdir
./dir/file4:dir
./subdir/dir/file3:subdir
./subdir/file1:subdir
然后,要进行仿真mv,您可能想要删除源目录:
$ rm -r subdir/给予:
./file1:subdir
./file2:parent
./dir/file3:subdir
./dir/file4:dir
如果这是错误的,您是否可以提供一个类似的示例(例如,使用该答案顶部附近的测试树)并获得所需的结果?
rm -r在末尾添加了使其与基本上相同mv。
                    mv是原子的,保留inode编号(因此文件可以保持打开状态),并且无需花费时间和空间即可进行复制。
                    cp -r; rm -r。我认为从这个意义上讲,rsync也值得一提。
                    您可以使用cp和进行此操作rm,但无需复制大量的数据(大概)是要避免传输数据。@mattdm在其评论中暗示了这一点,而对另一个问题的答案则对各种选项进行了更完整的讨论。
cp -rlf source destination
rm -r source
本质上,该命令的-l选项将cp创建到文件的硬链接,而不是将其数据复制到新文件。
cp -al source destination为了保留所有者信息和权限而对其进行了更改。
                    cp默认情况下会覆盖。该-f选项尝试rm在尝试重新复制之前删除文件(如中的),这可以在进程无法打开文件进行写入的情况下提供帮助,尽管有些人希望看到出现错误。我不知道这对OP是否重要,但是-f无论如何,我还是将标记添加到了答案中。
                    这是一个脚本,可将文件从下移动到下/path/to/source/root的相应路径/path/to/destination/root。
当心,未经测试的代码。
export dest='/path/to/destination/root'
cd /path/to/source/root
find . -type d \( -exec sh -c '[ -d "$dest/$0" ]' \; -o \
                  -exec sh -c 'mv "$0" "$dest/$0"' {} \; -prune \) \
    -o -exec sh -c '
        if ! [ -e "$dest/$0" ]; then
          mv -f "$0" "$dest/$0"
        fi
' {} \;
\;之前-o在第一线find指挥,你不应该逃避!的if-它只是!不\!
                    该线程已经存在多年了,但在Google上仍然排名第一,所以我想添加另一种方法。我通常的操作方式是:将subdir内容打包到tarball中,将tarball移到父目录中,然后使用默认的--overwrite行为将其解压缩。这正是您要寻找的。之后,您可以删除您的子目录。
cd xyz
tar -cvzpf tmp.tar.gz *
mv tmp.tar.gz ../tmp.tar.gz
cd ..
tar -xvzpf tmp.tar.gz
rm -rf xyz
rm -f tmp.tar.gz
mv -f吗?