Eri*_*fer 205 command-line shell files
我想移动到父目录的子目录中有大量文件和目录。目标目录中已经有一些文件和目录需要覆盖。只存在于目标中的文件应该保持不变。我可以强迫mv这样做吗?它 ( mv * ..) 抱怨
mv: cannot move `xyz' to `../xyz': Directory not empty
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
dog*_*ane 160
您必须将它们复制到目的地,然后使用cp -r * ..后跟的命令删除源rm -rf *。
我认为您不能使用mv.
Mik*_*kel 86
rsync在这里可能是更好的选择。就这么简单rsync -a subdir/ ./。
我的测试树filename:contents格式:
./file1:root
./file2:root
./dir/file3:dir
./dir/file4:dir
./subdir/dir/file3:subdir
./subdir/file1:subdir
Run Code Online (Sandbox Code Playgroud)
运行rsync:
$ rsync -a -v subdir/ ./
sending incremental file list
./
file1
dir/
dir/file3
Run Code Online (Sandbox Code Playgroud)
给出:
./file1:subdir
./file2:root
./dir/file3:subdir
./dir/file4:dir
./subdir/dir/file3:subdir
./subdir/file1:subdir
Run Code Online (Sandbox Code Playgroud)
然后,为了模拟mv,您可能想要删除源目录:
$ rm -r subdir/
Run Code Online (Sandbox Code Playgroud)
给予:
./file1:subdir
./file2:parent
./dir/file3:subdir
./dir/file4:dir
Run Code Online (Sandbox Code Playgroud)
如果这是错误的,您能否提供一个类似的示例(例如,使用本答案顶部附近的我的测试树)以及所需的结果?
小智 59
rsync 复制后可以删除源 --remove-source-files参数。
从rsync手册页:
--remove-source-files sender removes synchronized files (non-dir)
Run Code Online (Sandbox Code Playgroud)
pal*_*wim 34
您可以使用cp和来做到这一点rm,但无需复制您(大概)试图避免传输的大量数据。@mattdm提到这在他的评论,而另一个问题的答案具有的各种选项更完整的讨论。
cp -rlf source destination
rm -r source
Run Code Online (Sandbox Code Playgroud)
本质上,该命令的-l选项cp会创建文件的硬链接,而不是将其数据复制到新文件。
这是一个将文件从下移动/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
' {} \;
Run Code Online (Sandbox Code Playgroud)