JustToThePoint English Website Version
JustToThePoint en español
JustToThePoint in Thai

Programming in Linux III. Useful scripts

Zsh

All you need is #!/bin/bash

All you need is #!/bin/bash

ZSH, also called the Z shell, is an extended version of the Bourne Shell, with plenty of new features and support for plugins and themes. .zshrc is the configuration file that contains the commands that run the zsh shell, vim .zshrc:

Let us see colorful man pages:

export MANPAGER="sh -c 'col -bx | bat -l man -p'"
# Let us define which commands should not be saved in the history.
export HISTORY_IGNORE="(ls|cd|pwd|exit|sudo reboot|history|cd ..)"
if [ -d "$HOME/.local/bin" ] ; 
# If the directory $HOME/.local/bin exists, add it to the path.
  then PATH="$HOME/.local/bin:$PATH"
fi

# If the directory $HOME/AppImages exists, add it to the path.
if [ -d "$HOME/AppImages" ] ;
  then PATH="$HOME/AppImages:$PATH"
fi

# Setting the default text editor
export EDITOR=/usr/bin/nvim
export VISUAL=/usr/bin/nvim

# Path to your oh-my-zsh installation.
export ZSH="~/.oh-my-zsh"

Powerlevel10k is a theme for Zsh. It emphasizes speed, flexibility, and out-of-the-box experience. We set the name of the theme to load.

ZSH_THEME="powerlevel10k/powerlevel10k" 
# Uncomment the following line to enable command auto-correction.
ENABLE_CORRECTION="true"

# colored-man-pages: adds colors to man pages
# chucknorris: Chuck Norris fortunes plugin for oh-my-zsh. It adds two commands: chuck and chuck_cow, super cool!!!
# colorize: it will syntax-highlight the content of a file based on the filename extension. Requirements: pip install Pygments. Use: ccat file, cless file
# emoji: Support for Unicode emoji. Use: echo $emoji[mouse\_face\]
# z: Tracks your most used directories and z will take you to the most frequent directories. Use: z myFolder, cd to the most frequent directory matching myFolder. z -l myFolder, list all dirs matching myFolder (by frequency)
# zsh-syntax-highlighting. It provides syntax highlighting for the shell zsh.
plugins=(git zsh-autosuggestions colored-man-pages chucknorris colorize emoji z zsh-syntax-highlighting)

Oh My Zsh is a delightful, open source, community-driven framework -somehow bloated- for managing your Zsh configuration.

source $ZSH/oh-my-zsh.sh
source ~/.zsh\_aliases

# Bash and Zsh insulter. I know, I know, an absolute must-have!
if [ -f /etc/bash.command-not-found ]; then
    . /etc/bash.command-not-found
fi

Zsh Aliases

Aliases are essentially shortcuts, a means of avoiding typing or remembering a long command sequence that you use often.

nvim ~/.zsh_aliases

alias ezsh="atom $HOME/.zshrc"
alias reload='source ~/.zshrc'
alias myip='curl http://ipecho.net/plain; echo'
alias atom='atom --no-sandbox'
alias fortune='fortune ~/.config/myquotes/quotes' # It selects a random quote from my collection.
alias ll='ls -alF'
alias ls='exa --long --header --git'
alias cat='bat --style=plain'
  1. exa is a modern replacement for ls: sudo pacman -Sy –noconfirmm bat exa.

  2. bat is a cat clone with wings with syntax highlighting and Git integration.

  3. ripgrep is a line-oriented search tool that recursively searches the current directory for a regex pattern.

    How do I find all files containing a specific text? Using grep: grep -rnw “/path/to/somewhere” -e “specificText” (-r: recursive; -n: line number; w: match the whole world). Using ripgrep: ripgrep “specificText” /path/to/somewhere

  4. youtube-dl download videos from YouTube.com or other video platforms.

alias df='df -h' # df reports file system disk usage with human-readable sizes.
alias free='free -m' # free displays the total amount of free and used memory in the system in MB

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

alias ei3='atom ~/.config/i3/config'
alias ferdi='./AppImages/Ferdi-5.8.0.AppImage'
alias radio='curseradio'

alias preview="fzf --preview 'bat --style=numbers --color=always {}'"
searchEdit() { du -a ~/Dropbox/ ~/.config/ ~/.local/bin/ | awk '{print $2}' | fzf | xargs -r $EDITOR ; }

# Create a folder and move into it in one command
mkcd() { mkdir -p "[email protected]" && cd "$\_"; }

# Search quickly: googleit "What is the capital of France"
googleit() { xdg-open "http://google.com/search?q=[email protected]"}

# Search inside files recursively: ripgrep "Marta"
ripgrep() { rg "[email protected]" -A 2 }

# It converts the video to mp3 and saves it in the Music folder.
ytmp3() { cd ~/Music; youtube-dl --extract-audio --audio-format mp3 "[email protected]"; }

# Play all music files from the Music directory
alias playmp3='cd ~/Music; nvlc *.mp3'

Automate the “git, commit, and push”

#!/bin/bash
# It automates the git, commit, and push process (.local/bin/gitpush.sh)
cd ~/dotfiles
git add .

echo 'Enter the commit message:'
read commitMessage

git commit -m "$commitMessage"
git push

Open any file from the command line

#!/bin/bash
# Script to extract files, view images, read plain text files, play music, etc.
#!/bin/bash

if [ -f $1 ] ; then
  case $1 in
    *.tar.bz2)  tar xjf $1     ;;
    *.tar.gz)   tar xzf $1     ;;
    *.tar.xz)   tar zxvf $1    ;;
    *.bz2)      bunzip2 $1     ;;
    *.rar)      rar x $1       ;;
    *.gz)       gunzip $1      ;;
    *.tar)      tar xf $1      ;;
    *.tbz2)     tar xjf $1     ;;
    *.tgz)      tar xzf $1     ;;
    *.xz)       xz -d $1       ;;
    *.zip)      unzip $1       ;;
    *.Z)        uncompress $1  ;;
    *.pdf)      evince $1      ;; # sudo pacman -S feh evince
    *.mov|*.avi|*.mpeg|*.mpg|*.flv|*.wmv|*.mp4|*.mp3) vlc $1    ;;
    *.doc|*.docx|*.xls|*.xlsx|*.ppt|*.pptx|*.odf) libreoffice $1 ;;
    *.txt|*.conf|*.sh|*.py|*.hs) atom --no-sandbox  $1;;
    *.png | *.jpeg | *.jpg) feh $1;;
    *)          xdg-open $1    ;;
  esac
else
  echo "'$1' is not recognized as a compressed file"
fi

A small script to clone and build packages from AUR

aurpkgs="bpytop deadbeef"
for pkg in $aurpkgs; do
    git clone "https://aur.archlinux.org/$pkg.git"
    cd $pkg
    makepkg -si
    cd ..
    rm -rf $pkg
  done

Updating your Arch

#!/bin/bash

# The archlinux-keyring package contains the latest keys.
pacman -S archlinux-keyring

# Reflector is a Python script that can retrieve the latest mirror list from the Arch Linux Mirror Status page.
# It selects the 5 most up-to-date mirrors from a specified location, sorts them by speed, and overwrites the file /etc/pacman.d/mirrorlist.
sudo reflector --verbose --country 'Spain' --latest 5 --sort rate --save /etc/pacman.d/mirrorlist

# Update all installed packages and verify their integrity.
sudo pacman -Syyu

# Update UAR packages.
yay -Syu

# Delete Pacman cache.
sudo pacman -Scc

# Delete Yay cache.
yay -Scc

# Delete unwanted dependencies.
yay -Yc

# Remove orphaned packages.
sudo pacman -Rns $(pacman -Qtdq)

# Clean the Cache.
rm -rf .cache/*

# Clean the journal. Journalctl is a utility for displaying logs from journald, systemd's logging service.
sudo journalctl --vacuum-time=2weeks

# Update the database of the files currently on your system. It is used with locale.
sudo updatedb

# Scan for viruses. It checks and removes all the infected files in the home directory.
clamscan --recursive --remove ~

Let’s use rsync to mirror HOME directory to an external drive (/media/mydisk/).

The --delete flag tells rsync to delete any files at the destination that are not currently at the source. -aAXv means that the files are transferred in “archive” mode, which ensures that symbolic links, permissions, ownerships, modification times, ACLs, and extended attributes are preserved.

rsync -aAXv --delete $HOME /media/mydisk/home

Information about your system

#!/bin/bash

function networking(){
	echo "Networking"
	echo "------------------------------"
	IFACE=$(ip route show default | awk '/default/ {print $5}')
	IP=$(ip -o -4 addr list $IFACE | awk '{print $4}' | cut -d/ -f1)
	MAC=$(cat "/sys/class/net/$IFACE/address")
	echo "Default Network Interface: $IFACE"
	echo "IP Address: $IP. MAC Address: $MAC"
	echo -n "Public IP: " && curl https://ipinfo.io/ip
	local dnsips=$(sed -e '/^$/d' /etc/resolv.conf | awk '{if (tolower($1)=="nameserver") print $2}')
	echo ". Hostname: $(uname -n)"
	echo "DNS name servers: " ${dnsips} | sed 's/\\\\n/\\n/g'
}

function systemResources(){
	echo "System Resources"
	echo "------------------------------"
	echo "Memory Usage:"
	/usr/bin/free -h

	echo $'\\n'$"Disk Usage:"
	df -hP | egrep '^/dev/'

	echo $'\\n'$"Uptime: (Current time, how long the system has been running, number of users with running sessions, and the system load averages)"
	/usr/bin/uptime
}

neofetch
echo
networking  
echo
systemResources
echo
echo "Temperatures"
sensors
echo "Clipboard: $(copyq clipboard)"
echo "Type htop, an interactive text-mode process viewer. q exit."

More scripts:

  1. Download an image (png, jpg, and jpeg) and convert it to webp
  2. Bash file converter from (png, jpg, and jpeg) to webp.
  3. Scripts for checking broken links.
  4. Bibliography: Awesome Bash is a curated list of delightful Bash scripts and resources.
Bitcoin donation

JustToThePoint Copyright © 2011 - 2022 PhD. Máximo Núñez Alarcón, Anawim. ALL RIGHTS RESERVED. Bilingual e-books, articles, and videos to help your child and your entire family succeed, develop a healthy lifestyle, and have a lot of fun.

This website uses cookies to improve your navigation experience.
By continuing, you are consenting to our use of cookies, in accordance with our Cookies Policy and Website Terms and Conditions of use.