Post your handy utility scripts!

Ah that’s useful to know, thanks!

3 Likes

I was working on my .bashrc and reading about its posibilities. So, I thought I should make something totaly useless. :laughing:
Put this code to your .bashrc and press F12 in your termianl window (works in tty too).

bind '"\C-k":kill-whole-line'           # clear current input line (Ctrl+k default is 'kill-line')
#F12 cowsay info
alias cowinfo='echo -ne "Who am I?\t$(whoami)\nWhere am I?\t$(pwd)\nWhen am I?\t$(date "+%T %d.%m.%Y")\nWhat'\''s this?\t$(uname -n)\nWhat'\''s on it?\t$(uname -sr)\n\n$(fortune)" | cowsay -n'
bind '"\e[24~":"\C-kcowinfo\n"'

You need cowsay and fortune-mod packages for it to work.

Result on my Ubuntu machine.
 ________________________________________
/ Who am I?       vlkon                  \
| Where am I?     /home/vlkon            |
| When am I?      23:08:12 13.09.2020    |
| What's this?    portable               |
| What's on it?   Linux 5.4.0-47-generic |
|                                        |
\ The bug starts here.                   /
 ----------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

As a sidenote this might be a handy way how to map commonly used commands to a key without DE. It is basicaly the same as listing it in inputrc file.

2 Likes

1

5 Likes

With a little modification it will make neofetch obsolete. :rofl:

1 Like

I was reinventing .desktop files in i3wm.
It lists predefined entries in dmenu based on existence of a specific file.
It would be much better to wrap each application to a package but since I use this mostly with wine appliations (games) I just copy whole folder with .exe launcher to wine/c_drive directory and it is ready - no stray files anywhere.

I have this in my .local/bin.

games
#!/bin/bash

# data array has to be in a format
# "name:path:exec_prefix:exec:exec_suffix"
# colons are important
# exec is launcher file that has to exist

###################################################
# Entry list
###################################################

entry=(

#"name:\
#path:\
#exec_prefix:\
#exec:\
#exec_suffix\
#"

#example
"crusader kings 2:\
${HOME}/hdd35/wine32/drive_c/hry/Crusader_Kings_II/:\
env WINEPREFIX=${HOME}/hdd35/wine32/ WINEDEBUG=fixme-all,err-all,warn+heap,trace-all wine :\
${HOME}/hdd35/wine32/drive_c/hry/Crusader_Kings_II/CK2game.exe:\
"

"ftl:\
${HOME}/games/ftl/:\
:\
${HOME}/games/ftl/start.sh:\
"

#end of entry
)

###################################################
# Main code
###################################################

# get dmenu theme
if ! [ -f "$HOME/.config/dmenurc" ]; then
	cp /usr/share/dmenu/dmenurc $HOME/.config/dmenurc
fi
. $HOME/.config/dmenurc

# sorts entry array alphabetically
IFS=$'\n' data=($(sort <<<"${entry[*]}"));unset IFS

# build dmenu list (only from existing valid entries)
d_entry=""
for d in "${data[@]}"; do
    name=$(echo "${d}" | awk -F: ' { print $1 } ')
    path=$(echo "${d}" | awk -F: ' { print $2 } ')
    exec=$(echo "${d}" | awk -F: ' { print $4 } ')

    if [[ -d "${path}" && -f "${exec}" ]];then
        d_entry="${d_entry}${name}\n"
    fi
done

# get user input from the dmenu list
slection=$(echo -e "${d_entry}" | sed '/^$/d' | dmenu $DMENU_OPTIONS -p games: "$@")

# match user input and run exec
for d in "${data[@]}"; do
    name=$(echo "${d}" | awk -F: ' { print $1 } ')
    if [[ "${name}" == "${slection}" ]]; then
        path=$(echo "${d}" | awk -F: ' { print $2 } ')
        exec_prefix=$(echo "${d}" | awk -F: ' { print $3 } ')
        exec=$(echo "${d}" | awk -F: ' { print $4 } ')
        exec_suffix=$(echo "${d}" | awk -F: ' { print $5 } ')

        full_exec="${exec_prefix}${exec}${exec_suffix}"

        (cd "${path}"; bash -c "${full_exec}" > /dev/null 2>&1 &)
        break
    fi
done
1 Like

Forgot to post the actual script…:

#!/bin/bash

RCUO=$(ps -e -o pid,comm | grep -w -E 'rcuo[a-z]\/[0-9]+' | awk '{ print $1 }' | tr '\n' ',' | head -c-1)
IFS=',' read -r -a ARRAY <<< "$RCUO"

for i in "${ARRAY[@]}"
do
  taskset -cp 4,16 "$i"
done

Hey guys,

I have a couple of scripts I wrote as I was learning Arch, which I am still learning… I thought I would share them here… Mainly for Plasma DE as I only use Plasma, not tested on other DEs…

  • Automated KVM Install & Config Script :
#!/usr/bin/env bash
set -e
########################################################################################################################
# Author	:	TechXero Thanks to Ermanno Ferrari @EFLinux for How-To
# Website	:	https://www.techxero.com / https://www.eflinux.net
########################################################################################################################

echo "##################################################################################################################"
echo "#                                           KVM Installer Script                                                 #"
echo "##################################################################################################################"
sleep 3
echo "Downloading KVM and Dpendencies..."
sudo pacman -S --noconfirm --needed qemu qemu-arch-extra virt-manager ovmf vde2 ebtables dnsmasq bridge-utils openbsd-netcat
sudo wget "https://raw.githubusercontent.com/TechXero/ArchLinux/main/Configs-n-Scripts/kvmnet.xml" -O ~/kvmnet.xml
echo "Starting LibVirt-d Service..."
sudo systemctl enable libvirtd.service && sudo systemctl start libvirtd.service
sleep 5
echo "Setting up Networking services..."
sudo virsh net-define kvmnet.xml
sudo virsh net-start XeroNet
sudo virsh net-autostart XeroNet
echo "All Done Happy VM'ing. Brough to you by @TechXero & @ermanno_ferrari"
sleep 5
  • Automated SAMBA Install & Config Script :
#!/bin/bash
set -e
##################################################################################################################
# Author	:	TechXero
# Website	:	https://www.techxero.com
#################################################################################

echo "################################################################"
echo "#########       enabling samba software         ################"
echo "################################################################"

sudo pacman -S --noconfirm --needed samba
sudo wget "https://git.samba.org/samba.git/?p=samba.git;a=blob_plain;f=examples/smb.conf.default;hb=HEAD" -O /etc/samba/smb.conf.original
sudo wget "https://raw.githubusercontent.com/arcolinux/arcolinux-system-config/master/etc/samba/smb.conf.arcolinux" -O /etc/samba/smb.conf.arcolinux
sudo wget "https://raw.githubusercontent.com/TechXero/ArchLinux/main/Configs-n-Scripts/smb.conf" -O /etc/samba/smb.conf
sudo systemctl enable smb.service
sudo systemctl start smb.service
sudo systemctl enable nmb.service
sudo systemctl start nmb.service

##Change your username here
read -p "What is your login? It will be used to add this user to smb : " choice
sudo smbpasswd -a $choice

#access samba share windows
sudo pacman -S --noconfirm --needed gvfs-smb

echo "################################################################"
echo "#########       samba  software enabled         ################"
echo "################################################################"
sleep 5
echo "Network Discovery"

sudo pacman -S --noconfirm --needed avahi
sudo systemctl enable avahi-daemon.service
sudo systemctl start avahi-daemon.service

#shares on a mac
sudo pacman -S --noconfirm --needed nss-mdns

#shares on a linux
sudo pacman -S --noconfirm --needed gvfs-smb

#change nsswitch.conf for access to nas servers
#original line comes from the package filesystem
#hosts: files mymachines myhostname resolve [!UNAVAIL=return] dns
#ArcoLinux line
#hosts: files mymachines resolve [!UNAVAIL=return] mdns dns wins myhostname

#first part
sudo sed -i 's/files mymachines myhostname/files mymachines/g' /etc/nsswitch.conf
#last part
sudo sed -i 's/\[\!UNAVAIL=return\] dns/\[\!UNAVAIL=return\] mdns dns wins myhostname/g' /etc/nsswitch.conf

echo "################################################################"
echo "####       network discovery  software installed        ########"
echo "################################################################"
sleep 5
echo "Network Shares"

if pacman -Qi samba &> /dev/null; then
  echo "###################################################################"
  echo "Samba is installed"
  echo "###################################################################"
else
  tput setaf 1;echo "###################################################################"
  echo "First use our scripts to install samba and/or network discovery"
  echo "###################################################################";tput sgr0
  exit 1
fi

#checking if filemanager is installed then install extra packages
if pacman -Qi dolphin &> /dev/null; then
  sudo pacman -S --noconfirm --needed kdenetwork-filesharing
fi
if pacman -Qi thunar &> /dev/null; then
  yay -S --noconfirm --needed thunar-shares-plugin
fi

FILE=/etc/samba/smb.conf

if test -f "$FILE"; then
    echo "/etc/samba/smb.conf has been found"
else
  tput setaf 1;echo "###################################################################"
  echo "We did not find /etc/samba/smb.conf"
  echo "First use our scripts to install samba and/or network discovery"
  echo "###################################################################";tput sgr0
  exit 1
fi

sleep 5

echo "################################################################"
echo "#########                       Some TechXero Magic                     ###############"
echo "################################################################"

file="/etc/samba/smb.conf"

sudo sed -i '/^\[global\]/a \
\
usershare allow guests = true \
usershare max shares =  100 \
usershare owner only = no \
usershare path = /var/lib/samba/usershares' $file

sudo mkdir -p /var/lib/samba/usershares
sudo groupadd -r sambashare
sudo gpasswd -a $USER sambashare
sudo chown root:sambashare /var/lib/samba/usershares
sudo chmod 1770 /var/lib/samba/usershares

tput setaf 1;echo "###################################################################"
echo "Now reboot before sharing folders"
echo "###################################################################";tput sgr0
  • Pamac-AUR for those like me who love it… (Uses Paru as helper since yay is dying)
#!/bin/bash
set -e
##################################################################################################################
# Written to be used on 64 bits computers
# Author 	: 	TechXero
# Website 	: 	http://www.techxero.com
##################################################################################################################
#################################################################################

sudo pacman -S base-devel --noconfirm --needed

echo "###################################################################"
echo "#########   Installing Paru & grabing Pamac-All ;)  ###############"
echo "###################################################################"
sleep 5

echo "First Step - Building & Installing Paru AUR Helper..."
sleep 5
source="https://aur.archlinux.org/cgit/aur.git/plain/PKGBUILD?h=paru-bin"
folder="paru-bin"
name="PKGBUILD"

mkdir /tmp/$folder
wget $source -O /tmp/$folder/$name
cd /tmp/$folder
makepkg -i
sleep 5

echo "Final Step - Building & Installing Pamac-AUR GUI..."
sleep 5
paru -S pamac-aur
sleep 5

echo "All done, now you can profit !"
sleep 3
echo "Brought to you by @TechXero :D"
  • My .bashrc Aliases collection :
#
# ~/.bashrc
#

# If not running interactively, don't do anything
[[ $- != *i* ]] && return

alias ls='ls --color=auto'
PS1='[\u@\h \W]\$ '

#list
alias ls='ls --color=auto'
alias la='ls -a'
alias ll='ls -la'
alias l='ls'          
alias l.="ls -A | egrep '^\.'"      

alias vtop='vtop -t wizard'

#fix obvious typo's
alias cd..="cd .."
alias pdw="pwd"

## Colorize the grep command output for ease of use (good for log files)##
alias grep="grep --color=auto"
alias egrep="egrep --color=auto"
alias fgrep="fgrep --color=auto"

#readable output
alias df='df -h'

#pacman unlock

alias unlock="sudo rm /var/lib/pacman/db.lck"

#free
alias free="free -mt"

#continue download
alias wget="wget -c"

#userlist
alias userlist="cut -d: -f1 /etc/passwd"

#merge new settings
alias merge="xrdb -merge ~/.Xresources"

# Aliases for software managment
# pacman or yay
alias search='sudo pacman -Qs'
alias remove='sudo pacman -R'
alias install='sudo pacman -S'
alias update='sudo pacman -Syyu'
alias clrcache='sudo pacman -Scc'
alias upall='yay && sudo pacman -Syyu'
alias psr="sudo pacman -Ss --color auto"
alias unlock='sudo rm /var/lib/pacman/db.lck'
alias orphans='sudo pacman -Rns $(pacman -Qtdq)'

alias aget='yay -S'
alias arm='yay -Rs'
alias pksyua='yay -Syu --noconfirm'

alias pdep="deps p"
alias pclean="sudo pacman -Rns \$(pacman -Qtdq)"

# yay as aur helper - updates everything
alias pksyua="yay -Syu --noconfirm"
alias upall="yay -Syu --noconfirm"
alias yins="yay -S --color auto"
alias yrm="yay -R --color auto"
alias ysr="yay -Ss --color auto"
alias ydep="deps y"

#generate repo databases
alias add-repo="repo-add hefftor-repo.db.tar.gz *.pkg.tar.xz"

#ps
alias psa="ps auxf"
alias psgrep="ps aux | grep -v grep | grep -i -e VSZ -e"

#grub update
alias grubup='sudo grub-mkconfig -o /boot/grub/grub.cfg'

#improve png
alias fixpng="find . -type f -name "*.png" -exec convert {} -strip {} \;"

#add new fonts
alias fc="sudo fc-cache -fv"

#copy/paste all content of /etc/skel over to home folder - Beware
alias skel='cp -Rf ~/.config ~/.config-backup-$(date +%Y.%m.%d-%H.%M.%S) && cp -rf /etc/skel/* ~'
#backup contents of /etc/skel to hidden backup folder in home/user
alias bupskel='cp -Rf /etc/skel ~/.skel-backup-$(date +%Y.%m.%d-%H.%M.%S)'


#get fastest mirrors in your neighborhood 
alias mirror="sudo reflector -f 30 -l 30 --number 10 --verbose --save /etc/pacman.d/mirrorlist"
alias mirrord="sudo reflector --latest 50 --number 20 --sort delay --save /etc/pacman.d/mirrorlist"
alias mirrors="sudo reflector --latest 50 --number 20 --sort score --save /etc/pacman.d/mirrorlist"
alias mirrora="sudo reflector --latest 50 --number 20 --sort age --save /etc/pacman.d/mirrorlist"


#copy bashrc-latest over on bashrc - cb= copy bashrc
alias cb="sudo cp /etc/skel/.bashrc-latest /etc/skel/.bashrc && cp /etc/skel/.bashrc-latest ~/.bashrc && cp /etc/skel/.bashrc-latest ~/.bashrc-latest && source ~/.bashrc"
alias ci="sudo cp /etc/skel/.inputrc-latest /etc/skel/.inputrc && cp /etc/skel/.inputrc-latest ~/.inputrc && cp /etc/skel/.inputrc-latest ~/.inputrc-latest"

#switch between bash and zsh
alias tobash="sudo chsh $USER -s /bin/bash && echo 'Now log out.'"
alias tozsh="sudo chsh $USER -s /bin/zsh && echo 'Now log out.'"

#quickly kill conkies
alias kc='killall conky'

#mounting the folder Public for exchange between host and guest on virtualbox
alias vbm="sudo mount -t vboxsf -o rw,uid=1000,gid=1000 Public /home/$USER/Public"

alias gpl="git pull"
alias gcl="git clone"
alias ga="git add"
alias gc="git commit -m"
alias gp="git push -u origin master"
alias gr="git rm"
alias grd="git rm -r"
alias gra="gitremote"
alias gcln="gitclone"

#Bash aliases
alias mkfile='touch'
alias thor='sudo thunar'
alias kc='killall conky'
alias jctl='journalctl -p 3 -xb'
alias ssaver='xscreensaver-demo'
alias pingme='ping -c64 techxero.com'
alias cls='clear && neofetch | lolcat'
alias traceme='traceroute techxero.com'
alias nxplayer='cd /usr/NX/bin/ && ./nxplayer & cls'
alias microcode='grep . /sys/devices/system/cpu/vulnerabilities/*'

# alias serv="ssh USER@192.168.1.3"
# alias servseed="ssh USER@joker.seedhost.eu"

#hardware info --short
alias hw="hwinfo --short"

alias sZ="source ~/.bashrc"

alias ~="cd ~ && source ~/.bashrc"

alias yt='youtube-dl --recode-video mp4'

#youtube-dl
alias yta-aac="youtube-dl --extract-audio --audio-format aac "
alias yta-best="youtube-dl --extract-audio --audio-format best "
alias yta-flac="youtube-dl --extract-audio --audio-format flac "
alias yta-m4a="youtube-dl --extract-audio --audio-format m4a "
alias yta-mp3="youtube-dl --extract-audio --audio-format mp3 "
alias yta-opus="youtube-dl --extract-audio --audio-format opus "
alias yta-vorbis="youtube-dl --extract-audio --audio-format vorbis "
alias yta-wav="youtube-dl --extract-audio --audio-format wav "

alias ytv-best="youtube-dl -f bestvideo+bestaudio "

alias rmd='rm -r'
alias srm='sudo rm'
alias srmd='sudo rm -r'
alias cpd='cp -R'
alias scp='sudo cp'
alias scpd='sudo cp -R'

alias slin='sudo ln -s'
alias lin='ln -s'

#Recent Installed Packages
alias rip="expac --timefmt='%Y-%m-%d %T' '%l\t%n %v' | sort | tail -100"

#Cleanup orphaned packages
alias cleanup='sudo pacman -Rns $(pacman -Qtdq)'

#get the error messages from journalctl
alias jctl="journalctl -p 3 -xb"

#nano
alias bashrc='sudo nano ~/.bashrc'
alias pconf='sudo nano /etc/pacman.conf'
alias mkpkg='sudo nano /etc/makepkg.conf'
alias ngrub='sudo nano /etc/default/grub'
alias smbconf='sudo nano /etc/samba/smb.conf'
alias baloorc='sudo nano ~/.config/baloofilerc'
alias tgram='sudo nano ~/.local/share/applications/userapp-Telegram*'

alias nmkinitcpio="sudo nano /etc/mkinitcpio.conf"
alias nhefflogout="sudo nano /etc/oblogout.conf"

#shutdown or reboot
alias ssn="sudo shutdown now"
alias sr="sudo reboot"

#cd/ aliases
alias home='cd ~'
alias etc='cd /etc/'
alias music='cd ~/Music'
alias vids='cd ~/Videos'
alias conf='cd ~/.config'
alias desk='cd ~/Desktop'
alias pics='cd ~/Pictures'
alias dldz='cd ~/Downloads'
alias docs='cd ~/Documents'
alias sapps='cd /usr/share/applications'
alias lapps='cd ~/.local/share/applications'

#XanMod Kernel Builder 
alias xankey='gpg --recv-key'
alias xanbuild='nano PKGBUILD && makepkg -si'
alias xanclone='cd ~ && git clone https://aur.archlinux.org/linux-xanmod.git && cd linux-xanmod'

#shopt
shopt -s autocd # change to named directory
shopt -s cdspell # autocorrects cd misspellings
shopt -s cmdhist # save multi-line commands in history as single line
shopt -s dotglob
shopt -s histappend # do not overwrite history
shopt -s expand_aliases # expand aliases
# shopt -s nocasematch

clear && neofetch | lolcat
EDITOR=nano

Hope those help

7 Likes

The rumours of yay’s death have been greatly exaggerated.

mkdir /tmp/$folder

A safer way to create a temporary directory is using mktemp -d command (see the man page for mktemp)

sleep 5

Don’t do that… :man_facepalming:

1 Like

The rumours of yay 's death have been greatly exaggerated.

Well, either way both are good… Enjoy my scripts, feel free to modify to suite your needs :wink:

A safer way to create a temporary directory is using mktemp -d command (see the man page for mktemp )

Will fix that… Thanks buddy…

Don’t do that…

Sorry am a slow reader need those so I know what script is doing… :stuck_out_tongue:

2 Likes

This is clearly the greatest QoL ever invented by humankind! :joy:

3 Likes

I also use alias sduo=sudo and it’t extreamly handy since I mess the keystroke about 30% of the time. :laughing:

3 Likes

udpate and update both work on system… :grin: - along with cd… of course. Also - I avoid most sudo usage by prepending an s to a command - you can guess how spacman works… (or at least be close!)

4 Likes

Here are some of my zsh aliases (most should work with bash, too, some might need a little tweaking):

alias logout="[[ -o login ]] && logout || qdbus org.kde.ksmserver /KSMServer logout 0 0 3 && exit"

alias fix-compositor='qdbus org.kde.KWin /Compositor suspend && qdbus org.kde.KWin /Compositor resume'

alias cd-='cd - >/dev/null'

alias dirs='dirs -v'

alias rename='qmv --format=do' # needs "renameutils" package

alias ccopy='xclip -sel c'
alias cpaste='xclip -o -sel c'
alias cclear='qdbus org.kde.klipper /klipper org.kde.klipper.klipper.clearClipboardHistory'
alias ytc='youtube-dl $(xclip -o -sel c)'

alias op='xdg-open >/dev/null 2>&1'

alias kate='setsid /usr/bin/kate >/dev/null 2>&1'
alias dolphin='setsid /usr/bin/dolphin >/dev/null 2>&1'
alias smp='setsid /usr/bin/smplayer >/dev/null 2>&1'
alias calc='bc ~/.bcrc -l -q'

alias reload-firefox='xdotool search --onlyvisible --class Firefox key --window %@ "F5" || >&2 echo "Unable to find a Firefox window."'

alias xeyes='setsid xeyes -fg black >/dev/null 2>&1' # needs xorg-xeyes
alias xclock='setsid xclock -tiecolor "#080" -bg "#FF0" 2>&1' # needs catclock-git from the AUR

line() {
  printf "%$(tput cols)s\n" | sed 'y/ /─/'
}

I’ve got a bunch more, but these are the most interesting ones.

4 Likes

space-man. :laughing: :rocket:

alias reload-firefox='xdotool search --onlyvisible --class Firefox key --window %@ "F5" || >&2 echo "Unable to find a Firefox window."'

@Kresimir do I understand it correctly that instead of pressing F5 key you write this command? Isn’t it just a little too much?

3 Likes

It’s a terminal-centric distro, isn’t it? Pfff… :frog:

But seriously, as an alias, it’s pretty useless. However, put it into a script and it suddenly becomes very useful.

For example, when editing a HTML file, you can have the browser automatically reload it every time you save it:

echo file.html | entr reload-firefox
4 Likes

Indeed they are interesting!

Never used setsid, but that provides a nice way to put something in the background in an alias. Usually I use functions with an appended &, simply because functions are so versatile and good for other tricks too.

1 Like

Yes, but if you run a GUI program in the background, when you exit the shell, the program exits, too. With setsid the program runs in a separate shell session, so you can quit your terminal emulator and the program continues to run.

On KDE, you can use

kioclient5 exec path_to_desktop_file

to get an equivalent result, but I prefer setsid, as it is more portable.

2 Likes

Looks like the behavior depends on how you terminate the terminal.

I tried command

 eos-welcome &

and then quit the terminal with the exit. Welcome did not exit.
Then I did the same but now terminated the terminal with the right upper corner (X) and now both stopped.

With setsid the program stays when terminal quits.

Nice! Life is a great learning experience! :sweat_smile:

3 Likes

I have a little setup that is too simple minded to post here, but the idea behind it might be worth mentioning.

I have a number of applications with either ugly icons (Thunar, Code) or in need of Exec command line mods (turn off passwords, scale the interface). The concept I came up with is a script called by a pacman hook when those items are updated, and copies ‘correct’ .desktop files into /usr/share/applications. Actually, I think I’m just lazy! :grin:

Should be pretty easy for anyone to duplicate (in whole or in part as desired)…

1 Like

But why do you need to copy the .desktop files on every update? :thinking:

1 Like