1. 程式人生 > 實用技巧 >Linux系統管理——基礎命令篇

Linux系統管理——基礎命令篇

終於開始接觸Linux,現在是一個標準的菜鳥,那麼就踏踏實實,一步一步開始進階,希望我的部落格慢慢可以給後來人帶來幫助。

廢話少說,下面就是我所接觸的Linux系統管理的一些基礎命令,以及我個人新增的命令的釋義,還望大大們多多指教。

一、Linux命令列基礎


[[email protected] ~]# uname -r //檢視核心版本

[[email protected] ~]# uname -a //檢視核心相關詳細資訊

[[email protected] ~]# hostname //檢視主機名

localhost.localdomain

[[email protected]

~]# ifconfig //檢視網絡卡資訊

[[email protected] ~]# ifconfig eth0 //檢視eth0這塊網絡卡的資訊

[[email protected] ~]# cat /proc/cpuinfo //檢視CPU資訊

[[email protected] ~]# cat /proc/meminfo //檢視記憶體資訊

[[email protected] ~]# shutdown -h +15 'Host will be shutdown !!' //將在15分後關機

[[email protected] ~]# type cd //檢視“cd”是內部還是外部命令

[[email protected] ~]# help cd //檢視內部命令“cd”的幫助資訊

[[email protected] ~]# ls –help //檢視外部命令“ls”的幫助資訊

[[email protected] ~]# manpath //man手冊存放位置


[[email protected] ~]# makewhatis //生成“whatis”資料庫

[[email protected] ~]# whatispasswd //檢視簡化版手冊頁資訊

[[email protected] ~]# whatis passwd = man-f passwd

二、目錄和檔案基本管理

pwd 檢視當前位置

[[email protected] ~]# pwd

cd 目錄切換

[[email protected] src]# cd - //返回之前目錄

[[email protected] grub]# cd .. //返回上級目錄

[[email protected] boot]# cd //進入家目錄

[[email protected] ~]# cd ~root //進入家目錄


[[email protected] Desktop]# cd ../.. //返回上一級的上一級

[[email protected] /]# cd . //當前目錄

[[email protected] ~]# cd ~ //進入家目錄

lsList檢視

[[email protected] ~]# ls //列表檢視目錄內容

[[email protected] ~]# ls –l //長格式列表檢視目錄內容

[[email protected] ~]# ls -l -h

[[email protected] ~]# ls –lh

-h //提供易讀容量單位

[[email protected] ~]# ls -lhd /boot/ /home/

-d //檢視當前目錄本身屬性

[[email protected] boot]# ls –A //顯示隱藏檔案

[[email protected] boot]# ls –a //顯示. ..所有隱藏

[[email protected] boot]# ls –R //遞迴顯示內容

萬用字元

[[email protected] opt]# touch file1.txt //建立檔案file1.txt

[[email protected] opt]# touch file2.txt

[[email protected] opt]# touch file3.txt

[[email protected] opt]# touch filea.txt fileb.txt filec.txt

[[email protected] opt]# ls file*

* //匹配任意多個字元

[[email protected] opt]# ls file?

? //匹配任意單個字元

[[email protected] opt]# ls file??.txt

[[email protected] opt]# ls file?.txt

[[email protected] opt]# touch file12.txt

[[email protected] opt]# touch file123.txt

[[email protected] opt]# touch fileab.txt

[[email protected] opt]# ls

[[email protected]localhost opt]# ls file[0-9].txt

[[email protected] opt]# ls file[0-9][0-9].txt

[[email protected] opt]# ls file??.txt

[[email protected] opt]# ls file{1,ab,12}.txt

[[email protected] opt]# ls file{1,ab,12,abc}.txt

du 評估目錄或檔案大小

[[email protected] opt]# du -sh /boot/ /etc/pki/

[[email protected] opt]# mkdir 1406 //建立資料夾1406

[[email protected] opt]# ls

[[email protected] opt]# mkdir abc mp4 mp3

[[email protected] opt]# ls

[[email protected] opt]# mkdir -p aaa/bbb/ccc/ddd

-p //建立遞迴目錄

[[email protected] opt]# ls -R aaa

-R //檢視遞迴目錄資訊 各層目錄內容

[[email protected] opt]# mkdir -p /test1 data/mp4 mp3

[[email protected] opt]# ls -ld /test1/ data/ mp3

[[email protected] opt]# ls -l file1.txt

[[email protected] opt]# touch file1.txt //建立檔案file1.txt

[[email protected] opt]# ls -l file1.txt

[[email protected] opt]# ls -l /sbin/network

[[email protected] opt]# ln -s /usr/sbin/system-config-network-tui /sbin/network -s //建立連結源 目標

[[email protected] opt]# ls -l /sbin/network

[[email protected] opt]# ls /root/file1.txt

[[email protected] opt]# cp file1.txt /root/ //複製file1到/root

[[email protected] opt]# ls /root/file1.txt

[[email protected] opt]# ls

[[email protected] opt]# cp aaa /root/

[[email protected] opt]# ls /root/aaa

[[email protected] opt]# cp -r aaa /root/

-r //複製目錄

[[email protected] opt]# ls /root/aaa

[[email protected] opt]# ls /root/file1.txt

[[email protected] opt]# cp file1.txt /root/

[[email protected] opt]# cp -f file1.txt /root/

-f //強制覆蓋

[[email protected] opt]# alias //檢視別名

[[email protected] opt]# unalias cp //臨時取消別名

[[email protected] opt]# cp -f file1.txt /root/

[[email protected] opt]# \cp -f file1.txt /root/

\ //洗腳水(可以讓-i失效,這裡跟我的老師講的一個典故有關)

三、程式和檔案檢索 [locate/find]

[[email protected] ~]# echo $PATH //檢視命令路徑

[[email protected] ~]# which ls cd //查詢命令cd路徑(外)

[[email protected] ~]# updatedb //建立/更新資料庫

[[email protected] ~]# touch myhttpd.conf

[[email protected] ~]# ls

[[email protected] ~]# locate myhttpd.conf //基於資料庫查詢

[[email protected] ~]# updatedb //更新資料庫

[[email protected] ~]# locate myhttpd.conf

[[email protected] ~]# rm myhttpd.conf

[[email protected] ~]# locate myhttpd.conf

[[email protected] ~]# updatedb

[[email protected] ~]# locate myhttpd.conf

find

[[email protected] ~]# find /boot -type l //查詢boot下的連結

[[email protected] ~]# ls -l/boot/grub/menu.lst //檢視連結檔案

[[email protected] ~]# find /boot -type d //查詢boot下目錄

[[email protected] ~]# find /etc -name"resolv*conf" //按名字查詢


[[email protected] ~]# find /dev -type c -a -name "tty[1-3]"

-type //按檔案型別查詢

整句釋義//查詢dev下的字元裝置檔案必須匹配tty和編號1-3

[[email protected] ~]# ls -lh /boot/* //人性化方式長格式顯示boot下內容

[[email protected] ~]# find /boot -size +2M //查詢大於2M的檔案或目錄 //按照檔案大小查詢

[[email protected] ~]# cp install.loginstall.new

[[email protected] ~]# ls -lh install.???

[[email protected] ~]# find -name"install.???" -mtime +30

//查詢30天修改的以install.開頭的檔案

//按內容修改時間

[[email protected] ~]# find /boot -size +2M

[[email protected] ~]# find /boot -size +2M -exec ls -lh {} \;

//處理find找到的檔案//固定格式

//-exec只用於find

//以人性化長格式顯示找到的2M以上的檔案

Dmesg:系統啟動時所載入的硬體資訊

[[email protected] ~]# cat /etc/resolv.conf //檢視檔案內容

[[email protected] ~]# cat -n /etc/resolv.conf //行數編號檢視

[[email protected] ~]# cat /root/install.log

[[email protected] ~]# cat -n/root/install.log

[[email protected] ~]# cat -n /etc/passwd

[[email protected] ~]# more /root/install.log //分頁檢視大檔案

[[email protected] ~]# type ls

[[email protected] ~]# ls --help

[[email protected] ~]# ls --help | more //|”管道,管道前的執行結果作為管道後的輸入

[[email protected] ~]# cat /root/install.log |more

[[email protected] ~]# more /root/install.log

[[email protected] ~]#less /root/install.log //分頁檢視大檔案

more更靈活方便

[[email protected] ~]#head /etc/passwd //顯示前幾行內容

預設前十行

[[email protected] ~]# head -n 2 /etc/passwd //開啟file前兩行

[[email protected] ~]# tail /var/log/messages //開啟檔案末尾

//預設開啟後10

[[email protected] ~]#tail -n 2 /etc/passwd //開啟檔案後兩行

[[email protected] ~]# head -n 12 /etc/passwd | tail -n 5

//開啟812 開啟前12行,再開啟其中後5

[[email protected] ~]# touch news.txt

[[email protected] ~]# tail -f news.txt //實時檢查檔案內容變化

切換終端(Ctrl + Shift + t

> //寫入,覆蓋之前檔案

>> //追加

[[email protected] ~]# echo 111111111 >>news.txt

//111111111寫進檔案

[[email protected] ~]# echo 222222222 >>news.txt

[[email protected] ~]# echo 333333333 >>news.txt

[[email protected] ~]# wc /etc/passwd //顯示檔案行數,大小

[[email protected] ~]# wc -l /etc/passwd //只顯示檔案行數

[[email protected] ~]# find /etc -name "*.conf" -a -type f | wc

等同於find /etc -type f -name "*.conf"|wc

//查詢以.conf結尾的普通檔案然後顯示其數量

這裡的-a可以省略

[[email protected] ~]# cat /etc/hosts

[[email protected] ~]# grep 127.0.0.1 /etc/hosts //過濾含有關鍵字的內容

[[email protected] ~]# grep --color 127.0.0.1 /etc/hosts

//將關鍵字以其他顏色顯示

[[email protected] ~]# grep -v 127.0.0.1 /etc/hosts

//過濾顯示含關鍵字以外的內容

[[email protected] ~]# grep root /etc/passwd

[[email protected] ~]# grep Root /etc/passwd

[[email protected] ~]# grep -i Root /etc/passwd

//不嚴格區分大小寫過濾

[[email protected] ~]# dmesg | grep eth

//系統啟動時的硬體資訊

[[email protected] ~]# dmesg | grep sda

[[email protected] ~]#grep "^#" /etc/hosts

//過濾#開頭(註釋)的內容

[[email protected] ~]# grep -v "^#"/etc/hosts

[[email protected] ~]# grep "bash$" /etc/passwd

//過濾bash結尾的內容

[[email protected] ~]# grep -v "^#"/etc/xinetd.conf | grep -v "^$"

//顯示除#開頭和$結尾以外的內容

[[email protected] ~]# grep -vE "^#|^$"/etc/xinetd.conf

//擴充套件查詢模式 將管道應用到中間


[[email protected] ~]# grep -c "/bin/bash$" /etc/passwd

//匹配的行數

wKioL1PU-LrT28SQAAFWstSyse4009.jpg

wKiom1PU96Cisa9sAAEfq4HiyFs722.jpg

wKiom1PU96DTzRveAAD1sQoq8EI568.jpg

wKioL1PU-LrReDgUAAEXpAuK-dc593.jpg

wKiom1PU96Dhpgi0AAD9YwyGUCo345.jpg



轉載於:https://blog.51cto.com/erick0608/1530989