Reference
- http://blog.jobbole.com/86948/ : 11個(gè)讓你吃驚的 Linux 終端命令
- http://coolshell.cn/articles/8883.html : 應(yīng)該知道的Linux技巧(from coolshell)
- http://coolshell.cn/articles/4990.html : 程序員技術(shù)練級攻略
- http://linuxtools-rst.readthedocs.org/zh_CN/latest/index.html : Linux工具快速教程(complete)
Bash Shell Shortcut
- 在 bash 里脖律,使用 Ctrl-R 而不是上下光標(biāo)鍵來查找歷史命令净赴。
- 在 bash里蛾号,使用 Ctrl-W 來刪除最后一個(gè)單詞捏检,使用 Ctrl-U 來刪除一行奢驯。請man bash后查找Readline Key Bindings一節(jié)來看看bash的默認(rèn)熱鍵,比如:Alt-. 把上一次命令的最后一個(gè)參數(shù)打出來岩榆,而Alt-* 則列出你可以輸入的命令各薇。
System Directory Structure
- /bin : All the executable binary programs (file) required during booting, repairing, files required to run into single-user-mode, and other important, basic commands 需要在[單用戶模式]可用的必要命令([可執(zhí)行文件]);面向所有用戶鳞仙,例如:cat, ls, cp.
- /boot : Holds important files during boot-up process, including Linux Kernel.
- /dev : Contains device files for all the hardware devices on the machine e.g., cdrom, cpu, etc
- /etc : Contains Application’s configuration files, startup, shutdown, start, stop script for every individual program.
- /home : Home directory of the users. Every time a new user is created, a directory in the name of user is created within home directory which contains other directories. For Mac, user files are stored in Users.
- /lib : The Lib directory contains kernel modules and shared library images required to boot the system and run commands in root file system.
- /media : Temporary mount directory is created for removable devices viz., media/cdrom.
- /mnt : Temporary mount directory for mounting file system.
- /opt : Optional is abbreviated as opt. Contains third party application software. Viz., Java, etc.
- /proc : A virtual and pseudo file-system which contains information about running process with a particular Process-id aka pid.
- /root : This is the home directory of root user and should never be confused with ‘/‘
- /run : This directory is the only clean solution for early-runtime-dir problem.
- /sbin : Contains binary executable programs, required by System Administrator, for Maintenance. 必要的系統(tǒng)二進(jìn)制文件寇蚊,例如: init、 ip棍好、 mount仗岸。
- /srv : Service is abbreviated as ‘srv‘. This directory contains server specific and service related files.
- /sys : Modern Linux distributions include a /sys directory as a virtual filesystem, which stores and allows modification of the devices connected to the system.
- /tmp :System’s Temporary Directory, Accessible by users and root. Stores temporary files for user andsystem, till next boot.
- /usr : Contains executable binaries, documentation, source code, libraries for second level program.
- /var : Stands for variable. The contents of this file is expected to grow. This directory contains log, lock,spool, mail and temp files.
Some further explanation
bin is different from lib. bin is for binary file, while lib or Library is for library (like dynamic/static linking library).
-
/usr – User Programs
Contains binaries, libraries, documentation, and source-code for second level programs.- /usr/bin contains binary files for user programs. If you can’t find a user binary under /bin, look under /usr/bin. For example: at, awk, cc, less, scp. 非必要可執(zhí)行文件(在單用戶模式中不需要);面向所有用戶借笙。
- /usr/sbin contains binary files for system administrators. If you can’t find a system binary under /sbin, look under /usr/sbin. For example: atd, cron, sshd, useradd, userdel. 非必要的系統(tǒng)二進(jìn)制文件扒怖,例如:大量[網(wǎng)絡(luò)服務(wù)]的[守護(hù)進(jìn)程]。
- /usr/lib contains libraries for /usr/bin and /usr/sbin
- /usr/local contains users programs that you install from source. For example, when you install apache from source, it goes under /usr/local/apache2. 本地?cái)?shù)據(jù)的第三層次提澎, 具體到本臺主機(jī)姚垃。通常而言有進(jìn)一步的子目錄念链, 例如:bin/, lib/, share/.
/lib – System Libraries
Contains library files that supports the binaries located under /bin and /sbin
Library filenames are either ld* or lib.so.
For example: ld-2.11.1.so, libncurses.so.5.7/etc – Configuration Files
Contains configuration files required by all programs.
This also contains startup and shutdown shell scripts used to start/stop individual programs.
For example: /etc/resolv.conf, /etc/logrotate.conf/bin – User Binaries
Contains binary executables.
Common linux commands you need to use in single-user modes are located under this directory.
Commands used by all the users of the system are located here.
For example: ps, ls, ping, grep, cp./sbin – System Binaries
Just like /bin, /sbin also contains binary executables.
But, the linux commands located under this directory are used typically by system aministrator, for system maintenance purpose.
For example: iptables, reboot, fdisk, ifconfig, swapon/etc/ssh/
Other important directories under user directory:
- /Users/abrocod/.ssh
- /Users/abrocod/.aws
Common Shell Command
Simple Command Collection
- which
- whereis
- whoami
- date
- printenv
- env
- alias
alias ls='ls --color=auto'
alias grep='grep --color=auto'
sudo -i # run as root user
sudo !!
sudo -E
ls
-l :列出長數(shù)據(jù)串盼忌,包含文件的屬性與權(quán)限數(shù)據(jù)等
-a :列出全部的文件,連同隱藏文件(開頭為.的文件)一起列出來(常用)
-d :僅列出目錄本身掂墓,而不是列出目錄的文件數(shù)據(jù)
-h :將文件容量以較易讀的方式(GB谦纱,kB等)列出來
-R :連同子目錄的內(nèi)容一起列出(遞歸列出),等于該目錄下的所有文件都會顯示出來
du, df
check directory size and check the whole disk space utility
du -sh
du -h
df -h
grep
grep [-acinv] [--color=auto] '查找字符串' filename
-a :將binary文件以text文件的方式查找數(shù)據(jù)
-c :計(jì)算找到‘查找字符串’的次數(shù)
-i :忽略大小寫的區(qū)別君编,即把大小寫視為相同
-v :反向選擇跨嘉,即顯示出沒有‘查找字符串’內(nèi)容的那一行
------- 例如:-------
取出文件/etc/man.config中包含MANPATH的行,并把找到的關(guān)鍵字加上顏色:
grep --color=auto 'MANPATH' /etc/man.config
把ls -l的輸出中包含字母file(不區(qū)分大小寫)的內(nèi)容輸出:
ls -l | grep -i file
- search for pattern
grep "pattern" -r *
cut
clean file:
cut -c3-73 sample_thumbid.txt > clean_tbnail.txt
find
find [PATH] [option] [action]
與時(shí)間有關(guān)的參數(shù):
-mtime n : n為數(shù)字吃嘿,意思為在n天之前的“一天內(nèi)”被更改過的文件祠乃;
-mtime +n : 列出在n天之前(不含n天本身)被更改過的文件名;
-mtime -n : 列出在n天之內(nèi)(含n天本身)被更改過的文件名兑燥;
-newer file : 列出比file還要新的文件名
例如:
find /root -mtime 0 # 在當(dāng)前目錄下查找今天之內(nèi)有改動(dòng)的文件
與用戶或用戶組名有關(guān)的參數(shù):
-user name : 列出文件所有者為name的文件
-group name : 列出文件所屬用戶組為name的文件
-uid n : 列出文件所有者為用戶ID為n的文件
-gid n : 列出文件所屬用戶組為用戶組ID為n的文件
例如:
find /home/ljianhui -user ljianhui # 在目錄/home/ljianhui中找出所有者為ljianhui的文件
與文件權(quán)限及名稱有關(guān)的參數(shù):
-name filename :找出文件名為filename的文件
-size [+-]SIZE :找出比SIZE還要大(+)或辛链伞(-)的文件
-tpye TYPE :查找文件的類型為TYPE的文件,TYPE的值主要有:一般文件(f)降瞳、設(shè)備文件(b嘱支、c)蚓胸、
目錄(d)、連接文件(l)除师、socket(s)沛膳、FIFO管道文件(p);
-perm mode :查找文件權(quán)限剛好等于mode的文件汛聚,mode用數(shù)字表示锹安,如0755;
-perm -mode :查找文件權(quán)限必須要全部包括mode權(quán)限的文件倚舀,mode用數(shù)字表示
-perm +mode :查找文件權(quán)限包含任一mode的權(quán)限的文件八毯,mode用數(shù)字表示
例如:
find / -name passwd # 查找文件名為passwd的文件
find ./ -name '.o'
find ./ -name ".o" -exec rm {} ;
find . -perm 0755 # 查找當(dāng)前目錄中文件權(quán)限的0755的文件
find . -size +12k # 查找當(dāng)前目錄中大于12KB的文件,注意c表示byte
cp
-a :將文件的特性一起復(fù)制
-p :連同文件的屬性一起復(fù)制瞄桨,而非使用默認(rèn)方式话速,與-a相似,常用于備份
-i :若目標(biāo)文件已經(jīng)存在時(shí)芯侥,在覆蓋時(shí)會先詢問操作的進(jìn)行
-r :遞歸持續(xù)復(fù)制泊交,用于目錄的復(fù)制行為
-u :目標(biāo)文件與源文件有差異時(shí)才會復(fù)制
cp -a file1 file2 #連同文件的所有特性把文件file1復(fù)制成文件file2
cp file1 file2 file3 dir #把文件file1、file2柱查、file3復(fù)制到目錄dir中
mv
-f :force強(qiáng)制的意思廓俭,如果目標(biāo)文件已經(jīng)存在,不會詢問而直接覆蓋
-i :若目標(biāo)文件已經(jīng)存在唉工,就會詢問是否覆蓋
-u :若目標(biāo)文件已經(jīng)存在研乒,且比目標(biāo)文件新,才會更新
mv file1 file2 file3 dir # 把文件file1淋硝、file2雹熬、file3移動(dòng)到目錄dir中
mv file1 file2 # 把文件file1重命名為file2
rm
-f :就是force的意思,忽略不存在的文件谣膳,不會出現(xiàn)警告消息
-i :互動(dòng)模式竿报,在刪除前會詢問用戶是否操作
-r :遞歸刪除,最常用于目錄刪除继谚,它是一個(gè)非常危險(xiǎn)的參數(shù)
rm -i file # 刪除文件file烈菌,在刪除之前會詢問是否進(jìn)行該操作
rm -fr dir # 強(qiáng)制刪除目錄dir中的所有文件
file
file filename
例如:
file ./test
tar
-c :新建打包文件
-t :查看打包文件的內(nèi)容含有哪些文件名
-x :解打包或解壓縮的功能,可以搭配-C(大寫)指定解壓的目錄花履,注意-c,-t,-x不能同時(shí)出現(xiàn)在同一條命令中
-j :通過bzip2的支持進(jìn)行壓縮/解壓縮
-z :通過gzip的支持進(jìn)行壓縮/解壓縮
-v :在壓縮/解壓縮過程中芽世,將正在處理的文件名顯示出來
-f filename :filename為要處理的文件
-C dir :指定壓縮/解壓縮的目錄dir
壓縮:tar -zcvf filename.tar.gz 要被處理的文件或目錄名稱
查詢:tar -ztvf filename.tar.gz
解壓:tar -zxvf filename.tar.gz -C 欲解壓縮的目錄
tar -zcvf test.tar.gz *
Note: -f option must follow the filename !!!
-- if the file is just .tar, not .tar.gz or .tar.bz2, then use:
tar -xvf filename.tar -C ./dir
-- if the file is just a .zip
unzip file.zip -d destination_folder
zip
zip squash.zip file1 file2 file3
zip -r squash.zip dir1 # zip recursively, -r
unzip squash.zip
use less squash.zip to look at the content of zip file
or use 'unzip -l squash.zip', which is the same as less
ln
ln cc ccAgain :硬連接;刪除一個(gè)诡壁,將仍能找到济瓢;
ln -s cc ccTo :符號鏈接(軟鏈接);刪除源欢峰,另一個(gè)無法使用葬荷;(后面一個(gè)ccTo 為新建的文件)
ln -s TARGET LINK_NAME
- Find the link after softlink
ls -l `which java`
lrwxr-xr-x 1 root wheel 74 Sep 1 15:27 /usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
chgrp, chown
chgrp [-R] dirname/filename
-R :進(jìn)行遞歸的持續(xù)對所有文件和子目錄更改
例如:
chgrp users -R ./dir # 遞歸地把dir目錄下中的所有文件和子目錄下所有文件的用戶組修改為users
groups <username> -- check which group user belongs to
-- chown
chown owner-user file
chown owner-user:owner-group file
chown owner-user:owner-group directory
chown options owner-user:owner-group file
e.g:
chown vivek demo.txt # change owner
chown vivek:vivek demo.txt # change owner and group
chown :ftp demo.txt # change only group
chown -R vivek /home/vivek # recursively change
open file in Ubuntu
You can use:
gnome-open file2open.xxx (xxx = some file extension).
With this command gnome will invoke the default app for "xxx"
----------- for example evince if you want to open pdf ------------
Or specifically:
evince file2open.pdf
Or (default for KDE):
okular file2open.pdf
process management
ps
ps [aux]
ps aux | grep 'chrome' | less
pstree
top
Top will give you a realtime view of the system and only show the number of processes which will fit on the screen.
kill
kill [signal] <PID>
e.g:
kill 6978
kill -1 6978 // kill/quit the process gently
kill -9 6978 // kill the process with brutal force
lsof
If you know what port the process is running you can type: lsof -i:<port>. For instance, lsof -i:8080, to list the process (pid) running on port 8080. Then kill the process with kill <pid>
tail
The command tail -f will display the last 10 lines of a file, and then continuously wait for new lines, and display them as they appear.
$ tail -f /var/log/messages
If you want to see more than ten lines at the outset, specify the new number (say 50 lines) like this:
$ tail -50 -f /var/log/messages