Common

Unzip files

ls > unzipnames.txt
touch unzip_script.sh
chmod +x unzip_script.sh

Contents of unzip_script.sh

#!/bin/bash
for file in $(cat unzipnames.txt)
do
    tar -xf $file
    echo "$file done"
done

Make your life easy while working in a terminal

Browsing through your computer using a terminal can be extremely efficient or inefficient depending on how you use it. I make it very efficient for myself by using these commands. I have an alias of most of the directories (not shown below) I visit regularly. And since I visit them so often, I never have any difficulty remembering the shortcut I created for them. Usually, my shortcuts contain three or four letters and intuitively (for me at least) named, so I can guess even if I forget.

I have this in my ~/.zshrc,

# zsh_aliases
if [ -f ~/.zsh_aliases ]; then
    . ~/.zsh_aliases
fi
# zsh_functions
if [ -f ~/.zsh_functions ]; then
    . ~/.zsh_functions
fi
# zsh_exports
if [ -f ~/.zsh_exports ]; then
    . ~/.zsh_exports
fi

and I have three files in my home directory, .zsh_aliases, .zsh_exports, and .zsh_functions with the following contents (I removed the ones that were too personal). I have added the comments here for your understanding.

#######
# ~/.zsh_aliases
#######

# System commands
alias szrc='source ~/.zshrc'
alias rm='rm -i' 
alias mv='mv -i' 
alias cb='cd .. && ls' 
alias lsh='ls -ltrh' ## list the contents, list the file size, and sort them according to time
alias dus='du -sh' ## A shortcut to see the total size of a directory. I mostly use it like 'dus .' or 'dus directory-name/'.

# Programs
## jekyll and bundle
alias bje='bundle exec jekyll build'  ## build the website
alias sje='bundle exec jekyll serve' ## serve the website on a local address
## zsh
alias vza="vim ~/.zsh_aliases" ## Since I use the aliases and functions so much, I must have a shortcut to access them quickly whenever I need to look something up or edit them.
alias vzf="vim ~/.zsh_functions"
alias vze="vim ~/.zsh_exports"
alias vzrc="vim ~/.zshrc"
## vim
alias v='vim'
alias vd='vimdiff'
alias vvrc='vim ~/.vimrc'
## git
alias gst='git status'
alias gaa='git add .'
alias ga='git add' 
alias gcm='git commit -m'
alias gca='git commit -a'
alias gp='git push'
alias gun='git config user.name'
alias gue='git config user.email'
## python
alias py='python'
alias acon='conda activate'
alias dcon='conda deactivate'
alias jn='jupyter notebook'
alias rootenv='conda activate rootenv'
alias aenv='conda activate aenv'
alias rdcal='conda activate rdcal'

# directories
alias dow='cd ~/Downloads && ls'
alias doc='cd ~/Documents && ls'
alias son='cd ~/Music/songs && ls'
alias pic='cd ~/Pictures && ls'
alias vid='cd ~/Videos && ls'
alias pro='cd ~/products && ls'
alias docs='cd $uti/docs/ && ls' ## see .zsh_exports for the definition of 'uti'
alias uti='cd $uti && ls' 
alias aca='cd $uti/academics && ls'
alias boo='cd $uti/academics/books && ls'
alias gdy='cd ~/git && ls'

## dyfrad
alias dyf='cd ~/git/dyfrad && ls'
alias dys='cd ~/git/dyfrad/_scripts && ls'
alias udy='~/git/dyfrad/_scripts/_update-site.sh'
alias ddy='~/git/dyfrad/_scripts/_dev-site.sh'

## msaharan
alias msa='cd ~/git/msaharan && ls'
alias mss='cd ~/git/msaharan/_scripts && ls'
alias ums='~/git/msaharan/_scripts/_update-site.sh'
alias dms='~/git/msaharan/_scripts/_dev-site.sh'

## sencilla
alias sen='cd ~/git/sencilla && ls'

# ssh 
alias sname='ssh name@servername.com' ## I do not like to type `ssh name@servername.com` everytime I log in. I always make an alias or a function to do it faster. With this alias, I would have to type just 'cname' in the terminal.
alias snamey='ssh -Y name@clustername.com' ## transfer the display from the server to your computer. I use this to see plots and other graphical things while working on a server.
#######
# ~/.zsh_exports
#######

# directories
export uti=~/Documents/utility
export msa=~/git/msaharan/
export dyf=~/git/dyfrad/
#######
# ~/.zsh_functions
#######

## how to use these functions
## For example, delete files using the 'trash' function defined below
## trash filename # moves the file to the Trash directory that you have defined.
## trash file1 file2 file3 file4 ## The "$@" argument in the definition means "all arguments", so this line moves all the files to the Trash directory.

# system commands

## delete safely
trash() { mv "$@" ~/Trash } # I find it safer to use 'trash filename' instead of 'rm filename'. I make a folder named 'Trash' somewhere in my computer and that acts as my recycle bin for the stuff I delete through command line.

## change directory and show contents
cf() { cd "$@" && ls; } # Makes navigating direcories super easy. I hate having to do 'cd directory_name' and then 'ls' to see the contents. I have a similar definition for going back in ''.zsh_aliases'.

## locate command
l10() { locate "$1" --limit="10"; } # find things in your computer and limit the list to 10 results. 
l20() { locate "$1" --limit="20"; }

# programs

## youtube-dl
ydl() { youtube-dl -ix --audio-format mp3 --audio-quality 0 "$1"; } ## Download best quality audio from YouTube using youtube-downloader. I download music like this. It extracts the audio and saves it in mp3 format.

## grep
gnr() { grep -nr "$1" "$2"; } ## find a string (argument "$1") in the directory of your choice (argument "$2"). Usage example: gnr bloga ./ # serches for the word 'bloga' in the current directory. The output includes the files (with line numbers) that contain this word.

## texstudio
ts() { texstudio "$1" & }

## conda
jnb() { jupyter notebook "$1" & } ## I use it to stop the running jupyter kernels like this: jnb stop 8888.

## ssh 
snamet() { ssh -L 8888:localhost:"$1" name@servername.com; } ## makes a tunnel between the server and my computer. I can access the localhost:xxxx port of the server on my computer at the address localhost:8888. I run Jupyter notebooks on a server and view them on my laptop's web browser.

snamett { ssh -L "$2":localhost:"$3" name@servername.com; } ## usage: cnamett 8889 8891 ## makes a tunnel between the port 8891 of the server and the port 8889 of your computer. I use this mostly when the default port 8888 is busy on the server or on my laptop.

MacOS

Manage Images

Compress

JPEG

Install jpegoptim

brew install jpegoptim

Use

  • -p flag to preserve the original file.
  • -d to save the compressed file to directory of choice.
  • -size=200k to reduce the file size to 200 KBs. This is lossy compression, so the file quality will be reduced.
  • -m20 to compress the image to retain 20% of its original quality.
mkdir compressed
jpegoptim --size=300k name.jpg -d compressed/
PNG

Install optipng

brew install optipng

Manage PDFs

Split PDF

I do not know about a program for MacOS that could extract multiple pages from a PDF file and save them in a single output file. It’s simple in Linux but unreasonably complicated in a Mac. Well, I found a workaround for myself. Here’s what I do:

pdfseparate -f 4 -l 6 myfile.pdf myfile-trimmed%d.pdf && pdfunite $ls myfile-trimmed*.pdf myfile-trimmed-combined.pdf && rm -f myfile-trimmed*.pdf

Here:

  • -f first page that you want to extract
  • -l last page number
  • %d page number. myfile-trimmed4.pdf would contain page 4 of myfile.pdf.
  • pdfseparate -f 4 -l 6 myfile.pdf myfile-trimmed%d.pdf
    • Generate myfile-trimmed4.pdf, myfile-trimmed5.pdf, and myfile-trimmed6.pdf
  • pdfunite $ls myfile-trimmed*.pdf myfile-trimmed-combined.pdf
    • Merge myfile-trimmed4.pdf, myfile-trimmed5.pdf, and myfile-trimmed6.pdf into a single file named myfile-trimmed-combined.pdf.
  • rm -f myfile-trimmed*.pdf
    • Clean up the mess. Be careful with this command. I advise you to use rm -iinstead of rm -f if you are extracting a few pages. The -i flag will prompt you to confirm the deletion of each file.

This command gives the following warning:

Syntax Warning: PDFDoc::markDictionnary: Found recursive dicts

but it does the job so I don’t care.

PS: When I finished writing the last line, I scrolled on this page to see how I split PDF in Linux. I use pdftk in Linux. A quick brew search pdftk showed me that pdftk-java was available to install. Well, now I know that I could use pdftk-java to split a PDF.


Linux

File-system / drives / partitions

List all partitions

lsblk

Mount drives

  • List all partitions using lsblk or fdisk -l and identify the drive you want to mount.
  • Mount it
    sudo mount /dev/sd<?><?> /mnt/<name>/
    

Bootable USB

Source

  • Identify
  • Unmount the drive if it is mounted
    sudo unmount /dev/sd<?><?>
    
  • Write
     sudo dd bs=4M if=path/to/input.iso of=/dev/sd<?> conv=fdatasync  status=progress
    

Date command in Bash

Source

SSH keys

Start the ssh agent:


eval $(ssh-agent)

Add ssh key:


ssh-add

Remember password for one hour:


git config --global credential.helper 'cache --timeout=3600'

Updates in Arch based distros

Source

  • Ignore a package while updating the system:
    sudo pacman -Syu --ignore=dunst
    
  • Force update - The source suggests
    sudo pacman -Syu --overwrite /usr/bin/dusntify
    

    but the following command also worked for me.

    sudo pacman -Syu --overwrite=/usr/bin/dusntify
    

PDF

Compress

Source 1, 2

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/prepress -dNOPAUSE -dQUIET -dBATCH -sOutputFile=compressed_PDF_file.pdf input_PDF_file.pdf

Merge

Source

gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=combine.pdf -dBATCH file1.pdf file2.pdf

Split pdf using pdftk

Continuous range:

# Split the pages ranging from 1 to 20
pdftk myoldfile.pdf cat 1-20 output mynewfile.pdf

Variable range:

# Split the pages ranging from 1-20, 23-40 and combine them to make a single file
pdftk myoldfile.pdf cat 1-20 23-40 output mynewfile.pdf

Find and replace strings within files in a directory

sed -i 's/search-something/replace-with/' file.txt

Search a string within a directory using grep

source

grep -nr 'yourString*' .
-n  Show relative line number in the file
-r Recursively search subdirectories listed
. Directory for search (current directory)

Manjaro i3

Configure I3WM

Source 1, 2, 3

Screen brightness control in Manjaro i3

Install light

sudo pacman -S light

Add yourself to video group (Source 1, 2)

sudo usermod -a -G video $USER

Log out and log back. Run groups to make sure that you are in the video group Add the following lines to i3config file:

bindsym XF86MonBrightnessUp exec --no-startup-id light -A 5
bindsym XF86MonBrightnessDown exec --no-startup-id light -U 5

I had to reboot to make the brightness controls work.

Wi-Fi

nmtui

Set wallpaper with feh

Source

/home/dyskun/.local/share/wallpapers
feh --bg-scale /path/to/image.file

Other scaling options include:

--bg-tile FILE
--bg-center FILE
--bg-max FILE
--bg-fill FILE

To restore the background on the next session, add the following to your startup file (e.g. ~/.xinitrc):

~/.fehbg &

Change scroll direction

Source

Open /usr/share/X11/xorg.conf.d/40-libinput.conf and add

Option "NaturalScrolling" "True"

to get something like this.

Section "InputClass"                                                     
        Identifier "libinput pointer catchall"
        MatchIsPointer "on"
        MatchDevicePath "/dev/input/event*"
        Driver "libinput"
    Option "NaturalScrolling" "True"
EndSection

Section "InputClass"
        Identifier "libinput touchpad catchall"
        MatchIsTouchpad "on"
        MatchDevicePath "/dev/input/event*"
        Driver "libinput"
    Option "NaturalScrolling" "True"
EndSection