简体   繁体   English

如何删除Linux中除少数提到的文件夹以外的所有文件夹

[英]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.

相关问题 除了linux终端中的一种类型之外,删除某种类型的所有文件 - Remove all files of a certain type except for one type in linux terminal Linux / shell-从一个目录中删除除一个目录以外的所有(子)子文件夹 - Linux/shell - Remove all (sub)subfolders from a directory except one 删除linux目录中的所有文件夹和文件,除了一个文件夹和该文件夹中的所有内容 - delete all folders and files within a linux directory except one folder and all contents inside that folder Linux:如何删除文件夹中除具有特定名称的文件之外的所有文件? - Linux: how to remove all the files in a folder except ones with specific names? linux ksh如何删除除最新目录以外的所有目录 - linux ksh how to remove all directories except the latest some directories 如何使用linux删除除两个文件夹之外的文件夹 - How to delete folders except two with linux 在Linux终端中,如何删除目录中除一个或两个以外的所有文件 - In Linux terminal, how to delete all files in a directory except one or two 如何使用 python 将几个文件夹从一个目录复制到 Linux 中的另一个文件夹 - how do I copy over a few folders from one directory into another folder in Linux using python 在Linux中删除所有但很少的文件特定模式 - Remove all but few specific pattern of files in Linux 如何从当前目录内的所有文件夹中删除Linux中具有特定文件名的文件? - How to remove a file with specific filename in Linux from all the folders inside current directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM