如何删除 linux 中提到的几个文件夹以外的所有文件夹

How to remove all folders except a few mentioned one in linux

我的 wp-content 文件夹中有以下文件夹结构 =>

2016/ => it has many subfolders in it and I wanna keep them
2015/ => it has many subfolders in it and I wanna keep them
2014/ => it has many subfolders in it and I wanna keep them
2013/ => it has many subfolders in it and I wanna keep them

besides those folder there are tons of temp folders which I want to delete along with anything inside it. The folder name look like this:

ZhsvhgTjh/
Vgfsugu79/
1agDjgdki/
8gdygREfh/
Hbjddsyug/
....so on....

现在的问题是,如果我 运行 rm -f 那么它将删除该文件夹中的所有内容,包括像 2016, 2015, 2014, 2013.

这样的文件夹

此外,如果我尝试以下操作:find . -name a -exec rm -rf {} \; 那么它将仅适用于 1 个文件夹名称,并且我输入了每个随机文件夹名称,这太疯狂了,因为它几乎有 20,000 多个临时文件夹。

所以,我希望是否有人可以通过命令帮助我删除其中除 2016, 2015, 2014, 2013 文件夹及其内容之外的所有文件夹和内容。

还有一个删除命令,任何人都可以告诉我是否有办法 运行 一个计数命令来查看查询是否选择了正确数量的文件夹?我不想不小心删除任何重要的东西。

谢谢。

打印树

prodrive11@raccoon:~/tmpp/wp-content$ ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'
   .
   |-2015
   |---keep2
   |---keep3
   |-2016
   |---keep1
   |---keep2
   |-NKAXOIND
   |-x232sfsw
   |---we233ds

显示垃圾文件夹:

   prodrive11@raccoon:~/tmpp/wp-content$ find . -type d | grep -Pv '20\d{2}' | tail -n +2                   ./x232sfsw
    ./x232sfsw/we233ds
    ./NKAXOIND

数一数

prodrive11@raccoon:~/tmpp/wp-content$ find . -type d | grep -Pv '20\d{2}' | tail -n +2 | wc -l
3

删除它们

prodrive11@raccoon:~/tmpp/wp-content$ find . -type d | grep -Pv '20\d{2}' | tail -n +2 | xargs rm -rf

再次显示树

prodrive11@raccoon:~/tmpp/wp-content$ ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'
   .
   |-2015
   |---keep2
   |---keep3
   |-2016
   |---keep1
   |---keep2