Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在linux中删除除上面提到的几个文件夹之外的所有文件夹_Linux_Unix_Rm - Fatal编程技术网

如何在linux中删除除上面提到的几个文件夹之外的所有文件夹

如何在linux中删除除上面提到的几个文件夹之外的所有文件夹,linux,unix,rm,Linux,Unix,Rm,我的wp contentfolder=> 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

我的
wp content
folder=>

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-名称a-exec rm-rf{}\然后它将只适用于1个文件夹名称,我已经输入了每个随机文件夹名称,这是疯狂的,因为它几乎有20000多个临时文件夹

所以,我希望如果有人能帮我用一个命令,我可以删除其中的所有文件夹和内容,除了
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

嗨,我有个问题。如果我想排除更多文件夹,比如ai想排除以下文件夹及其所有内容:
2016/2015/、2014/、nginx-helper/、wpseo-redirects/、gravity\u-forms/
我如何编辑上述命令以满足我的需要?如我所见,您仅排除以
20
开头的文件夹。感谢您的帮助。只需向grep添加更多正则表达式,第一个20\d{2}匹配2000-2099年的所有年份1世纪
find-类型d | grep-Pv'(20\d{2}| nginx | wpseo)| tail-n+2