Linux Tips

cd
# 切換回家目錄

cd -
# 切換回上一個瀏覽的目錄



history
# 瀏覽執行過的指令記錄

!指令記錄的編號
# 直接執行該指令

!指令名稱:p
# 搜尋該指令但不執行

Ctrl-R
# 進入歷史命令搜尋模式,壓左右鍵修改,壓 Enter 直接執行

Ctrl-U
# 清除目前輸入的指令,要消除一長串指令時很好用!



cp 檔案名稱{,.backup}
# 快速將該檔案進行備份



mtr google.com
# 對 google.com 進行 ping 與 traceroute



sudo !!
# 以 sudo 執行上一條指令



vim ~/.bash_aliases
# 設定屬於自己的指令別名

alias ll='ls -lhF'
alias la='ls -alh'



pushd 目錄路徑
# 將指定的目錄推入目錄堆疊

popd
# 彈出目錄堆疊中最上層的目錄(即前往該目錄)

dirs
# 顯示目前的目錄堆疊



export PS1="[\t] \u@\h \w> "
# PS1 環境變數可以設定 shell 提示符號樣式

export PS2="Continuation Interactive Prompt >"
# PS2 環境變數可以設定多行指令的提示符號

export PROMPT_COMMAND="echo -n [$(date+%k:%m:%S)]"
# PROMPT_COMMAND 環境變數可以設定 shell 提示符號前的內容



export PS1="\e[1;34m \u@\h \w> \e[m "
# 改變 shell 提示符號的顏色
# \e[:顏色開始改變
# 1;34m:顏色碼
# \e[m:顏色終止改變

顏色碼:
Black 0;30
Blue 0;34
Green 0;32
Cyan 0;36
Red 0;31
Purple 0;35
Brown 0;33



tar cvf *.tar *
tar xvf *.tar

tar zcvf *.tar.gzip *
tar zxvf *.tar.gzip

tar jcvf *.tar.bz2 *
tar jxvf *.tar.bz2



Standard Input:0
Standard Output:1
Standard Error:2

1>
1>>
# 標準輸出重導向

2>
2>>
# 錯誤輸出重導向

2>&1
# 錯誤輸出重導向至標準輸出之導向

-
# 可以視情況代表 Standard Input 或 Standard Output

cat > file < input_file
# 由 input_file 讀入資料至 file



command 0 && command 1 || command 2



grep -ni 'RooT'
# 顯示行號 (n)
# 忽略大小寫 (i)

nl
# 讀取檔案,並顯示行號



cut -d ':' -f 3
# 每列以 : 切割
# 取第 3 個 field



sort -fn
# 排序忽略大小寫 (f)
# 排序以純數字 (n)



uniq -c
# 將資料唯一化
# 計算重複次數 (c)



wc -lwm
# 數檔案內的行 (l)
# 數檔案內的字 (w)
# 數檔案內的字元 (m)



tr 'a' 'A'
# a 取代爲 A

tr -d 'a'
# 刪除 a



tee
# 資料導向檔案,並且螢幕輸出



col -x
# 將 Tab 取代爲 Space

expand -t 8
# 將 Tab 取代爲 8 個 Space

unexpand
# 將 Space 取代爲 Tab



join
# 兩個檔案中,將具有相同資料的列,聯合在一起



paste -d '#' - file
# 將 Standard Input 與 file 同一行貼在一起,以 # 作爲分隔



split -l 10
# 以 10 行來分割檔案



command 0 | xargs command 1
# 將 Command 0 的 Ouput 轉成 Command 1 的「參數」



sed
# Stream Editor



awk



diff



patch



find . -name "some*thing" -exec grep -l "some*word" {} \;
find . -name "some*thing" | xargs grep -l "some*word"
grep -l "some*word" `find . -name "some*thing"`

# 三者效果相同
# {} 爲 -exec 命令參數的佔位器,將前一命令的輸出逐一放至此
# xargs 將前一命令之輸出傳至後一命令作爲參數



sudo apache2 -V
sudo apache2 -l
sudo apache2 -t -f /etc/httpd.conf.debug

sudo httpd -V
sudo httpd -l
sudo httpd -t -f /etc/httpd.conf.debug



echo $RANDOM



bash -xv file.sh
# 使用 debug 模式執行 Shell Script 檔

set -xv
# 設定該 Shell Script 檔在 debug 模式執行



df -Tha
# Disk Free

du -sh
# Disk Usage



dpkg -L PackageName

apt-cache show PackageName
apt-cache showpkg PackageName
apt-cache depends PackageName
apt-cache stats



顯示資料夾名稱

ls -l | grep ^d | sed 's:.*\ ::g'
tree -dL 1
ls -d */
find . -type d -maxdepth 1
alias lt="ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'"

查看 USER 開啟的檔案

lsof –u USER

Reference:
http://www.commandlinefu.com/commands/browse
http://linuxtoy.org/archives/top-10-one-liners.html