Linux tool

Reference


Bash Shell Shortcut

  • 在 bash 里,使用 Ctrl-R 而不是上下光標(biāo)鍵來查找歷史命令。
  • 在 bash里,使用 Ctrl-W 來刪除最后一個(gè)單詞,使用 Ctrl-U 來刪除一行。請(qǐng)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ù)的第三層次, 具體到本臺(tái)主機(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)容一起列出(遞歸列出),等于該目錄下的所有文件都會(huì)顯示出來

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í)會(huì)先詢問操作的進(jìn)行
-r :遞歸持續(xù)復(fù)制,用于目錄的復(fù)制行為
-u :目標(biāo)文件與源文件有差異時(shí)才會(huì)復(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)存在,不會(huì)詢問而直接覆蓋
-i :若目標(biāo)文件已經(jīng)存在,就會(huì)詢問是否覆蓋
-u :若目標(biāo)文件已經(jīng)存在,且比目標(biāo)文件新,才會(huì)更新

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

rm

-f :就是force的意思,忽略不存在的文件,不會(huì)出現(xiàn)警告消息
-i :互動(dòng)模式,在刪除前會(huì)詢問用戶是否操作
-r :遞歸刪除,最常用于目錄刪除,它是一個(gè)非常危險(xiǎn)的參數(shù)

rm -i file # 刪除文件file,在刪除之前會(huì)詢問是否進(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 :符號(hào)鏈接(軟鏈接);刪除源,另一個(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ù)對(duì)所有文件和子目錄更改
例如:
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


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容