Find and delete lines in .desktop files

Discussion of programming related topics

Moderator: crosscourt

Post Reply
User avatar
bin
Posts: 51
Joined: Wed Aug 05, 2020 11:08 am

Find and delete lines in .desktop files

Post by bin » Thu Aug 06, 2020 5:26 pm

.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
Attachments
google-chrome.desktop.zip
(3.78 KiB) Downloaded 11 times

chris
Posts: 40
Joined: Mon Jan 15, 2018 11:37 pm

Re: Find and delete lines in .desktop files

Post by chris » 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

User avatar
bin
Posts: 51
Joined: Wed Aug 05, 2020 11:08 am

Re: Find and delete lines in .desktop files

Post by bin » Fri Aug 07, 2020 6:35 am

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!

chris
Posts: 40
Joined: Mon Jan 15, 2018 11:37 pm

Re: Find and delete lines in .desktop files

Post by chris » Fri Aug 07, 2020 10:50 am

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

User avatar
bin
Posts: 51
Joined: Wed Aug 05, 2020 11:08 am

Re: Find and delete lines in .desktop files

Post by bin » Fri Aug 07, 2020 2:13 pm

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.
Attachments
chrome desktop files.zip
Beffore and after files renamed with txt extension.
(4.44 KiB) Downloaded 14 times

User avatar
dai_trying
Posts: 548
Joined: Sun Jan 14, 2018 7:44 pm
Location: UK
Contact:

Re: Find and delete lines in .desktop files

Post by dai_trying » Fri Aug 07, 2020 4:23 pm

I tried it when I first read this post, and it sure makes editing .desktop files easier! Thank you both :)

chris
Posts: 40
Joined: Mon Jan 15, 2018 11:37 pm

Re: Find and delete lines in .desktop files

Post by chris » Fri Aug 07, 2020 5:09 pm

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.

User avatar
bin
Posts: 51
Joined: Wed Aug 05, 2020 11:08 am

Re: Find and delete lines in .desktop files

Post by bin » Sat Aug 08, 2020 9:56 am

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 :)

Post Reply