Linux tool

Reference


Bash Shell Shortcut

  • 在 bash 里,使用 Ctrl-R 而不是上下光標鍵來查找歷史命令。
  • 在 bash里,使用 Ctrl-W 來刪除最后一個單詞,使用 Ctrl-U 來刪除一行。請man bash后查找Readline Key Bindings一節來看看bash的默認熱鍵,比如:Alt-. 把上一次命令的最后一個參數打出來,而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 需要在[單用戶模式]可用的必要命令([可執行文件]);面向所有用戶,例如: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. 必要的系統二進制文件,例如: 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. 非必要可執行文件(在單用戶模式中不需要);面向所有用戶。
    • /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. 非必要的系統二進制文件,例如:大量[網絡服務]的[守護進程]。
    • /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. 本地數據的第三層次, 具體到本臺主機。通常而言有進一步的子目錄, 例如: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 :列出長數據串,包含文件的屬性與權限數據等
-a :列出全部的文件,連同隱藏文件(開頭為.的文件)一起列出來(常用)
-d :僅列出目錄本身,而不是列出目錄的文件數據
-h :將文件容量以較易讀的方式(GB,kB等)列出來
-R :連同子目錄的內容一起列出(遞歸列出),等于該目錄下的所有文件都會顯示出來

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文件的方式查找數據
-c :計算找到‘查找字符串’的次數
-i :忽略大小寫的區別,即把大小寫視為相同
-v :反向選擇,即顯示出沒有‘查找字符串’內容的那一行

------- 例如:-------
取出文件/etc/man.config中包含MANPATH的行,并把找到的關鍵字加上顏色:
grep --color=auto 'MANPATH' /etc/man.config
把ls -l的輸出中包含字母file(不區分大小寫)的內容輸出:
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]
與時間有關的參數:
-mtime n : n為數字,意思為在n天之前的“一天內”被更改過的文件;
-mtime +n : 列出在n天之前(不含n天本身)被更改過的文件名;
-mtime -n : 列出在n天之內(含n天本身)被更改過的文件名;
-newer file : 列出比file還要新的文件名
例如:
find /root -mtime 0 # 在當前目錄下查找今天之內有改動的文件

與用戶或用戶組名有關的參數:
-user name : 列出文件所有者為name的文件
-group name : 列出文件所屬用戶組為name的文件
-uid n : 列出文件所有者為用戶ID為n的文件
-gid n : 列出文件所屬用戶組為用戶組ID為n的文件
例如:
find /home/ljianhui -user ljianhui # 在目錄/home/ljianhui中找出所有者為ljianhui的文件

與文件權限及名稱有關的參數:
-name filename :找出文件名為filename的文件
-size [+-]SIZE :找出比SIZE還要大(+)或?。?)的文件
-tpye TYPE :查找文件的類型為TYPE的文件,TYPE的值主要有:一般文件(f)、設備文件(b、c)、
目錄(d)、連接文件(l)、socket(s)、FIFO管道文件(p);
-perm mode :查找文件權限剛好等于mode的文件,mode用數字表示,如0755;
-perm -mode :查找文件權限必須要全部包括mode權限的文件,mode用數字表示
-perm +mode :查找文件權限包含任一mode的權限的文件,mode用數字表示
例如:
find / -name passwd # 查找文件名為passwd的文件
find ./ -name '.o'
find ./ -name "
.o" -exec rm {} ;
find . -perm 0755 # 查找當前目錄中文件權限的0755的文件
find . -size +12k # 查找當前目錄中大于12KB的文件,注意c表示byte

cp

-a :將文件的特性一起復制
-p :連同文件的屬性一起復制,而非使用默認方式,與-a相似,常用于備份
-i :若目標文件已經存在時,在覆蓋時會先詢問操作的進行
-r :遞歸持續復制,用于目錄的復制行為
-u :目標文件與源文件有差異時才會復制

cp -a file1 file2 #連同文件的所有特性把文件file1復制成文件file2
cp file1 file2 file3 dir #把文件file1、file2、file3復制到目錄dir中

mv

-f :force強制的意思,如果目標文件已經存在,不會詢問而直接覆蓋
-i :若目標文件已經存在,就會詢問是否覆蓋
-u :若目標文件已經存在,且比目標文件新,才會更新

mv file1 file2 file3 dir # 把文件file1、file2、file3移動到目錄dir中
mv file1 file2 # 把文件file1重命名為file2

rm

-f :就是force的意思,忽略不存在的文件,不會出現警告消息
-i :互動模式,在刪除前會詢問用戶是否操作
-r :遞歸刪除,最常用于目錄刪除,它是一個非常危險的參數

rm -i file # 刪除文件file,在刪除之前會詢問是否進行該操作
rm -fr dir # 強制刪除目錄dir中的所有文件

file

file filename
例如:
file ./test

tar

-c :新建打包文件
-t :查看打包文件的內容含有哪些文件名
-x :解打包或解壓縮的功能,可以搭配-C(大寫)指定解壓的目錄,注意-c,-t,-x不能同時出現在同一條命令中
-j :通過bzip2的支持進行壓縮/解壓縮
-z :通過gzip的支持進行壓縮/解壓縮
-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 :硬連接;刪除一個,將仍能找到;
ln -s cc ccTo :符號鏈接(軟鏈接);刪除源,另一個無法使用;(后面一個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 :進行遞歸的持續對所有文件和子目錄更改
例如:
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


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容