this post was submitted on 18 Feb 2025
12 points (92.9% liked)

Linux

50337 readers
937 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 5 years ago
MODERATORS
 

Hi all. Today I was messing around with making custom icons in Debian 12 and I was having a heck of a time. I finally figured it out and wanted to share my solution. Below is a .sh script that will automate creating and replacing existing icons.

How it works

The script takes a path to an .svg file as an input argument. It then searches the /usr/share/icons/hicolor folder's subdirectories for .pngs of a matching name, notes their resolutions, and utilizes InkScape to convert the .svg to .pngs and replace the icons.

To utilize, save the script below as an .sh file and provide it an input .svg as follows:

sudo ./icons.sh /home/USERNAME/icon.svg

(note: your input .svg file must match the name of the existing icon you are trying to replace. Check the folder path below to determine the correct name)

Script

#!/bin/bash

# Define the base directory where icons are located
BASE_DIR="/usr/share/icons/hicolor"

# Ensure the script is run as root to modify files in /usr/share/icons/hicolor
if [ "$(id -u)" -ne 0 ]; then
    echo "This script must be run with root privileges to access the icons folder."
    exit 1
fi

# Check if Inkscape is installed
if ! command -v inkscape &> /dev/null; then
    echo "Inkscape is not installed. Please install it and try again. (sudo apt install inkscape)"
    exit 1
fi

# Input SVG file filepath
INPUT_SVG="$1"
if [ -z "$INPUT_SVG" ]; then
    echo "Usage: $0 /path/to/input.svg"
    exit 1
fi

# Validate that the input SVG file exists
if [ ! -f "$INPUT_SVG" ]; then
    echo "Input SVG file does not exist."
    exit 1
fi

# Loop through all resolution folders (resolutions like 16x16, 32x32, etc.) in the /usr/share/icons/hicolor folder
for resolution_dir in "$BASE_DIR"/*x*; do
    # Check if the resolution folder contains an 'apps' subfolder
    if [ -d "$resolution_dir/apps" ]; then
        echo "Found apps folder in $resolution_dir"

        # Extract the resolution size (e.g., 16x16)
        resolution=$(basename "$resolution_dir")

        # Get the name of the input SVG file (without the .svg extension)
        base_name=$(basename "$INPUT_SVG" .svg)

        # Define the target PNG file path in the current resolution folder
        target_png="$resolution_dir/apps/$base_name.png"

        # Check if the resolution folder already contains a PNG file to replace
        if [ -f "$target_png" ]; then
            echo "Found existing $target_png. Replacing it."

            # Use Inkscape to convert the SVG to PNG at the correct resolution
            inkscape "$INPUT_SVG" --export-type=png --export-filename="$target_png" --export-width="${resolution%x*}" --export-height="${resolution#*x}"

            # Confirm creation
            if [ -f "$target_png" ]; then
                echo "Successfully created $target_png"
            else
                echo "Failed to create $target_png"
            fi
        else
            echo "No existing $target_png found. Skipping this resolution."
        fi
    else
        echo "No 'apps' folder found in $resolution_dir. Skipping."
    fi
done

echo "Icon update process complete!"
top 5 comments
sorted by: hot top controversial new old
[–] SpicySquid@lemmy.ml 8 points 2 days ago (1 children)

The sudo makes this a bit suspicious to me. Maybe you can store the results in ~/.local/share/icons/hicolor (not sure if that's exactly the correct path. It would allow you to run this for a specific user instead of doing things with your entire system.

[–] towelie@lemm.ee 5 points 2 days ago* (last edited 2 days ago) (3 children)

I'm still in my first month of using Linux, so apologies for naive questions. My local icons folder is empty, hence using the global folder; do you know why my icons are being saved system wide and not in the local icons folder? I'll definitely modify the script to use the local folder if I can save some applications there.

[–] SpicySquid@lemmy.ml 4 points 2 days ago

No worries. You have several places where you can store icons, themes, etc. The Book has some good information on this. You might not be running ArchLinux, but these paths will apply to you as well. You can basically overlap system-wide icons with user-specific icons in your home directory.

To easily get the icons from the system directory into your home directory you can just copy them and make the alterations in your home directory.

Also, like the others here also said: great work on getting so far in such a short amount of time!

[–] friendly_ghost@beehaw.org 3 points 2 days ago

One month in and you're writing complex bash scripts!! Keep up the good work! 🎉

[–] notanapple@lemm.ee 4 points 2 days ago

Distros ship with icons and themes system wide because apps running as root only load icons/themes that are installed system wide (you can check this by running an app as sudo).