日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

8.1-8.5 shell介绍,命令历史,命令补全和别名,通配符,输入输出重定向

發布時間:2025/3/20 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 8.1-8.5 shell介绍,命令历史,命令补全和别名,通配符,输入输出重定向 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

8.1?shell介紹
8.2?命令歷史
8.3?命令補全和別名
8.4?通配符
8.5?輸入輸出重定向



8.1 shell介紹


Linux Shell基礎

介紹shell的特性,用法。

shell是一個命令解釋器,提供用戶和機器之間的交互。


支持特定語法,比如邏輯判斷、循環。


每個用戶都可以有自己特定的shell


CentOS7默認shell為bash(Bourne Agin Shell)

Bourne是一個用戶,他開發的shell是sh.

后來CentOS7默認使用的shell是bash,基于sh優化而來。


shell還有zsh、ksh等



8.2 命令歷史


history命令

.bash_historyhistory文件,存放命令

# cat /root/.bash_history?


歷史命令最大可以存1000條 (要修改最大值,可以修改/etc/profile文件)

查看變量HISTSIZE值,?

# echo $HISTSIZE?

1000

有時候敲命令,可以敲到1000+ ,這是因為這些命令還沒有真正寫到文件里,因為現在運行的命令是暫時存在內存中。


#history -c?

清空當前命令歷史,不能清空相關歷史配置文件的,對配置文件無任何影響。只有退出終端,命令歷史記錄才會被寫入文件中。


要改變$HISTSIZE,需在/etc/profile中修改

修改profile中的history參數之后,需要讓修改生效,利用#source /etc/profile?

[root@centos7 ~]# vi /etc/profile

[root@centos7 ~]# echo $HISTSIZE

1000

[root@centos7 ~]# source /etc/profile

[root@centos7 ~]# echo $HISTSIZE?

5000


用時間顯示使用過的命令歷史記錄。

使用參數:HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "

[root@centos7 ~]# echo HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "Y年m月d日H時M分鐘S秒

HISTTIMEFORMAT=%Y/%m/%d %H:%M:%S ?

[root@centos7 ~]# vi /etc/profile修改profile文件

[root@centos7 ~]# source /etc/profile使參數修改生效

[root@centos7 ~]# history?

? ? 6 ?2018/03/07 15:08:26 history?

? ? 7 ?2018/03/07 15:08:32 ls

? ? 8 ?2018/03/07 15:08:33 history?

? ? 9 ?2018/03/07 15:09:16 cd /etc/

? ?10 ?2018/03/07 15:09:19 cd -

? ?11 ?2018/03/07 15:09:23 history?



?

用久保存 chattr +a ~/.bash_history

如果不正常退出,保存的命令會不全。


!! 上一條命令,則history的最后一條命令。


!n ?!接n條命令。例如!6 執行history的第6條命令


!word 從history倒退網上找最近一條相關word命令。例如!echo,從history里找最后一條echo的命令。


# history?

? ? ? 26 ?2018/03/07 16:02:29 history?

? ?27 ?2018/03/07 16:04:17 echo HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "?

? ?28 ?2018/03/07 16:04:29 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "

? ?29 ?2018/03/07 16:06:06 history?

# !echo

echo HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "?

HISTTIMEFORMAT=%Y/%m/%d %H:%M:%S?



8.3 命令補全和別名


命令補全及別名

tab鍵,敲一下,敲兩下

敲一下補全效果,

敲兩下,列出相關補全文件或目錄名


系統參數命令在未安裝bash-completion之前,是不能補全的,例如敲入:

#systemctl restart network.service 會發現未安裝之前是不能全部敲入的。


參數補全,安裝bash-completion,安裝完成后重啟。

#yum install -y bash-completion

# rpm -qa bash-completion安裝成功,

bash-completion-2.1-6.el7.noarch


alias別名給命令重新起個名字

給systemctl restart network.service做一個alias,名為:restartnet

[root@centos7 ~]# alias restartnet='systemctl restart network.service'

[root@centos7 ~]# restartnet?

[root@centos7 ~]# alias查看系統有哪些別名?

alias cp='cp -i'

alias egrep='egrep --color=auto'

alias fgrep='fgrep --color=auto'

alias grep='grep --color=auto'

alias l.='ls -d .* --color=auto'

alias ll='ls -l --color=auto'

alias ls='ls --color=auto'

alias mv='mv -i'

alias restartnet='systemctl restart network.service'

alias rm='rm -i'

alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'



alias存放目錄?


各用戶都有自己配置別名的文件 ~/.bashrc


ls /etc/profile.d/


自定義的alias放到 ~/.bashrc


~/.bashrc 部分在這

/etc/profile.d/ ?grep,colors


#unalias restartnet取消restartnet的alias



8.4 通配符


介紹

*用法

[root@centos7 ~]# ls

1.txt ?1.xtx ?2.txt ?a_(2).txt ?anaconda-ks.cfg ?anaconda-ks.cfg.1 ?a.txt ?temp.1

[root@centos7 ~]# ls *txt目錄下所有TXT文件

1.txt ?2.txt ?a_(2).txt ?a.txt

[root@centos7 ~]# ls 1*txt目錄下1開頭的TXT文件

1.txt

[root@centos7 ~]# ls *2*.txt目錄下名稱含有2的TXT文件

2.txt ?a_(2).txt


?用法

[root@centos7 ~]# touch 3.txt 4.txt a.txt bb.txt

[root@centos7 ~]# ls

1.txt ?1.xtx ?2.txt ?3.txt ?4.txt ?a_(2).txt ?anaconda-ks.cfg ?anaconda-ks.cfg.1 ?a.txt ?bb.txt ?temp.1

[root@centos7 ~]# ls ?.txt文件名只有一個字的TXT文件名

1.txt ?2.txt ?3.txt ?4.txt ?a.txt

[root@centos7 ~]# ls ??.txt文件名有兩個字的TXT文件名

bb.txt


[]數字范圍用法,支持特定數字,也支持特定范圍。

[root@centos7 ~]# ls [0-4].txt

1.txt ?2.txt ?3.txt ?4.txt

[root@centos7 ~]# ls [14].txt

1.txt ?4.txt

[root@centos7 ~]# ls [1234].txt

1.txt ?2.txt ?3.txt ?4.txt

英文和數字用法

[root@centos7 ~]# touch A.txt

[root@centos7 ~]# ls [0-9a-zA-Z].txt

1.txt ?2.txt ?3.txt ?4.txt ?a.txt ?A.txt


{}用法,例如{1,2,3}=[1-3]

[root@centos7 ~]# ls {1,2,3}.txt

1.txt ?2.txt ?3.txt

[root@centos7 ~]# ls [1-3].txt

1.txt ?2.txt ?3.txt



8.5 輸入輸出重定向



輸出重定向>


cat 1.txt >2.txt把1.txt輸出的內容結果重定向輸入到2.txt的文件里。

cat 1.txt >>2.txt>>和>差不多,只是>>是追加,>>不把1.txt的文件內容刪掉,會保留著,并且輸入到2.txt,這個動作叫做追加。

>把前面輸出的內容結果,重定向輸入到后面的文件里。(前面被重定向的內容會被刪掉)



ls aaa.txt 2>4.txt lsaaa.txt 產生的錯誤信息指定輸出到4.txt里

[root@centos7 ~]# ls aaa.txt 2> 4.txt

[root@centos7 ~]# cat 4.txt

ls: 無法訪問aaa.txt: 沒有那個文件或目錄


ls aaa.txt 2>>4.txtlsaaa.txt 產生的錯誤信息指定輸出追加到4.txt里


2>產生的錯誤信息指定輸出到指定文件里

2>>產生的錯誤信息指定輸出追加到指定文件里

>正確的信息,2>錯誤的信息


>+2>=&> &>=>正確信息 2>的結合


&>舉例演示

[root@centos7 ~]# ls [1-2].txt aaa.txt &>a.txt

[root@centos7 ~]# cat a.txt?

ls: 無法訪問aaa.txt: 沒有那個文件或目錄

1.txt

2.txt


&>>演示

[root@centos7 ~]# ls [1-2].txt aaa.txt &>>a.txt

[root@centos7 ~]# cat a.txt?

ls: 無法訪問aaa.txt: 沒有那個文件或目錄

1.txt

2.txt

ls: 無法訪問aaa.txt: 沒有那個文件或目錄

1.txt

2.txt


用2個文件區分 正確與錯誤輸出結果

[root@centos7 ~]# ls [1-2].txt aaaa.txt >1.txt 2>a.txt

[root@centos7 ~]# cat 1.txt?

1.txt

2.txt

[root@centos7 ~]# cat a.txt?

ls: 無法訪問aaaa.txt: 沒有那個文件或目錄


輸入重定向<,格式一般是:命令 < 文件 (這個比較少用)

[root@centos7 ~]# wc -l < 1.txt

2


轉載于:https://blog.51cto.com/13578154/2083929

總結

以上是生活随笔為你收集整理的8.1-8.5 shell介绍,命令历史,命令补全和别名,通配符,输入输出重定向的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。