gpt4 book ai didi

bash - 如何删除所有子目录但将这些文件保留在父目录中?

转载 作者:行者123 更新时间:2023-11-29 09:22:56 25 4
gpt4 key购买 nike

我在 AIX 上使用 ksh,我的目录结构如下。

dir/
subdir1/file1
subdir2/file2
subdir3/file3

我想删除所有子目录 subdir1subdir2 等等。我想将文件 file1file2 等保存在 dir 中。换句话说,我想将 subdirs 中的所有文件移动到 dir 中,然后删除所有空的 subdirs。所以最后的结果是:

dir/
file1
file2
file3
....

我应该使用什么命令?

最佳答案

我只使用 find 命令,它可以在任何 shell 中工作:

find ./dir -mindepth 2 -type f -exec mv {} ./dir/ \;

./dir开始,找到./dir一级以下的文件,然后将这些文件移动到./dir >.

要删除空子目录,请使用:

find ./dir -mindepth 1 -type d -empty -delete

警告:如果发生文件名冲突,这将覆盖同名文件。

关于bash - 如何删除所有子目录但将这些文件保留在父目录中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43905955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com