[SOLVED] How to delete all directories but two which contain certain string?

rado84

Well-Known Member
Joined
Feb 25, 2019
Messages
757
Reaction score
623
Credits
4,705
There are some directories in the Firefox profile which take space and which I have found to remove manually via a command:
Code:
rm -rfv ~/.mozilla/firefox/tjdaag57.default-release/storage/default/https* && rm -rfv ~/.mozilla/firefox/tjdaag57.default-release/storage/default/http*
These above are a part of a huge alias but only these two are relevant, that's why I'm posting only them.
However, there are two directories which I'd like to keep. I tried setting their rights to be undeletable, but with these undeletable directories the terminal refuses to complete the entire alias. I had found somewhere on the internet a way to "delete FILES except these" but it didn't work for directories (not even with a / after the names to indicate they're directories and on top of that that screwed up the whole alias and the terminal was skipping it to execute the next alias in the ~/.bashrc file.
So, I need a way to make the terminal omit the directories with these names (I've put then in a code tag, so that they don't get parsed as URL addresses):
Code:
https+++duckduckgo.com
&&
https+++twitter.com
 


Not very elegant maybe, but could you copy the two directories to a temporary location with your alias code, and then move them back into place later with your alias code after the major deletion has completed?
 
If I have to do that every time I mack a FF profile backup, it will get annoying quickly. I'm trying to find an alternative in case this angle doesn't work - to change the location of where these two directories are being created (the dom storage of Firefox).
 
You could probably do something like this.
find ~/.mozilla/firefox/tjdaag57.default-release/storage/default -type d -name http* | egrep -v "duckduckgo.com|twitter.com" | xargs rm -rf
That would find all the directories in the path ~/.mozilla/firefox/tjdaag57.default-release/storage/default with "http" in the name and then exclude anything with "duckduckgo.com and twitter.com".

Running it without "xargs rm -rf" first so that you can see the output that gets deleted first so that you don't delete something you don't want to delete.
find ~/.mozilla/firefox/tjdaag57.default-release/storage/default -type d -name http* | egrep -v "duckduckgo.com|twitter.com"
 
@f33dm3bits , just one question though: what does the | do? I get the most of the synthax, just not this one. Based on your description, I'd say it works as an "and" inside the command (as opposed to && which starts a new command), but I could be wrong.
 
The | is a pipe, in the case of using egrep it is used for extended regular expression, so if you have other things you want to exclude you could add multiple pipes when using egrep. For example like this.
Code:
egrep "pattern1|pattern2|pattern3|patter4"
 

Staff online

Members online


Top