gpt4 book ai didi

linux - 从目录子树的所有级别按扩展名删除文件而不破坏目录结构

转载 作者:太空狗 更新时间:2023-10-29 11:37:34 26 4
gpt4 key购买 nike

使用 shell 脚本,我只想从以下目录结构中删除文本文件、日志文件和 CSV 文件,同时保持目录结构不变:

|
|------bar/
|
|---file1.txt
|---file2.txt
|
|---subdir1/
| |---file1.log
| |---file2.log
|
|---subdir2/
|---image1.log
|---image2.log

我正在使用 rm -rf/bar/*,所以我得到以下结果:

|------bar/

但是,我想要以下结果:

|
|------bar/
|
|
|
|
|---subdir1/
|
|
|
|---subdir2/

最佳答案

在 UNIX 上(或在使用 cygwin 的 Windows 上):

守旧派:

find bar -type f -name "*.txt" -o -name "*.log" -o -name "*.csv" -print0 |
xargs -0 rm -f

使用 GNU 查找(由 mklement0 建议):

find bar -type f -name "*.txt" -o -name "*.log" -o -name "*.csv" -delete

关于linux - 从目录子树的所有级别按扩展名删除文件而不破坏目录结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24175830/

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