大网数据广告活动
服务器运维问答

Delete all folder not containing filename matching glob/regex

· 52CE 我爱测

I want to do the following. I have depth 2 file hierarchy like this: A | B | | g | | h50000 | C Where A, B, C are folders. I want to delete all folders without files matching specific patters, (in my case 50000 is pattern, glob s…

问题原文

原帖链接 · nmiculinic · CC BY-SA 3.0

I want to do the following. I have depth 2 file hierarchy like this:

A
| B
| | g
| | h50000
| C

Where A, B, C are folders. I want to delete all folders without files matching specific patters, (in my case 50000 is pattern, glob style) on depth level 2. (In this example folder C should only be deleted, alongside its content, while A remains)

In my application I'm fine if I delete all folders where neither children contains filename matching a pattern. (It would make it easier to not delete A without specifying on which depth I'm deleting).

How would I be able to do it in bash on Linux machine?

已采纳解答

原帖链接 · FelixJN · CC BY-SA 3.0

With find you could exclude a pattern:

find \! -name '*50000*' -delete

Escaping ! is necessary due to its usage in shells.

Directories with contents will not be deleted.

WARNING: since from your question it is a bit unclear to me - this WILL delete other files in folders that have a 50000 pattern, e.g. A/B/g will be removed and only files (and dirs) with name 50000 (including their parent direcories) be kept!


For deleting all directories that have no 50000 file in them and keep directories with such files along with the other contents of this directory, I'd suggest a two-step method:

  1. list all files and directories and safe into text file

    find . -depth -mindepth 1 > all
    
  2. list directories that need to be kept (find file and print dir only)

    find . -depth -name '*50000*' printf '%h\n' > keep
    
  3. pick the deletable files and directories with an inverted grep

    grep -vf keep all > deletable
    
  4. use this list for deletion (just a sample)

    while read line 
    do
    
       find . -wholename "$line" -delete
    
    done < deletable
    

Note that point 4 is slow due to the nature of being a line-per-line shell script. Not the nicest, but will do the job.


Alternatively (and simpler): If you have root access intermedially change the i-attribute, preventing changes, including deletion, delete everything (as deletion is not allowed for i-flagged files and dirs), and remove the i flag in the end.

#%h goes for parent directories of our hits
find -name '*5000*' -printf '%h\0' | xargs -0 chattr -R +i '{}'
#be careful now ....
rm -r *
chattr -i -R *

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-03-01)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。