$ ~ $ 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
nl #number line
xargs <cmd> #change input to arguments, use with pipe
sed #stream editor
pbcopy < <file> # OSX: copy file into clipboard
$ hash # list command and its path
$ type -a <cmd> # tell all path of commamd
$ which <cmd> # tell default path of command
$ 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 # 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 -e #edit crontab profile
# */5 * * * * cmd
# run cmd every five minutes
$ grep CRON /var/log/syslog #see crontablog (debian)
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
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 --brief <file1> <file2>
$ cat /etc/*-release
$ export TZ=Asia/Bangkok date
$ Ctrl+R grep Ctrl+R Ctrl+R
# 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
# 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'
#!/bin/bash
VAR = "hello"
if [ "$VAR" = "hello" ]; then # use = to equal string not ==
echo $VAR
else
echo "fasle"
fi
$ ./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)
$ echo 'rm() { mkdir -p /tmp/rm; /bin/mv "$@" /tmp/rm; }' >> .zshrc
cmd1 && cmd2 # cmd 2 run only if cmd1 successful
cmd1 ; cmd2 # cmd 2 run whether failed or not
process1 &
process2 &
process3 &
process4 &
wait # wait until process1, 2, 3, 4 done
process5 &
process6 &
process7 &
process8 &
wait # wait until process5, 6, 7, 8 done
#!/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
if [ $? -eq 0 ]
do
echo "hello"
done
#!/bin/bash
array=( one two three )
for i in "${array[@]}"
do
echo $i
done
$ dirname "/home/user/dir/file.py"
/home/user/dir
$ basename "/home/user/dir/file.py"
file.py
last reboot | less
last -x|grep shutdown | head -1
Improve Your oh-my-zsh Startup Time (Maybe)
# check start zsh time
/usr/bin/time zsh -i -c exit
$ less +F log/production.log