Page 1 of 1

Find and delete lines in .desktop files

Posted: Thu Aug 06, 2020 5:26 pm
by bin
.desktop files contain loads and loads of lines that are not needed.

e.g:-
GenericName[es]=Navegador web
Comment[ca]=Accedeix a Internet
Name[fr]=Nouvelle fenêtre

ad nauseam - take a look at the google-chrome.desktop attached and you'll see what I mean.

I don't need any of these language entries and in the past I used to manually clean up those that I used the most to improve execution times. That was back in the days of a 486 with 500mb RAM and an 80gb hard drive!!!!

Now, I have tried using various combinations of sed to find and remove these but I keep running into the same problem and that is the [ ]s which are seen as regex entries. Obviously what I want to do is find any line that contains Name[anything]= or Comment[anything]= and so on and delete the line.
This would leave lines like
Comment=Access the Internet intact - which is what I want.

I have searched for finding lines with variables or wildcards but keep stumbling on the square brackets. Anyone got any ideas please? I have tried various find utilities but this needs to be programmatic to handle the number of files involved.

Thanx - bin

Re: Find and delete lines in .desktop files

Posted: Thu Aug 06, 2020 8:41 pm
by chris

Code: Select all

grep -v '.*\[.*\]=' textmaker-free18.desktop
[/size]

will print ONLY lines NOT containing:
Something[something]= from the file textmaker-free18.desktop
Note the \ escaping [ and ], and .* for any-length string

Re: Find and delete lines in .desktop files

Posted: Fri Aug 07, 2020 6:35 am
by bin
chris wrote: Thu Aug 06, 2020 8:41 pm

Code: Select all

grep -v '.*\[.*\]=' textmaker-free18.desktop
[/size]

will print ONLY lines NOT containing:
Something[something]= from the file textmaker-free18.desktop
Note the \ escaping [ and ], and .* for any-length string
Excellent - find what you're not looking for! No wonder I struggle with this stuff! :lol: :lol:

From there it's just a quick leap to

Code: Select all

sed -i /'.*\[.*\]=/d' sol.desktop
Job done - now for the brave bit

Code: Select all

sudo sed -i /'.*\[.*\]=/d' *.desktop
:o

Thanks very much chris!

Re: Find and delete lines in .desktop files

Posted: Fri Aug 07, 2020 10:50 am
by chris
A safer way to do what you want:

Code: Select all

for i in *.desktop; do cp "$i" "$i.backup"; grep -v '.*\[.*\]=' "$i.backup" > "$i"; done
And then restart to make sure your "new" .desktop files are working. Only then delete the .backup files

Re: Find and delete lines in .desktop files

Posted: Fri Aug 07, 2020 2:13 pm
by bin
Good thinking. :)

I was puzzled by the use of grep but a bit more digging with a relevant example makes it a lot clearer. Thanks again for assisting my little grey cell!

If anyone is reading this and wondering what I'm on about - please see the before and after attached.

221 lines down to 25 lines to be read each time it is used.

Re: Find and delete lines in .desktop files

Posted: Fri Aug 07, 2020 4:23 pm
by dai_trying
I tried it when I first read this post, and it sure makes editing .desktop files easier! Thank you both :)

Re: Find and delete lines in .desktop files

Posted: Fri Aug 07, 2020 5:09 pm
by chris
As an aside/addition if you change the grep bit to:

Code: Select all

grep -v -E '.*\[.*\]=|^$' "filename"
you get rid of blank lines too.
Note capital E and vertical bar separator between regex bits.

Re: Find and delete lines in .desktop files

Posted: Sat Aug 08, 2020 9:56 am
by bin
Thanks for that chris - a useful extra tweak.

Well, I ran the cleanup and certainly things do feel a bit snappier - especially Konqueror. It may be a placebo effect of course!! But, it still results in a much cleaner system and that I like.

I did try to find a way to make the script recursive, but couldn't get ant sort of -r switch to work. So, I did it old school by finding the folders with .desktop files, created a script tp run on each folder *.desktop and that worked OK. I excluded my /home so that's next :)