Linux常用命令(8)–bash應用技巧

來源:互聯網
上載者:User

查看系統存在的shell

[root@localhost ~]# cat /etc/shells/bin/sh/bin/bash/sbin/nologin/bin/tcsh/bin/csh[root@localhost ~]# 

 

  bash中常用的快速鍵:

    ctrl+l 清屏、crtl+u 刪除命令列游標前面的所有內容

1、命令補齊

  命令補齊允許使用者輸入檔案起始的若干個字母后,按<Tab>鍵補齊檔案名稱

  這在軟體包安裝的時候非常有用,因為linux中軟體包的命令都很長,一個個字母敲容易出錯,也很麻煩

2、命令曆史

  命令曆史允許使用者瀏覽先前輸入的命令並重新調用它們,用history命令可以顯示使用過的命令列表,按方向鍵可以尋找以前執行過的命令

[root@localhost abc]# history 1005  ping -s 1024 192.168.1.1 1006  ping -c 4 -s 1024 192.168.1.1 1007  ping -s 60000000 192.168.1.1 1008  clear 1009  ifconfig 1010  clear 1011  history 1012  clear 1013  cat /etc/shells 1014  cd bac 1015  cd /abc 1016  ls 1017  history[root@localhost abc]# 

  使用!+命令前的序號,就可以調用相應的命令

[root@localhost abc]# !1016lsadir      afile      afile.zip   issue.soft  test         test.zipadir.tar  afile.bz2  issue.hard  prem        test.tar.gz[root@localhost abc]# 

 

3、命令別名

  範例: alias copy=cp   使用的時候就可以用copy替代cp

      alias drm="rm -rf"  命令的組合,需要用雙引號括起來

  查看別名資訊:alias

  刪除別名:unalias copy

查看系統別名[root@localhost ~]# aliasalias cp='cp -i'alias l.='ls -d .* --color=auto'alias ll='ls -l --color=auto'alias ls='ls --color=auto'alias mv='mv -i'alias rm='rm -i'alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'[root@localhost ~]# 定義別名drm="rm -rf"[root@localhost ~]# alias drm="rm -rf"使用別名刪除test目錄[root@localhost ~]# drm test刪除別名drm[root@localhost ~]# unalias drm

 

4、輸入/輸出重新導向

  Shell對於每一個進程預先定義3個檔案描述字(0、1、2),分別低對應於:

    0  (STDIN)標準輸入

    1  (STDOUT)標準輸出

    2  (STDERR)標準錯誤輸出

  1)> 或 >> 輸出重新導向

    範例:ls -l /tmp > /tmp.msg

       date >> /tmp.msg

輸出重新導向,ls -l /tmp命令的結果資訊不顯示到螢幕上,而是輸出到tmp.msg檔案中[root@localhost ~]# ls -l /tmp > /tmp.msg我們查看一下tmp.msg檔案中的內容[root@localhost ~]# cat /tmp.msgtotal 100drwx------. 2 luxh luxh  4096 Nov 14 04:42 keyring-h3yH6mdrwx------. 2 luxh luxh  4096 Nov 22 05:09 keyring-YG0Kwldrwx------. 2 gdm  gdm   4096 Nov 22 05:08 orbit-gdmdrwx------. 2 luxh luxh  4096 Nov 22 05:09 orbit-luxhdrwx------. 2 gdm  gdm   4096 Nov 22 05:09 pulse-84tzFLbuLWkGdrwx------. 2 luxh luxh  4096 Nov 22 05:09 pulse-Ijt6UMdvv23Edrwx------. 2 root root  4096 May 16  2012 pulse-ztOQIKtvdyh9drwx------. 2 luxh luxh  4096 Nov 22 05:09 virtual-luxh.2MU7ZXdrwx------. 2 luxh luxh  4096 Nov 13 06:16 virtual-luxh.DEg7dTdrwx------. 2 luxh luxh  4096 Nov 20 04:29 virtual-luxh.eKfxnYdrwx------. 2 luxh luxh  4096 Nov 16 18:16 virtual-luxh.q0XCV9drwx------. 2 luxh luxh  4096 Nov 15 04:15 virtual-luxh.Uib40ldrwx------. 2 luxh luxh  4096 Nov 21 05:07 virtual-luxh.yq7GZgdrwx------. 2 luxh luxh  4096 Nov 14 04:45 virtual-luxh.zu4sqydrwxrwxrwt. 2 root root  4096 Nov 15 04:14 VMwareDnDdrwx------. 2 luxh luxh  4096 Nov 22 05:09 vmware-luxhdrwx------. 2 root root 36864 Nov 22 05:07 vmware-root[root@localhost ~]# 

 

把date命令的結果資訊追加到tmp.msg[root@localhost ~]# date >> /tmp.msg[root@localhost ~]# cat /tmp.msgtotal 100drwx------. 2 luxh luxh  4096 Nov 14 04:42 keyring-h3yH6mdrwx------. 2 luxh luxh  4096 Nov 22 05:09 keyring-YG0Kwldrwx------. 2 gdm  gdm   4096 Nov 22 05:08 orbit-gdmdrwx------. 2 luxh luxh  4096 Nov 22 05:09 orbit-luxhdrwx------. 2 gdm  gdm   4096 Nov 22 05:09 pulse-84tzFLbuLWkGdrwx------. 2 luxh luxh  4096 Nov 22 05:09 pulse-Ijt6UMdvv23Edrwx------. 2 root root  4096 May 16  2012 pulse-ztOQIKtvdyh9drwx------. 2 luxh luxh  4096 Nov 22 05:09 virtual-luxh.2MU7ZXdrwx------. 2 luxh luxh  4096 Nov 13 06:16 virtual-luxh.DEg7dTdrwx------. 2 luxh luxh  4096 Nov 20 04:29 virtual-luxh.eKfxnYdrwx------. 2 luxh luxh  4096 Nov 16 18:16 virtual-luxh.q0XCV9drwx------. 2 luxh luxh  4096 Nov 15 04:15 virtual-luxh.Uib40ldrwx------. 2 luxh luxh  4096 Nov 21 05:07 virtual-luxh.yq7GZgdrwx------. 2 luxh luxh  4096 Nov 14 04:45 virtual-luxh.zu4sqydrwxrwxrwt. 2 root root  4096 Nov 15 04:14 VMwareDnDdrwx------. 2 luxh luxh  4096 Nov 22 05:09 vmware-luxhdrwx------. 2 root root 36864 Nov 22 05:07 vmware-rootThu Nov 22 06:59:05 PST 2012[root@localhost ~]#

  2)< 輸入重新導向

    範例:wall < user.info

[root@localhost ~]# cat user.infoamani1::1001:503::/home/amani1:/bin/bash amani2::1002:503::/home/amani2:/bin/bash把user.info檔案中的內容作為輸入進行廣播[root@localhost ~]# wall < user.info[root@localhost ~]# Broadcast message from root@localhost.localdomain (Thu Nov 22 07:07:56 2012):amani1::1001:503::/home/amani1:/bin/bash amani2::1002:503::/home/amani2:/bin/bash

  3)2> 錯誤輸出重新導向

    範例:cp -R /usr /backup/usr.bak 2> /bak.error  備份/usr目錄,如果出錯,將錯誤資訊輸出到bak.error

查看一個不存在的目錄,會在螢幕上提示錯誤資訊[root@localhost ~]# ls /aaaals: cannot access /aaaa: No such file or directory將錯誤資訊重新導向到ls.err檔案[root@localhost ~]# ls /aaaa 2> ls.err[root@localhost ~]# cat ls.errls: cannot access /aaaa: No such file or directory[root@localhost ~]#

 

5、管道

  將一個命令的輸出傳送給另一個命令,作為另一個命令的輸入。

  使用方法:

    命令1|命令2|命令3|......|命令n

  如:

    ls -l /etc | more

    ls -l /etc | grep init

    ls -l /etc | grep init | wc -l

將ls -l /etc 命令輸出的內容傳給more命令分頁瀏覽[root@localhost ~]# ls -l /etc | moretotal 1720drwxr-xr-x.   3 root root   4096 May  6  2012 abrtdrwxr-xr-x.   4 root root   4096 May  6  2012 acpi-rw-r--r--.   1 root root     45 Nov 22 08:19 adjtime-rw-r--r--.   1 root root   1512 Jan 12  2010 aliases-rw-r--r--.   1 root root  12288 May  6  2012 aliases.dbdrwxr-xr-x.   2 root root   4096 May  6  2012 alsadrwxr-xr-x.   2 root root   4096 May  6  2012 alternatives-rw-r--r--.   1 root root    541 Jul 18  2011 anacrontab-rw-r--r--.   1 root root    148 May 14  2009 asound.conf-rw-r--r--.   1 root root      1 Jun 24  2011 at.denydrwxr-x---.   3 root root   4096 May  6  2012 audispdrwxr-x---.   2 root root   4096 May  6  2012 auditdrwxr-xr-x.   4 root root   4096 May  6  2012 avahidrwxr-xr-x.   2 root root   4096 May  6  2012 bash_completion.d-rw-r--r--.   1 root root   2681 Sep 23  2011 bashrcdrwxr-xr-x.   2 root root   4096 Dec  7  2011 blkiddrwxr-xr-x.   2 root root   4096 May  6  2012 bluetoothdrwxr-xr-x.   2 root root   4096 May  6  2012 bonobo-activation-rw-r--r--.   1 root root     27 Dec  8  2011 centos-releasedrwxr-xr-x.   2 root root   4096 Nov 10  2010 chkconfig.ddrwxr-xr-x.   5 root root   4096 May  6  2012 ConsoleKitdrwxr-xr-x.   2 root root   4096 May  6  2012 cron.d--More--將ls -l /etc 命令輸出的內容傳給grep命令尋找出含有init的行[root@localhost ~]#  ls -l /etc | grep initdrwxr-xr-x.   2 root root   4096 May  6  2012 initlrwxrwxrwx.   1 root root     11 May  6  2012 init.d -> rc.d/init.d-rw-r--r--.   1 root root    884 May  5  2012 inittablrwxrwxrwx.   1 root root     15 May  6  2012 rc.sysinit -> rc.d/rc.sysinit[root@localhost ~]# 將ls -l /etc 命令輸出的內容傳給grep命令尋找出含有init的行,再把grep命令輸出的內容傳送給wc -l命令進行統計有多少行[root@localhost ~]# ls -l /etc | grep init | wc -l4[root@localhost ~]# 

 

6、命令串連符

  1);  用;間隔的各命令按順序依次執行

例如,顯示當前所在目錄,然後顯示目前的目錄有什麼檔案,最後顯示系統時間[root@localhost ~]# pwd;ls;date/root2012             Documents           Music                Templatesa b              Downloads           Pictures             testabc              install.log         Public               user.infoanaconda-ks.cfg  install.log.syslog  sudo-1.8.4p4         VideosDesktop          ls.err              sudo-1.8.4p4.tar.gzFri Nov 23 05:34:04 PST 2012[root@localhost ~]#

 

  2)&&  前後命令的執行存在邏輯與關係,只有&&前面的命令執行成功後,它後面的命令才被執行

例如,給Lily發送一封情書,情書發送成功就刪掉[root@localhost ~]# write Lily < /root/love.txt && rm /root/love.txt先執行ls命令,執行成功後再執行pwd[root@localhost ~]# ls && pwd2012             Documents           Music                Templatesa b              Downloads           Pictures             testabc              install.log         Public               user.infoanaconda-ks.cfg  install.log.syslog  sudo-1.8.4p4         VideosDesktop          ls.err              sudo-1.8.4p4.tar.gz/root[root@localhost ~]# 先執行la命令,由於沒有la命令,就會執行出錯,後面的pwd命令就不會執行[root@localhost ~]# la && pwdbash: la: command not found[root@localhost ~]# 

 

  3)||  前後命令的執行存在邏輯或關係,只有||前面的命令執行失敗後,它後面的命令才被執行

先執行ls命令,執行成功,後面的pwd命令就不會執行了[root@localhost ~]# ls || pwd2012             Documents           Music                Templatesa b              Downloads           Pictures             testabc              install.log         Public               user.infoanaconda-ks.cfg  install.log.syslog  sudo-1.8.4p4         VideosDesktop          ls.err              sudo-1.8.4p4.tar.gz先執行la命令,由於沒有la命令,就會執行出錯,後面的pwd命令就會執行[root@localhost ~]# la || pwdbash: la: command not found/root[root@localhost ~]#

 

7、命令替換符

  將一個命令的輸出作為另一個命令的參數

  格式:命令1 `命令2`   `是鍵盤上數字1左邊的那個撇號

  如:ls -l `which touch`

which touch命令的輸出就作為 ls -l 的參數
[root@localhost ~]# ls -l `which touch`-rwxr-xr-x. 1 root root 50644 Dec 7 2011 /bin/touch[root@localhost ~]#

 

 

 

 

 

   

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.