将文件夹中的所有文件移动到另一个文件夹而不移动子文件夹

将文件夹中的所有文件移动到另一个文件夹而不移动子文件夹

我有一个名为 的文件夹folder1。它有一堆文件和两个子文件夹subfolder1subfolder2.我想folder1subfolder2subfolder1.

我怎样才能做到这一点?

答案1

您可以使用findmv

这是我的文件夹设置:

$ find folder1
folder1
folder1/subfolder2
folder1/subfolder2/i
folder1/subfolder2/h
folder1/subfolder2/g
folder1/subfolder1
folder1/subfolder1/f
folder1/subfolder1/e
folder1/subfolder1/d
folder1/c
folder1/b
folder1/a

为了模拟您的案例,我想将abc移至subfolder2

$ find folder1 -maxdepth 1 -type f -exec mv "{}" folder1/subfolder2 \;

现在,如果我看一下以下内容folder1

$ find folder1
folder1
folder1/subfolder2
folder1/subfolder2/a
folder1/subfolder2/b
folder1/subfolder2/c
folder1/subfolder2/i
folder1/subfolder2/h
folder1/subfolder2/g
folder1/subfolder1
folder1/subfolder1/f
folder1/subfolder1/e
folder1/subfolder1/d

答案2

要将 shell中的非目录文件folder1(不包括隐藏文件)移动到, 您可以执行以下操作:folder1/subfolder2zsh

mv folder1/*(^/) folder1/subfolder2/

要移动所有文件,无论其类型如何,除了subfolder1(显然subfolder2也是如此):

set -o extendedglob # best in ~/.zshrc
mv folder1/^(subfolder1|subfolder2) folder1/subfolder2

要同时移动隐藏文件,请添加Dglob 限定符。

答案3

使用findxargs.读man find xargs。由于您没有说明文件名是什么样的(嵌入空格、其他有趣的字符),我将使用-print0.

find folder1 -maxdepth 1 -type f -print0 | \
    xargs -0 -r mv --target-directory=subfolder2

相关内容