[英]How to remove all folders except a few mentioned one in linux
I have the following folder structure inside my wp-content
folder => 我的
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....
Now the problem is if I run rm -f
then it is gonna remove everything inside that folder, including the folders like 2016, 2015, 2014, 2013
. 现在的问题是,如果我跑
rm -f
那么它会在该文件夹内取出的一切,包括像文件夹2016, 2015, 2014, 2013
。
Also if I try the following: find . -name a -exec rm -rf {} \\;
另外,如果我尝试以下操作:
find . -name a -exec rm -rf {} \\;
find . -name a -exec rm -rf {} \\;
then it will only work for 1 folder name and I have type each random folder name which is insane as it has almmost 20,000+ temp folders. 那么它将仅适用于1个文件夹名称,而我输入的每个随机文件夹名称都是疯狂的,因为它几乎有20,000个以上的临时文件夹。
So, I was hoping if anyone can help me with a command using which I can remove all the folders and contents within it except the 2016, 2015, 2014, 2013
folders and their contents. 所以,我希望如果有人能帮助我使用,我可以删除其中的所有文件夹和内容除命令
2016, 2015, 2014, 2013
文件夹及其内容。
Also as this a remove command can anyone let me know if there is a way I can run a count command to see if the query is selecting the right number of folders or not? 另外,由于这个remove命令,任何人都可以让我知道是否有一种方法可以运行count命令,以查看查询是否选择了正确数量的文件夹? I don't wanna delete any important stuffs accidentally.
我不想意外删除任何重要内容。
Thank you. 谢谢。
Print tree 打印树
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
Show garbage folders: 显示垃圾文件夹:
prodrive11@raccoon:~/tmpp/wp-content$ find . -type d | grep -Pv '20\d{2}' | tail -n +2 ./x232sfsw
./x232sfsw/we233ds
./NKAXOIND
Count them 数他们
prodrive11@raccoon:~/tmpp/wp-content$ find . -type d | grep -Pv '20\d{2}' | tail -n +2 | wc -l
3
Remove them 删除它们
prodrive11@raccoon:~/tmpp/wp-content$ find . -type d | grep -Pv '20\d{2}' | tail -n +2 | xargs rm -rf
Show tree again 再次显示树
prodrive11@raccoon:~/tmpp/wp-content$ ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'
.
|-2015
|---keep2
|---keep3
|-2016
|---keep1
|---keep2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.