標籤:
Linux系統龐大而複雜,經過總結,做為一名DBA,一些基本管理、網路管理、進階應用、效能診斷、管道複雜應用、硬體相關感測命令都需要掌握,這篇文章,專門來講做為一名DBA,必須學會的11個基本應用程式命令。
1、 ls命令
Ls命令是Linux中最常用的命令,是list的縮寫,預設下ls命令是列出目前的目錄的清單,如果ls指定其他目錄,就會顯示指定目錄裡的檔案及檔案夾清單。
常用用法(常用參數):
(1) ls –l
以長格式(每行只顯示一個檔案或目錄資訊)列出檔案及目錄的詳細資料,輸出結果如下:
[[email protected] single]$ ls -l
total 14091544
-rw-r-----. 1 oracle oinstall 5368717312 Apr 10 10:50 sysaux02.dbf
-rw-r-----. 1 oracle oinstall 9061015552 Apr 10 08:05 users01.dbf
(2) ls –a
列出目錄下的所有檔案及目錄,輸出結果如下:
[[email protected] single]$ ls -a
. .. sysaux02.dbf users01.dbf
(3) ls –s
列出目錄佔用空間總大小,以及各個檔案名稱及其大小(單位為KB),輸出結果如下:
[[email protected] single]$ ls -s
total 14091544
5242892 sysaux02.dbf 8848652 users01.dbf
(4) ls –lh
以長格式(每行只顯示一個檔案或目錄資訊),並且以直觀形式顯示檔案及目錄總大小,輸出結果如下:
[[email protected] single]$ ls -lh
total 14G
-rw-r-----. 1 oracle oinstall 5.1G Apr 10 10:50 sysaux02.dbf
-rw-r-----. 1 oracle oinstall 8.5G Apr 10 08:05 users01.dbf
(5) ls –lrt
以長格式(每行只顯示一個檔案或目錄資訊),並用對檔案及目錄按照最時間進行排序,輸出結果如下:
[[email protected] single]$ ls -lrt
total 14091544
-rw-r-----. 1 oracle oinstall 9061015552 Apr 10 08:05 users01.dbf
-rw-r-----. 1 oracle oinstall 5368717312 Apr 10 10:50 sysaux02.dbf
(6) ls –lrth
與“ls –lrt”相比,將檔案大小以更直觀的方式顯示出來,輸出結果如下:
[[email protected] single]$ ls -lrth
total 14G
-rw-r-----. 1 oracle oinstall 8.5G Apr 10 08:05 users01.dbf
-rw-r-----. 1 oracle oinstall 5.1G Apr 10 10:50 sysaux02.dbf
2、 pwd命令
pwd命令是Print Working Directory的縮寫,準系統是列印當前的工作目錄。
常用用法(常用參數):
(1) pwd
顯示當前所處目錄絕對路徑,輸出結果如下:
[[email protected] oradata]$ pwd
/u01/oradata
(2) pwd -p
顯示當前所處目錄的實體路徑。因為有些目錄是link後的結果,用-P可以顯示link的源頭路徑,輸出結果如下:
[email protected] oradata]$ cd /etc/init.d
[[email protected] init.d]$ pwd
/etc/init.d
[[email protected] init.d]$ pwd -P
/etc/rc.d/init.d
3、 cd命令
cd命令是linux中最常用的命令之一,用於切換目錄路徑
常用用法(常用參數):
(1) cd
回到自己的HOME目錄,輸出結果如下:
[[email protected] init.d]$ pwd
/etc/init.d
[[email protected] init.d]$ cd
[[email protected] ~]$ pwd
/home/oracle
(2) cd –
返回進入此目錄之前所在的目錄,輸出結果如下:
[[email protected] ~]$ pwd
/home/oracle
[[email protected] ~]$ cd /u01/oradata/single
[[email protected] single]$ pwd
/u01/oradata/single
[[email protected] single]$ cd -
/home/oracle
[[email protected] ~]$ pwd
/home/oracle
(3) cd ..
返回到上一級目錄,輸出結果如下:
[[email protected] ~]$ pwd
/home/oracle
[[email protected] ~]$ cd ..
[[email protected] home]$ pwd
/home
(4) cd ../..
返回上兩級目錄,輸出結果如下:
[[email protected] single]$ pwd
/u01/oradata/single
[[email protected] single]$ cd ../..
[[email protected] u01]$ pwd
/u01
(5) cd /u01
切換到指定的目錄路徑,輸出結果如下:
[[email protected] /]$ cd /u01
[[email protected] u01]$ pwd
/u01
4、 grep命令
grep 是global search regular expression(RE)的縮寫,是一種強大的文本搜尋工具,它能使用Regex搜尋文本,並把匹配的行列印出來。
(1) grep命令用法:
grep [OPTIONS] PATTERN [FILE...]
grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]
(2) 常用的參數(OPTIONS)
-a :將 binary 檔案以 text 檔案的方式搜尋資料
-c :計算找到 ‘搜尋字串‘ 的次數
-i :忽略大小寫不同,所以大小寫視為相同
-n :順便輸出行號
-v :反向選擇,亦即顯示出沒有 ‘搜尋字串‘ 內容的那一行
-s:不顯示不存在或無匹配文本的所有行
--color:將找到的關鍵詞部分加上顏色顯示(值有:never、always、auto三種)
(3) 常用的Regex(PATTERN)
\:忽略Regex中特殊字元的原有含義。
^:匹配Regex的開始行。
$: 匹配Regex的結束行。
\<:從匹配正則表達 式的行開始。
\>:到匹配Regex的行結束。
[ ]:單個字元,如[A]即A符合要求 。
[ - ]:範圍,如[A-Z],即A、B、C一直到Z都符合要求 。
。:所有的單個字元。
* :有字元,長度可以為0。
(4) 簡單使用執行個體
常用用法或常用參數:
將包含passwd字元的檔案及其所在行顯示出來:
[[email protected] admin]$ grep -n passwd *
catexp7.sql:170: (name, userid, passwd, defrole, datats, tempts, profile#,
catzxs.sql:364: tmp := DBMS_XDB.CreateResource(‘/sys/xs/roles/dbms_passwd.xml‘,XSAUTHXSD);
csminst.sql:25:rem ywu 02/19/04 - fix bug 3434808, delete hard code passwd.
將包含0-9數位行取出來:
[[email protected] admin]$ grep -n ‘[0-9]‘ xsu111.sql
2:Rem $Header: rdbms/admin/xsu111.sql /main/13 2010/06/06 21:49:30 snadhika Exp $
4:Rem xsu111.sql
6:Rem Copyright (c) 2007, 2010, Oracle and/or its affiliates.
5、 cat
cat是一個文字檔查看和串連工具。查看一個檔案的內容,用cat比較簡單,就是cat 後面直接接檔案名稱。
常用用法(參數):
(1)cat /檔案名稱
最簡單的cat用法,原原本本的列印出整個檔案的全部內容
(3) cat –b /檔案名稱
列印出整個檔案的內容,並且對非空白行進行編號,行號從1開始,輸出結果如下所示:
[[email protected] admin]$ cat -b xsu111.sql |more
1 Rem
2 Rem $Header: rdbms/admin/xsu111.sql /main/13 2010/06/06 21:49:30 snadhika Exp $
3 Rem
4 Rem xsu111.sql
5 Rem
……
6、 more
類似 cat 命令,不過會以一頁一頁的顯示方便使用者逐頁閱讀,而最基本的指令就是按空格鍵(space)就往下一頁顯示,按 b 鍵就會往回(back)一頁顯示,而且還有搜尋字串的功能(與 vi 相似)
[[email protected] admin]$ more test.txt
dddddddddddddddddddddddddddddd
7、 echo
Echo命令是在螢幕上顯示字元或變數的值。
常用用法:
(1)輸出字串
將要輸出的字串,用雙引號引起來,輸出結果如下所示:
[email protected] admin]$ echo "please wait 2 minute"
please wait 2 minute
(2)輸出變數值
輸出$ORACLE_HOME環境變數的值,輸出結果如下所示:
[[email protected] admin]$ echo $ORACLE_HOME
/dba/oracle/product/11.2.0/db_1
8、 hostname
顯示和設定當前主機系統的名稱,只有具有ROOT許可權的使用者才能設定主機名稱。
常用用法:
(1) 顯示主機名稱:
[[email protected] admin]$ hostname
ol6-single
(2) 解析主機名稱所對應的IP地址:
前提是/etc/hosts中有編加主機名稱對應IP地址的資訊,或DNS可以正常解析,輸出結果如下:
[[email protected] ~]# hostname -i
192.168.2.150
(3) 臨時設定主機名稱:
臨時設定主機名稱,修改後,/etc/sysconfig/network檔案中的HOSTNAME值仍然未改變,要想永久修改主機名稱,還需要修改/etc/sysconfig/network檔案中hostname的值,使用方法如下:
[[email protected] ~]# hostname lijunjie
[[email protected] ~]# hostname
lijunjie
9、 touch
Touch命令用於修訪問和變更檔的時間到目前時間或指定時間,或者建立一個不存在的檔案
常用用法:
(1)touch 檔案名稱
用於建立一個新檔案,如果檔案名稱已經存在,則修改檔案的修改時間為當前系統時間
[[email protected] admin]$ ls -l test.txt
-rw-r--r--. 1 oracle oinstall 31 Apr 11 05:18 test.txt
[[email protected] admin]$ date
Sat Apr 11 05:20:01 CST 2015
[[email protected] admin]$ touch test.txt
[[email protected] admin]$ ls -l test.txt
-rw-r--r--. 1 oracle oinstall 31 Apr 11 05:20 test.txt
(2)touch –r 源檔案名稱 目標檔案名
將源檔案的時間,更新到目標檔案上,使兩個檔案的時間相同
[[email protected] admin]$ ls -l
-rw-r--r--. 1 oracle oinstall 363 Sep 10 2014 listener.ora
-rw-r--r--. 1 oracle oinstall 31 Apr 11 05:20 test.txt
[[email protected] admin]$ touch -r listener.ora test.txt
[[email protected] admin]$ ls -lrt
-rw-r--r--. 1 oracle oinstall 31 Sep 10 2014 test.txt
-rw-r--r--. 1 oracle oinstall 363 Sep 10 2014 listener.ora
(4) touch –t yyyymmddhhmi.ss
將檔案的時間修改成指定的年、月、日、小時、分.秒
[[email protected] admin]$ ls -l test.txt
-rw-r--r--. 1 oracle oinstall 31 Sep 10 2014 test.txt
[[email protected] admin]$ touch -t 201101012050.50 test.txt
[[email protected] admin]$ ls -l test.txt
-rw-r--r--. 1 oracle oinstall 31 Jan 1 2011 test.txt
10、 df
Df命令用於顯示檔案系統與目錄的詳細資料。
常用用法:
(1) df –a
列出所有的檔案系統與掛載點,輸出結果如下:
[[email protected] admin]$ df -a
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda3 10321208 3403240 6393680 35% /
proc 0 0 0 - /proc
sysfs 0 0 0 - /sys
devpts 0 0 0 - /dev/pts
tmpfs 507124 0 507124 0% /dev/shm
/dev/sda1 198337 50193 137904 27% /boot
/dev/sda2 39015880 27626000 9407928 75% /dba
none 0 0 0 - /proc/sys/fs/binfmt_misc
sunrpc 0 0 0 - /var/lib/nfs/rpc_pipefs
/dev/sdb1 51605436 15703408 33280624 33% /u01
(2)df -h
列出已有使用的檔案系統與掛載點與便於識別的大小資訊,便於識別的大小資訊為K、M、G格式,輸出結果如下所示;
[[email protected] admin]$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 9.9G 3.3G 6.1G 35% /
tmpfs 496M 0 496M 0% /dev/shm
/dev/sda1 194M 50M 135M 27% /boot
/dev/sda2 38G 27G 9.0G 75% /dba
/dev/sdb1 50G 15G 32G 33% /u01
11 vim|vi
例似Windows上的記事本,用於編輯檔案中的內容或建立一個新檔案。功能強大,做為DBA,必須相閱相關資料掌握,在此不詳述。
做DBA必須學會的11個Linux基本命令--主要看參數