Post your handy utility scripts!

Yeah for sure I want to be able to do that. :grinning:

hahaha Just have to wait until I learn.

I think ages back I remember distrotube saying something along those lines when he was talking about setting a tiling manager.

That is part of the learning process. I had done similar things in the past before realizing I could make alias’s and scripts to make those things easier to do. We all start out crawling before we start running.

2 Likes

:sweat_smile: for sure lol. Honestly I think it was only the past week that I came to that realisation lol.

And then I thought oh s*** lol Linux is even better than I thought before.

1 Like

I have a backup script, here goes a sample and what it does:
It makes a backup of the files and folders on _array_home for user stuff and on _array_root for root file system.

I set DESTINATION variable to copy where i’m running the script (an external hd)

It also creates a list of all installed packges (official and AUR) to make it easier to install on new system.

Iwas inspired by some youtube video, can’t remember now so I can properly give credit.

Here goes the sample (save as .sh and make it executable, be sure to test it before to avoid problems)

#!/bin/bash

# -a, --archive (includes --links)
# 		-l, --links		copy symlinks as symlinks
#
# -v --verbose 	This  option increases the amount of information you 
# 				are given during the transfer.
# -x, --one-file-system       don't cross filesystem boundaries
#
# -P    The  -P  option  is  equivalent to --partial --progress.  
# 		--partial Keeps the partial file
# 		--progress Shows the progress of the transfer.
#
# -n, 	--dry-run Perform a trial run with no changes made
#
# --delete                delete extraneous files from dest dirs
#
# --exclude=PATTERN       exclude files matching PATTERN
#	rsync -av --exclude={'*.txt','dir3','dir4'} sourcedir/ destinationdir/
# 
# --exclude-from=FILE     read exclude patterns from FILE
# Use the -R or --relative option to preserve the full path

DESTINATION=.
_array_home=(
scripts
archiso
.bash_profile
.bashrc
.thunderbird
.config/autostart
.config/gtk-3.0
.config/i3
.config/menus
.config/sxhkd
.config/terminator
.config/tilix
.config/xfce4

)
_array_root=(

/etc/X11/xorg.conf.d/10-dpms.conf
/etc/X11/xorg.conf.d/20-3rdbutton.conf
/etc/pacman.d/hooks

)

_home(){

for x in ${_array_home[@]}
do
rsync -avxRP /home/$USER/$x $DESTINATION
# to restore
# rsync -avxRP --delete home/ /

done
}

_root(){

for x in ${_array_root[@]}
do
sudo rsync -avxRP $x $DESTINATION

done

}


_make_list(){
pacman -Qq | sort > arch_packages.txt
pacman -Qm | sort | awk '{print $1}' > aur_packages.txt
}

# START HERE
_home
_root
_make_list
2 Likes

lol oh damn I think thats exactly what I wanted lol.

Thank you sir!

so I can just add the paths that I want. lol wicked!

whats the bit about delete?

if you make backups multiple times, and let’s say you don’t want a file (from source) and delete it, next time you use rsync it will also delete the file (at destination), considering it existed before.

I’m not using it on my script, but you can if you want.

In my case I clean my system from time to time, but never delete old stuff on destination, it works like a time capsule

1 Like

Does Bashrc also count? Wanting to have a colorful name I ended up making this to see the exit status. I’m just starting out and I don’t know if it’s silly, I just felt like sharing it:

bashrc_exit_status() {
	local status=$?
	if [ $status -ne 0 ]; then
		echo "Exit Status: $status"
	fi
}
PROMPT_COMMAND='bashrc_exit_status'

you may find this handy if you take a lot of photo’s with your android device.

function get_DEVICE_STATUS
{

#simple function to determine if the device is “Online which will show as device”
#possible values of current_state: offline bootloader device unauthorized null
DEVICE_STATUS=adb get-state | tr -d '\r\n\t /\\\' | sed '/^$/d'

if [[ ${DEVICE_STATUS} != 'device' ]] ; then
	echo "No Devices Found"
    err_Exit=1
    Script_Exit
fi

}
####################################################################
#Execute #
####################################################################
get_DEVICE_STATUS

adb pull /storage/emulated/0/DCIM/Camera $HOME/tmp/pulled
thunar $HOME/tmp/pulled &

Enjoy

Found this for anyone who has a need to reduce the size of pdf files

#!/bin/sh
 
# http://www.alfredklomp.com/programming/shrinkpdf
# Licensed under the 3-clause BSD license:
#
# Copyright (c) 2014-2019, Alfred Klomp
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
 
#
# Modified by Vivek Gite to suit my needs
#
shrink ()
{
	gs					\
	  -q -dNOPAUSE -dBATCH -dSAFER		\
	  -sDEVICE=pdfwrite			\
	  -dCompatibilityLevel=1.3		\
	  -dPDFSETTINGS=/screen			\
	  -dEmbedAllFonts=true			\
	  -dSubsetFonts=true			\
	  -dAutoRotatePages=/None		\
	  -dColorImageDownsampleType=/Bicubic	\
	  -dColorImageResolution=$3		\
	  -dGrayImageDownsampleType=/Bicubic	\
	  -dGrayImageResolution=$3		\
	  -dMonoImageDownsampleType=/Subsample	\
	  -dMonoImageResolution=$3		\
	  -sOutputFile="$2"			\
	  "$1"
}
 
check_smaller ()
{
	# If $1 and $2 are regular files, we can compare file sizes to
	# see if we succeeded in shrinking. If not, we copy $1 over $2:
	if [ ! -f "$1" -o ! -f "$2" ]; then
		return 0;
	fi
	ISIZE="$(echo $(wc -c "$1") | cut -f1 -d\ )"
	OSIZE="$(echo $(wc -c "$2") | cut -f1 -d\ )"
	if [ "$ISIZE" -lt "$OSIZE" ]; then
		echo "Input smaller than output, doing straight copy" >&2
		cp "$1" "$2"
	fi
}
 
usage ()
{
	echo "Reduces PDF filesize by lossy recompressing with Ghostscript."
	echo "Not guaranteed to succeed, but usually works."
	echo "  Usage: $1 infile [outfile] [resolution_in_dpi]"
}
 
IFILE="$1"
 
# Need an input file:
if [ -z "$IFILE" ]; then
	usage "$0"
	exit 1
fi
 
# Output filename defaults to "-" (stdout) unless given:
if [ ! -z "$2" ]; then
	OFILE="$2"
else
	OFILE="-"
fi
 
# Output resolution defaults to 72 unless given:
if [ ! -z "$3" ]; then
	res="$3"
else
	res="90"
fi
 
shrink "$IFILE" "$OFILE" "$res" || exit $?
 
check_smaller "$IFILE" "$OFILE"

https://www.cyberciti.biz/faq/linux-shell-script-to-reduce-pdf-file-size/

4 Likes

Maybe someone may like this. After ytfzf stopped working for me, I wrote a small script that uses (and needs) yt-dlp, fzf and mpv to search and play YouTube videos. The functions are made for fish shell. You can simply put them into your config.fish file.

If you use bash or zsh, it shouldn’t be too hard rewriting the functions. I can only recommend running fish as an interactive shell inside your terminal. It’s so god damn good out of the box.

For searching in general:
ys endeavourOS test

For searching latest videos of a channel:
yc CHANNELNAME

For using a pre-defined channel list (defined inside function):
yl

Here’s the code (edited once for new version):

# Function to select a YouTube video from a list.
# Requires fzf and yt-dlp to be installed.
# Put this code into config.fish or into ~/.config/fish/conf.d/ytfzf.fish.
# select_yt is not meant to be used. Use ys, yc, yl for searching and playing videos.

function select_yt
    set temp_file "/var/tmp/ytfzf_list.txt"
    set video_titles
    set video_urls
    set line_index 0

    if not test -e $temp_file
        echo "Error: $temp_file does not exist."
    end

    if not command -v fzf >/dev/null
        echo "Error: fzf is not installed."
        return 1
    end

    if not command -v mpv >/dev/null
        echo "Error: mpv is not installed."
        return 1
    end

    # Parse the file into titles and URLs
    for line in (cat $temp_file)
        if test (math $line_index % 2) -eq 0
            set video_titles $video_titles $line
        else
            set video_urls $video_urls $line
        end
        set line_index (math $line_index + 1)
    end

    # Use fzf to select a title.
    set selected_title (printf "%s\n" $video_titles | command fzf)

    if test $status -ne 0
        echo "Aborted fzf selection..."
        return 1
    end

    # Find the index of the selected title.
    set line_index 0
    for title in $video_titles
        if test "$title" = "$selected_title"
            set selected_index (math $line_index + 1)
        end
        set line_index (math $line_index + 1)
    end

    echo "Starting the selected video in mpv! Might take a second..."
    command mpv "$video_urls[$selected_index]" </dev/null &>/dev/null &
    disown
end

# Function to fetch the ten latest videos from a YouTube channel.
# Usage example: yc DeStaatOfficial
function yc
    set temp_file "/var/tmp/ytfzf_list.txt"

    if test -z "$argv"
        echo "Error: No channel string provided."
        return 1
    end

    if not command -v yt-dlp >/dev/null
        echo "Error: yt-dlp is not installed."
        return 1
    end

    if test -e $temp_file
        command rm $temp_file >/dev/null # Delete temp_file, because --print-to-file doesn't overwrite the temp_file, but appends to it.
    end
    command yt-dlp --quiet -I1:10 --flat-playlist --lazy-playlist --print-to-file title,webpage_url $temp_file "https://www.youtube.com/@$argv/videos" 2>/dev/null

    if test $status -ne 0
        echo "Something went wrong with yt-dlp..."
        return 1
    end

    select_yt
end

# Function to search YouTube videos. Returns ten search results.
# Usage example: ys De Staat Peptalk
function ys
    set temp_file "/var/tmp/ytfzf_list.txt"

    if test -z "$argv"
        echo "Error: No search string provided."
        return 1
    end

    if not command -v yt-dlp >/dev/null
        echo "Error: yt-dlp is not installed."
        return 1
    end

    command yt-dlp ytsearch10:"$argv" --flat-playlist --print title,webpage_url >$temp_file

    if test $status -ne 0
        echo "Something went wrong with yt-dlp..."
        return 1
    end

    select_yt
end

# Function to list and select from predefined channels.
# Add channels to the predefined_channels list.
# Usage: yl
function yl
    set predefined_channels CHANNEL1 CHANNEL2

    if not command -v fzf >/dev/null
        echo "Error: fzf is not installed."
        return 1
    end

    set selected_channel (printf "%s\n" $predefined_channels | command fzf)

    if test $status -ne 0
        echo "Aborted fzf selection..."
        return 1
    end

    yc $selected_channel
end
4 Likes