作為一名 Python 開發者,僅僅會 Python 是遠遠不夠的。Linux 也是我們經常需要使用的一門語言。
在這篇文章,我梳理了11個基本應用命令,也是必須學會的。下面分享給大家。
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
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
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
grep 是global search regular expression(RE)的縮寫,是一種強大的文本搜索工具,它能使用正則表達式搜索文本,並把匹配的行打印出來。
(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) 常用的正則表達式(PATTERN)
\:忽略正則表達式中特殊字符的原有含義。
^:匹配正則表達式的開始行。
$: 匹配正則表達式的結束行。
<:從匹配正則表達 式的行開始。
>:到匹配正則表達式的行結束。
[ ]:單個字符,如[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.
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
……
類似 cat 命令,不過會以一頁一頁的顯示方便使用者逐頁閱讀,而最基本的指令就是按空格鍵(space)就往下一頁顯示,按 b 鍵就會往回(back)一頁顯示,而且還有搜尋字串的功能(與 vi 相似)
[[email protected] admin]$ more test.txt
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
顯示和設置當前主機系統的名稱,只有具有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
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
[ora[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
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
例似Windows上的記事本,用於編輯文件中的內容或新建一個新文件。
掌握好這11個Linux基本命令,做一個合格的Python開發者!
How to modify the horizontal and vertical axis scales of python line charts?
p1 is the current effectp2 is
Python reports an error when running, but it works fine on other computers, but it doesnt work if you change the computer. What is the reason for this?
Python runs an error, other co