用于将文件从子目录移动到父目录的脚本

Mis*_*isa 5 command-line bash scripts

我正在寻找用于将文件从子目录移动到父目录的脚本

我的文件夹结构是这样的。

  • 在一个文件夹 AI 里面有很多文件夹,名为 1,2,3,4,5...
  • 所有这些文件夹都包含另外两个名为 B 和 C 的文件夹。
  • B 和 C 包含我想向上移动一级的文件,然后删除文件夹 B 和 C。

在此先感谢您为我提供此脚本的任何帮助

Rah*_*til 3

您可以使用:

find test/*/* -type d | xargs -n1  sh -c 'echo mv -b ${0}/* "$( dirname ${0})" ";" rm -rvf ${0} '
Run Code Online (Sandbox Code Playgroud)

它只会在屏幕上打印输出,如下所示:

mv -b test/1/B/file1 test/1/B/file2 test/1/B/file3 test/1 ; rm -rvf test/1/B
mv -b test/1/C/file1 test/1/C/file2 test/1/C/file3 test/1 ; rm -rvf test/1/C
mv -b test/2/B/file1 test/2/B/file2 test/2/B/file3 test/2 ; rm -rvf test/2/B
mv -b test/2/C/file1 test/2/C/file2 test/2/C/file3 test/2 ; rm -rvf test/2/C
mv -b test/3/B/file1 test/3/B/file2 test/3/B/file3 test/3 ; rm -rvf test/3/B
mv -b test/3/C/file1 test/3/C/file2 test/3/C/file3 test/3 ; rm -rvf test/3/C
mv -b test/4/B/file1 test/4/B/file2 test/4/B/file3 test/4 ; rm -rvf test/4/B
mv -b test/4/C/file1 test/4/C/file2 test/4/C/file3 test/4 ; rm -rvf test/4/C
mv -b test/5/B/file1 test/5/B/file2 test/5/B/file3 test/5 ; rm -rvf test/5/B
mv -b test/5/C/file1 test/5/C/file2 test/5/C/file3 test/5 ; rm -rvf test/5/C
Run Code Online (Sandbox Code Playgroud)

如果输出看起来正常,那么您可以简单地附加 | sh在该命令的末尾,然后它将运行输出中显示的命令。


Rad*_*anu 1

以下命令将完成这项工作:

cd /path/to/A
find -mindepth 3 -type f -execdir mv {} .. \;
find -mindepth 2 -type d -exec rm -d {} \;
Run Code Online (Sandbox Code Playgroud)

另请参阅和操作man find之间的差异。-exec-execdir