Bash命令删除几个嵌套目录



我犯了一个错误,创建了嵌套目录,如:

dir1/dir2_A/out/dir2_A
file1
file2 
out
dir1/dir2_B/out/dir2_B
file1
file2 
out
dir1/dir2_C/out/dir2_C
file1
file2 
out
...

如何删除所有中间目录:

dir1/dir2_A/out/dir2_A/ → dir1/dir2_A/
dir1/dir2_B/out/dir2_B/ → dir1/dir2_B/
dir1/dir2_C/out/dir2_C/ → dir1/dir2_C/

等等。,在dir1中得到这个?

dir1/dir2_A
file1
file2 
out
dir1/dir2_B
file1
file2 
out
dir1/dir2_C
file1
file2 
out

下降到文件,将所有内容向上移动
接下来删除空目录。使用括号作为子shell,这样您将从d1返回而不使用cd ../..

for d1 in dir1/*/*/*; do
test -d "$d1" || continue
(cd "$d1" && mv * ../.. )
done
# Cleanup without `rm -r` avoiding errors when something went wrong
rmdir dir1/*/out/* 
rmdir dir1/*/out

相关内容

  • 没有找到相关文章

最新更新