Sitemap
Szando

Szando

Custom Commands on Linux Desktop

4 min readJun 30, 2024

--

Up until recently, I was using the command line to convert and resize the images I copied from my phone to my Linux Mint box.

I used a couple of very simple one-liners for this task:

find . -maxdepth 1 -type f -name "*.jpg" -exec sh -c 'convert "$0" -resize 30% -quality 92 "${0%.*}.png"' {} \; 
find . -maxdepth 1 -type f -name "*.png" -exec sh -c 'cwebp -q 60 "$0" -o "${0%.*}.webp"' {} \;

The issue was that I always had to create a directory structure with the right files in the right places to make sure that I didn’t reprocess the same files over and over again, and that I was working on the files I actually wanted to work on.

Instead, I wanted to be able to just open a file manager, select the files I wanted to convert, then right-click, and run my commands on the selected files.

I’m using the Nemo file manager on my Cinnamon desktop, so I had an easy solution to make this process a bit more user friendly and less error-prone.

Write the script

Nemo is happy to run the users’ scripts. All I had to do was:

  • Navigate to ~/.local/share/nemo/scripts/ (You can just create this directory if it doesn’t exist.)
  • Create a new script file, e.g., process_images.sh.

Using find was not an option anymore, because I wanted to be able to manually select the input files and pass the list to the commands for processing. Nemo forwards this list to the script as positional arguments, so I had to loop through the $@ special parameter to process the input files one-by-one.

Next, I just had to take the commands I used in the exec clause(?) of the original commands and put them in the input file loop.

The script I ended up with was quite simple:

#!/bin/bash

# Loop through each selected file
for selected_file in "$@"
do
# Get the full path and file extension
full_path=$(realpath "$selected_file")
file_ext="${full_path##*.}"

if [[ "$file_ext" == "jpg" ]]; then
# Convert .jpg to .png and resize
png_path="${full_path%.*}.png"
convert "$full_path" -resize 30% -quality 92 "$png_path"

# Convert .png to .webp
webp_path="${png_path%.*}.webp"
cwebp -q 60 "$png_path" -o "$webp_path"
fi
done

This version of the script does not contain any error handling and it’s not particularly well-written, but it should do the trick for personal use.

Add the script to Nemo’s (context) menu

Once you create a script file in Nemo’s script directory, it will be picked up and made available in the File > Scripts menu. All I had left to do was make sure that the script was executable:

chmod +x ~/.local/share/nemo/scripts/process_images.sh

(Of course you could tweak the file permissions to your needs, if you want to limit who can run the script.)

Get Szando’s stories in your inbox

Join Medium for free to get updates from this writer.

I didn’t even have to close and reopen my Nemo windows, the script just appeared in the menu as soon as I made it executable.

If your Nemo settings are not the default ones, the Scripts menu item might not appear in the context (right-click) menus. Make sure that you enable Scripts as a visible entry in the context menus.

  1. Edit > ⚙️ Preferences
  2. Select Context Menus from the left-hand pane
  3. Tick ✅ the checkbox next to Scripts in the Selection list

Make it pretty

It can be a bit off-putting to see a .sh file name in the Nemo menus, so to make it look prettier, you can use a link to the script file with a more user-friendly name.

  1. Move the script file to a different directory. (For example, you could create a new directory in your user’s home as a designated Nemo script collection)
  2. Create a symbolic link to the script file in the actual Nemo scripts directory, but with a more readable name.
# Create the new scripts directoy
mkdir ~/.my_nemo_scripts
# Move your script to the new directory
mv process_images.sh ~/.my_nemo_scripts/
# Create the symlink
ln -s ~/.my_nemo_scripts/process_images.sh ~/.local/share/nemo/scripts/'Convert and Optimize Selected Images'

Now the script should show up in the Nemo menus with the name of the symbolic link:

That’s it. You can run your scripts directly from Nemo.

Note: all of these instructions work with Nautilus a.k.a. Gnome Files, but you might need to replace nemo in the file paths with nautilus

--

--