1. 程式人生 > >2017.10.15 bash基礎特性及基礎命令

2017.10.15 bash基礎特性及基礎命令

命令補全 命令歷史 路徑補全


一. 命令歷史,命令補全和路徑補全

環境變量:

HISTFILE:命令歷史記錄的條數

HISTSIZE:~/.bash.history

HISTFILESIZE:命令歷史文件記錄歷史的條數


history:默認顯示10條歷史紀錄

history -n:顯示後n條歷史記錄

history -d n:刪除第n條歷史紀錄

history -c:刪除所有歷史記錄

history -a:手動追加當前會話緩沖區中的歷史命令至歷史文件中


!n:重復執行第n條命令

!!:重復執行上一條命令

!加命令的開頭幾個字母:重復執行最近一次以這幾個字母開頭的命令


控制命令歷史的記錄方式:

環境變量:HISTCONTROL:

ignoredups:忽略連續重復的命令

ignorespace:忽略以空格開頭的命令

ignoreboth:ignoredups,ignorespace

修改環境變量值的方式:export 變量名=“值”

變量賦值:把賦值符號後面的數據存儲於變量名指向的內存空間;

[[email protected] ~]# history                         //查看命令歷史,默認顯示10條歷史紀錄
   28  history
   29  ls
   30  history | more
   31  history
   32  history | more
   33  ls
   34  cd
   35  ls
   36  cd /.bash.hisory
   37  history
[[email protected] ~]# history 5                       //查看最近5條命令歷史
   34  cd
   35  ls
   36  cd /.bash.hisory
   37  history
   38  history 5
[[email protected] ~]# history -d 36                   //刪除第36條命令歷史,
[[email protected] ~]# history 5                       //再次查看,發現已經被刪除了
   35  ls
   36  history
   37  history 5
   38  history -d 36
   39  history 5
[[email protected] ~]# history -c                      //刪除所有歷史
[[email protected] ~]# history
   31  history
[[email protected] ~]# history -a
[[email protected] ~]# 

[[email protected] ~]# cd /
[[email protected] /]# ls
123  boot   dev   lib         media  proc  selinux  tmp  var
abc  data   etc   lib64       mnt    root  srv      usr  xxl
bin  data1  home  lost+found  opt    sbin  sys      uzz
[[email protected] /]# cd 
[[email protected] ~]# ls
12      anaconda-ks.cfg     -n                             uzz
123     ett.txt             oldboy                         xxl
123456  install.log         oldboy.txt                     xxl.txt
abc     install.log.syslog  session192.168.80.1280320.log
[[email protected] ~]# history
   31  history
   32  history -a
   33  cd /
   34  ls
   35  cd 
   36  ls
   37  history
[[email protected] ~]# !33                             //重復執行第33條歷史命令
cd /
[[email protected] /]# cd
[[email protected] ~]# ls
12      anaconda-ks.cfg     -n                             uzz
123     ett.txt             oldboy                         xxl
123456  install.log         oldboy.txt                     xxl.txt
abc     install.log.syslog  session192.168.80.1280320.log
[[email protected] ~]# !!                              //重復執行上一條命令
ls
12      anaconda-ks.cfg     -n                             uzz
123     ett.txt             oldboy                         xxl
123456  install.log         oldboy.txt                     xxl.txt
abc     install.log.syslog  session192.168.80.1280320.log
[[email protected] ~]# !c                              //重復執行最近一次以c打頭的命令
cd
[[email protected] ~]# 

[[email protected] ~]# history
    2  cd /
    3  ls
    4  cd 
    5  ls
    6  history
    7  cd /
    8  cd
    9  ls
   10  cd
   11  history
[[email protected] ~]# ls
12      anaconda-ks.cfg     -n                             uzz
123     ett.txt             oldboy                         xxl
123456  install.log         oldboy.txt                     xxl.txt
abc     install.log.syslog  session192.168.80.1280320.log
[[email protected] ~]# ls
12      anaconda-ks.cfg     -n                             uzz
123     ett.txt             oldboy                         xxl
123456  install.log         oldboy.txt                     xxl.txt
abc     install.log.syslog  session192.168.80.1280320.log
[[email protected] ~]# history                            //默認重復執行的命令不被記錄
    4  cd 
    5  ls
    6  history
    7  cd /
    8  cd
    9  ls
   10  cd
   11  history
   12  ls
   13  history
[[email protected] ~]#  cd /
[[email protected] /]# 
[[email protected] /]# history
    6  history
    7  cd /
    8  cd
    9  ls
   10  cd
   11  history
   12  ls
   13  history
   14   cd /
   15  history
[[email protected] /]# export HISTCONTROL="ignorespace"          //設置以空格打頭的命令不被記錄
[[email protected] /]#  cd 
[[email protected] ~]# 
[[email protected] ~]# history
    8  cd
    9  ls
   10  cd
   11  history
   12  ls
   13  history
   14   cd /
   15  history
   16  export HISTCONTROL="ignorespace"
   17  history
[[email protected] ~]#


命令補全和路徑補全

linux中命令和路徑眾多,在使用過程中難免會忘記,bash shell提供的命令補全功能彌補了這一缺陷。只需要給出命令的前幾個字母,bash會根據PATH環境變量定義的路徑,從左到右查找以給定命令名命名的文件,第一次查找到的即為要運行的文件。

按tab鍵即可自動補全,如果還未不全,可再敲一次tab鍵,從給出的選項中選擇要執行的命令,或者再對給出一些提示。

[[email protected] ~]# e                 //輸入e按tab,發現以e打頭的命令有很多,再按一次tab,列出所有
e2freefrag             envsubst               eu-objdump
e2fsck                 eqn                    eu-ranlib
e2image                eqn2graph              eu-readelf
e2label                era_check              eu-size
e2undo                 era_dump               eu-stack
echo                   era_invalidate         eu-strings
ed                     esac                   eu-strip
editdiff               espdiff                eu-unstrip
edquota                ether-wake             eval
efibootmgr             ethtool                ex
egrep                  eu-addr2line           exec
eject                  eu-ar                  execstack
elif                   eu-elfcmp              exit
elinks                 eu-elflint             expand
else                   eu-findtextrel         export
enable                 eu-make-debug-archive  expr
env                    eu-nm                  
[[email protected] ~]# echo             //輸入ec,按回車,顯示echo

[[email protected] ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0  /敲tab補全

目錄管理類命令

1. mkdir
mkdir [選項] 目錄 創建目錄

參數:

-p 遞歸創建目錄

-m 對創建的目錄設置權限,-m,默認是755

2. rmdir
rmdir [選項] 目錄 刪除”空“目錄

-p 遞歸刪除

mkdir用法案例:
在根目錄/下創建uzz目錄
[[email protected] /]# mkdir uzz
[[email protected] /]# ls /
12   bin   data1  home   lost+found  opt   sbin     sys  uzz
123  boot  dev    lib    media       proc  selinux  tmp  var
abc  data  etc    lib64  mnt         root  srv      usr  xxl
[[email protected] /]#
在uzz目錄下創建xk目錄,再在xk目錄下創建wlgc目錄
[[email protected] uzz]# mkdir -p xk/wlgc
[[email protected] uzz]# ls
xk
[[email protected] uzz]# ls xk
wlgc
[[email protected] uzz]# 

[[email protected] 12]# mkdir -m 711 files               //創建files文件夾,設置目錄權限是711
[[email protected] 12]# ls -l
total 4
drwx--x--x 2 root root 4096 May  4 12:43 files
[[email protected] 12]# mkdir file                       //創建file文件夾
[[email protected] 12]# ls -l                            //列出詳細信息
total 8
drwxr-xr-x 2 root root 4096 May  4 12:44 file
drwx--x--x 2 root root 4096 May  4 12:43 files
[[email protected] 12]#                                  //可以看出,這兩個文件的權限是不一樣的

rmdir用法案例:
[[email protected] ~]# tree uzz
uzz
└── xxkx
    └── wlgc
        └── xxl

[[email protected] ~]# rmdir -p uzz/xxkx/wlgc
rmdir: failed to remove `uzz/xxkx/wlgc‘: Directory not empty
[[email protected] ~]# rmdir -p uzz/xxkx/wlgc/xxl
[[email protected] ~]# ls uzz
ls: cannot access uzz: No such file or directory
[[email protected] ~]#

三.文件查看命令
1. more和less
more的功能是將文件從第一行開始,根據輸出窗口的大小,適當的輸出文件內容。當一頁無法全部輸出時,可以用“回車鍵”向下翻行,用“空格鍵”向下翻頁。退出查看頁面,請按“q”鍵。另外,more還可以配合管道符“|”(pipe)使用,例如:ls -al | more

more的語法:more 文件名

Enter 向下n行,需要定義,默認為1行;

Ctrl f 向下滾動一屏;

空格鍵 向下滾動一屏;

Ctrl b 返回上一屏;

= 輸出當前行的行號;

:f 輸出文件名和當前行的行號;

v 調用vi編輯器;

! 命令 調用Shell,並執行命令;

q 退出more


less的功能和more相似,但是使用more無法向前翻頁,只能向後翻。

less可以使用【pageup】和【pagedown】鍵進行前翻頁

less的語法:less 文件名

less還有一個功能,可以在文件中進行搜索你想找的內容,假設你想在passwd文件中查找有沒有weblogic字符串,那麽你可以這樣來做:

[[email protected] etc]# less passwd

然後輸入:

/weblogic

回車

此時如果有weblogic字符串,linux會把該字符已高亮方式顯示。

退出查看頁面,請按“q”鍵。


2. head和tail

head和tail通常使用在只需要讀取文件的前幾行或者後幾行的情況下使用。

head的功能是顯示文件的前幾行內容
head -4 a.txt 顯示前4行內容
tail的功能恰好和head相反,只顯示最後幾行內容
tail -5 b.txt 顯示後5行內容

[[email protected] ~]# head -5 install.log
Installing libgcc-4.4.7-16.el6.x86_64
warning: libgcc-4.4.7-16.el6.x86_64: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Installing setup-2.8.14-20.el6_4.1.noarch
Installing filesystem-2.4.30-3.el6.x86_64
Installing basesystem-10.0-4.el6.noarch
[[email protected] ~]# tail -5 install.log
Installing ipw2100-firmware-1.3-11.el6.noarch
Installing ql23xx-firmware-3.03.27-3.1.el6.noarch
Installing ipw2200-firmware-3.1-4.el6.noarch
Installing rootfiles-8.1-6.1.el6.noarch
*** FINISHED INSTALLING PACKAGES ***
[[email protected] ~]#

四. 時間戳管理命令

touch 創建文件或更新文件的時間戳
格式:touch 參數 文件名

參數:

-c :不創建不存在文件

-m:只更新修改時間,不更新訪問時間

-a:只更新訪問時間,不更新修改時間

-r file:使用文件file的時間更新文件的時間

-t:將時間修改為參數指定的日期,如01091135為 1月9日 11:35


補充知識:查看文件信息(創建日期):ls -l 查看文件信息(訪問日期):ls -lu

[[email protected] 123]# tree                      //交待實驗環境,該文件夾下有以下文件夾和文件
.
├── 1234
│   └── 12345
├── 1.txt
├── 2
├── 2.txt
└── 3.txt

2 directories, 4 files
[[email protected] 123]# touch -c a.txt          //如果有a.txt,更新其時間戳,如果沒有,也不要創建
[[email protected] 123]# ls
1234  1.txt  2  2.txt  3.txt
[[email protected] 123]# ls -l 1.txt
-rw-r--r--. 1 root root 9 May  4 15:04 1.txt
[[email protected] 123]# touch -c 1.txt
[[email protected] 123]# ls -l 1.txt
-rw-r--r--. 1 root root 9 May  4 16:21 1.txt
[[email protected] 123]# ls -lu 1.txt
-rw-r--r--. 1 root root 9 May  4 16:21 1.txt
[[email protected] 123]# touch -a 1.txt
[[email protected] 123]# ls -lu 1.txt
-rw-r--r--. 1 root root 9 May  4 16:23 1.txt
[[email protected] 123]# ls -l 2.txt
-rw-r--r--. 1 root root 9 Apr  5 05:17 2.txt
[[email protected] 123]# touch -r 2.txt 1.txt
[[email protected] 123]# ls -l 2.txt
-rw-r--r--. 1 root root 9 Apr  5 05:17 2.txt
[[email protected] 123]# touch -t 03301259 1.txt
[[email protected] 123]# ls -l 1.txt
-rw-r--r--. 1 root root 9 Mar 30 12:59 1.txt
[[email protected] 123]#



本文出自 “技術成就夢想” 博客,請務必保留此出處http://xuxiaoliang.blog.51cto.com/10882951/1972480

2017.10.15 bash基礎特性及基礎命令