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

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.

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

Deploying Hugo with Rsync.

#!/bin/sh
USER=yourUser # Your hosting's username
HOST=yourHostingIPAddress # Your hosting's IP address
DIR=httpdocs/ # The directory where your website files should go, i.e., the web server root of your website

SECONDS=0
rm -r ./public
hugo # It builds the public folder.
duration=$SECONDS # It returns a count of the number of seconds the shell has been running.
echo "$(($duration / 60)) minutes and $(($duration % 60)) seconds took the task."

rsync --compress --recursive --verbose --checksum --delete -e 'ssh -p 2222' --delete public/ ${USER}@${HOST}:~/${DIR}

The syntax is rsync OPTIONS SOURCE [[email protected]]HOST:DEST.

  1. ‐‐delete allows remote deletion of files, so it mirrors the local (public/) and remote directories (httpdocs/) and deletes extraneous files from the remote directory.
  2. -e ‘ssh -p 2222’ uses a non-standard SSH port (2222).
  3. SOURCE is the local directory, i.e., the Hugo’s default “public” directory in our local machine.
  4. [[email protected]]HOST:DEST indicates our hosting’s username, IP address, and destination directory.
  5. rsync compresses the file data as it is sent to the destination machine, so it reduces the amount of data being transmitted. It skips files that have likely not changed. We want files to be updated if they have a different size or checksum. I prefer to replace ‐‐archive with ‐‐recursive, because archive implies many options related to file metadata (owner, group, permissions, and timestamp), and I don’t want to update timestamps.

Credits: Hugo, Deployment with Rsync [https://nicholas.cloud/](Nicholas Whittaker), A pretty Little Bit of Rsync.

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. Get information about your Linux system.
  5. Bibliography: Awesome Bash is a curated list of delightful Bash scripts and resources.
Bitcoin donation

JustToThePoint Copyright © 2011 - 2022 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.