botbotbot 's blog

Unix

Helper

$ ~ $ home user directory
$ ~+ # same as pwd
$ ~- # previous directory
$ ls -1 $PWD/*.txt # list all .txt with full path
$ cd - # cd to previous path
$ export EDITOR=vim # set vim as default editor
$ groups #list group
$ getent group <group_name> #list members of group
$ top #check performance
$ du -sh <folder> # summary disk usage of folder
$ df -h # disk free
$ chsh - <user> # change default ssh user
$ printenv # list environment variables
$ \<cmd> # run command by  avoid alias

Command

nl #number line
xargs <cmd> #change input to arguments, use with pipe
sed #stream editor
pbcopy < <file> # OSX: copy file into clipboard

Where command

$ hash # list command and its path
$ type -a <cmd> # tell all path of commamd
$ which <cmd> # tell default path of command

Redirection

$ cmd > <file> # print output to file (overwrite)
$ cmd >> <file> # print output to file (append)
$ cmd 2> <file> # print error to file
$ cmd &> <file> # print output & error to file
$ cmd < <file> # use file as  input
$ cmd << <file> # use file as input
$ cmd <<< <any>  #use anyas input (here string not compute any tag)
# Redirection output wiht sudo
$ echo "hello" | sudo tee out > /dev/null # eq. echo "hello" > out
# echo "hello" | sudo tee -a out > /dev/null # eq echo "hello" > out

Jobs - fg bg Tutorial

$ jobs # list suspended jobs
$ fg # put the top suspended job in the foreground
$ fg %1 # put the suspended job id 1 in the foreground
Ctrl-z # Suspend a foreground job
$ bg # run suspended job in background
$ kill # kill job

Crontab Time schedule

$ crontab -e #edit crontab profile
# */5 * * * * cmd
# run cmd every five minutes
$ grep CRON /var/log/syslog #see crontablog (debian)

Zsh History expansion

should also work with bash.

$ man zshexpn # manual for look in-depth
$ history # tell history command that keep in ~/.bash_history
$ history -c # clear history
! # previous that can combine with command and argument
$ !! # call previous command
$ !<str> # call last command that start with str
$ !?<str>[?] # call last command containing str
$ !<hist_number> # call command in nth of history files
 ^ # first argument
 $ # last argument
 :n # nth argument
 :m-n # mth to nth argument
 * # all argument
$ <space><cmd> # call command without keep history

Zsh Basic Editing - Bash Shortcuts

should also work with bash.

$ bindkey -v # vi mode
$ bindkey -e # emac mode
Ctrl + a # moves the cursor to begin of line
Ctrl + e # moves the cursor to end of line
Ctrl + u # delete current to begin of line
Ctrl + k # delete until end of line
Ctrl + l # clear
Ctrl + w # delete word backwords
Ctrl + d # delete char (moves forward)
Ctrl + f # move char foreward
Ctrl + b # move char backward
Meta + f # move word foreward
Meata + b # move word backward
Ctrl + y # yank last delete word
Ctrl + t # transpoes two characters
Esc + t # transpoes two words

Meta is your Alt key, normally.
For Mac OSX user need to enable it,  Open Terminal > Preferences > Settings > Keyboard, and enable Use option as meta key.

Diff Command

$ diff --brief <file1> <file2>

HowTo: Find Out My Linux Distribution Name and Version

$ cat /etc/*-release

Timezone setting in Linux

$ export TZ=Asia/Bangkok date

How to cycle through reverse-i-search in BASH?

$ Ctrl+R grep Ctrl+R Ctrl+R

safeguard-rm

# only zsh

$ setopt noclobber # prevents overwriting an existing file.
$ setopt clobber # disable

$ unsetopt RM_STAR_SILENT # ask before rm with star (*)
$ setopt RM_STAR_WAIT # wait 10 sec before exec rm with star

Mac style copy/paste for linux machines

# copy/paste for linux machines (Mac style)
# sudo apt-get install xclip
$ alias pbcopy='xclip -selection clipboard'
$ alias pbpaste='xclip -selection clipboard -o'
$ alias pbselect='xclip -selection primary -o'

Bash

Shell/Bash Script

#!/bin/bash

VAR = "hello"
if [ "$VAR" = "hello" ]; then  # use = to equal string not ==
  echo $VAR
else
  echo "fasle"
fi

Run Script - Different ways to execute a shell script

$ ./script      #run with using shebang(#) in script ex. #!/usr/bin/python
$ . script      #run with shell (work in related bash)
$ source script #run with shell (work in both bash and csh)

Shell function - Passing argument to alias in bash

$ echo 'rm() { mkdir -p /tmp/rm; /bin/mv "$@" /tmp/rm; }' >> .zshrc

Execute combine multiple linux commands in one line

cmd1 && cmd2  # cmd 2 run only if cmd1 successful
cmd1 ; cmd2  # cmd 2 run whether failed or not

Parallel shell script

  1. Parallel batch processing in the shell
  2. Bash script processing commands in parallel
process1 &
process2 &
process3 &
process4 &
wait  # wait until process1, 2, 3, 4 done
process5 &
process6 &
process7 &
process8 &
wait  # wait until process5, 6, 7, 8 done
  1. Parallelize Bash Script
#!/bin/bash

nproc=$(($(nproc)-1))  # number of core cpu - 1

# waitproc - wait until have core slots
waitproc () {
  jobcnt=($(jobs -p))
  until [ ${#jobcnt[@]} -lt $nproc ]; do
    sleep 0.05
    jobcnt=($(job -p))
  done

Bash For Loop Array: Iterate Through Array Values

if [ $? -eq 0 ]
do
    echo "hello"
done
#!/bin/bash

array=( one two three )

for i in "${array[@]}"
do
    echo $i
done

Get file directory path from filepath

$ dirname "/home/user/dir/file.py"
/home/user/dir
$ basename "/home/user/dir/file.py"
file.py

How to find out from the logs what caused system shutdown?

last reboot | less
last -x|grep shutdown | head -1

Zsh

Bash References:

  1. Functions
  2. Bash shell expansion
  3. bash-tricks
  4. check command exitsts
  5. High-speed Bash

ETC

$ less +F log/production.log

Reference

  1. learning the shell
  2. HowTo: Add Jobs To cron Under Linux or UNIX?
  3. how to set cron job to run every 5 mins?
  4. CronHowto
  5. Checking UNIX Server Performance
  6. How do I use sudo to redirect output to a location I don’t have permission to write to?
  7. UNIX Disk Usage Command Examples
  8. Unix as IDE: Introduction
  9. หัดใช้ Command-line Interface เถอะ
  10. Pimp my command line
  11. The Art of Command Line
  12. Command Line Power User
  13. Here Strings
  14. what is the find command, and how do I use it to search
  15. Totally Tooling Mini Tip: Command-line Keyboard Shortcuts
  16. 10 examples of paste command usage in Linux
  17. Bang! Previous Command Hotkeys - Linux CLI BASH ZSH