Announcement

Collapse
No announcement yet.

Howto: Add diskusage (du) and md5sum to dolphin ServiceMenus

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Howto: Add diskusage (du) and md5sum to dolphin ServiceMenus

    Probably two things I do most that require me to drop into a shell are checking a files md5sum:

    md5sum Downloads/isofiles/VBoxGuestAdditions-4.0.2.iso
    b87a42646972054481bcc2541dc04a09 Downloads/isofiles/VBoxGuestAdditions-4.0.2.isofiles

    and running du -sh on a directory to see how big it is:
    $ du -sh MyDocuments
    3.6G MyDocuments

    I always thought it would be nice to be able to select a file and right-click and select md5sum, or select a directory and right-click and select Disk Usage. Well it turns out that it's not difficult at all to make this happen in Kubuntu. It requires only 3 things:

    1. DEPENDANCIES: All you need is kdialog which if you are using kubuntu it is part of kde-baseapps-bin and is most likely installed already. You would also need md5sum and du which are both in coreutils and are most likely installed.

    2. SCRIPTS: You need scripts to do the md5 and du calculations and output the results in kdialog. I wrote a couple simple scripts I'll include, but feel free to create your own more elaborate scripts.

    3. SERVICEMENUS: To put the Actions: md5sum and diskusage in dolphin you need to create the ServiceMenus files and put them in /usr/share/kde4/services/ServiceMenus. I created md5_generate.desktop and du_generate.desktop which should do the job.

    So here are the scripts:
    $HOME/bin/diskusage
    Code:
    #!/bin/bash
    # $HOME/bin/diskusage
    # this script can be called from a servicemenu file or from the command prompt
    # Usage: diskusage [directory1] [directory2] ...
    display_message() {
      kdialog --title "$title" --msgbox "$message" &
    }
    ERROR="0"
    while [[ "$1" ]];do
     DIR="$1"
     shift
     DIRNAME="$(basename ${DIR})"
     # Check disk usage of file or directory
     if !(test -a "${DIR}" );then
    	((ERROR++))
    	title="Error directory does not exist!"
        message="    Directory \`${DIR}' does not exist!     "
    	display_message
    
     else
    	title="Check Disk Usage of ${DIRNAME}"
    	DU=( $(du -sh "$DIR") )
    	message="Disk Usage => $DIR: \n ${DU[0]}"
    	display_message
    	#echo "${DU[@]}" #You can enable this line for some console output
     fi
    done
    exit "$ERROR"
    $HOME/bin/md5
    Code:
    #!/bin/bash
    # $HOME/bin/md5
    # this script can be called from a servicemenu file or from the command prompt
    # Usage: md5 [file1] [file2] ...
    display_message() {
      kdialog --title "$title" --msgbox "$message" &
    }
    # Check md5 of file
    ERROR="0"
    while [[ "$1" ]];do
     file="$1"
     shift
     filename="$(basename ${file})"
     if !(test -f "${file}");then
    	((ERROR++))
    	title="Error file does not exist!"
        message="    File \`${file}' does not exist!     "
    	display_message
    
     else
    	title="Check MD5 of ${filename}"
    	MD5=( $(md5sum "$file") )
    	message="MD5 for $file: \n ${MD5[0]}"
    	display_message
    	#echo "${MD5[@]}" #You can enable this line for some console output
     fi
    done
    exit "$ERROR"
    The above scripts can be placed anywhere you choose, just make sure they are executable and put the path in the ServiceMenu file, See Below in ServiceMenus files.
    The following ServiceMenus files must be placed in the KDE4 ServiceMenus directory:
    /usr/share/kde4/services/ServiceMenus

    du_generate.desktop
    Code:
    [Desktop Entry]
    Actions=DisplayDiskUsage;
    Type=Service
    ServiceTypes=KonqPopupMenu/Plugin
    MimeType=all/all;
    
    [Desktop Action DisplayDiskUsage]
    Exec=$HOME/bin/diskusage %F
    Name=Display disk usage...
    Icon=cdrom
    X-Ubuntu-Gettext-Domain=desktop_extragear-multimedia_k3b

    md5_generate.desktop
    Code:
    [Desktop Entry]
    Actions=GenerateMD5SUM;
    Type=Service
    ServiceTypes=KonqPopupMenu/Plugin
    MimeType=all/all;
    
    [Desktop Action GenerateMD5SUM]
    Exec=$HOME/bin/md5 %F
    Name=Generate MD5SUM for file...
    Icon=cdrom
    X-Ubuntu-Gettext-Domain=desktop_extragear-multimedia_k3b
    Thats all there is to it. Now when you are in Dolphin and you decide to check the md5sum of a file or the disk usage of a directory all you have to do is select the files or directories and right-click, Actions and selsct "Display disk usage..." or "Generate MD5SUM for file..."

    I hope maybe this is something you can use. If not then maybe you can post something you did with ServiceMenus that you think could be useful to others here.

    <edit> Added full path to the scripts.
    You may have noticed I simply took an existing servicemenu file and modified it, that is why the Icon is set to cdrom, you can change that.

    #2
    Re: Howto: Add diskusage (du) and md5sum to dolphin ServiceMenus

    Neat! Good job.

    Comment


      #3
      Re: Howto: Add diskusage (du) and md5sum to dolphin ServiceMenus

      Originally posted by SteveRiley
      Neat! Good job.
      Thanks. I forgot to mention that when you first install the ServiceMenu files it might take the system awhile to recognize the changes. Be patient and eventually it will start working.

      Comment


        #4
        Re: Howto: Add diskusage (du) and md5sum to dolphin ServiceMenus

        +1

        What may not be obvious to those unskilled in programming is that while you named your *.desktop files and indicated where they should be placed, your bash scripts weren't named nor were their locations indicated, except indirectly in the *.desktop files themselves.

        The du script should be called "diskusage" and saved in a bin directory under the user's home account. That directory may not exist so the user may have to create it.

        The md5sum script should be called "md5" and also stored in "$HOME/bin".

        Both with their execute permission set.

        Installations with more than one user, if the admin wants all users to access this capability, should store their scripts in /usr/bin, with world execute permission.
        "A nation that is afraid to let its people judge the truth and falsehood in an open market is a nation that is afraid of its people.”
        – John F. Kennedy, February 26, 1962.

        Comment


          #5
          Re: Howto: Add diskusage (du) and md5sum to dolphin ServiceMenus

          My solution was to create this file:

          Code:
          [Desktop Entry]
          Type=Service
          ServiceTypes=KonqPopupMenu/Plugin
          MimeType=application/x-cd-image;application/x-iso;application/x-mdf;application/x-nrg;application/x-iso9660-image;
          Icon=dialog-ok
          Actions=checkmd5;
          TryExec=md5sum
          
          [Desktop Action checkmd5]
          Name=Read md5 Checksum
          Icon=dialog-ok
          Exec=md5sum %F | zenity --text-info --width 800 --title Checksum
          and put it in /usr/share/kde4/services/ServiceMenus as an executable.

          Not a fancy and only checks iso's, but that's all I needed it for.

          Please Read Me

          Comment

          Working...
          X