Linux入門實驗

學習Linux要先作實驗來熟悉操做系統本次先寫點入門的操做。

關於Linux入門實驗的操做以下:

【例1】顯示當前使用的shellmysql

[root@centos7 ~]# echo ${SHELL}
/bin/bash

【例2】顯示當前系統使用的全部shellsql

[root@centos7 ~]#cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
/bin/tcsh
/bin/csh

一、type命令:判斷內部或外部
格式:type COMMANDshell

【例3】type查看內部顯示的結果centos

[root@centos7 ~]#type cd
cd is a shell builtin

【例4】type查看外部命令顯示的結果bash

[root@centos7 ~]#type top
top is /usr/bin/top

二、alias命令:查看或設置命令別名
格式:alias NAME=ʼVALUEʼ
取消別名:unalias [-a] NAME
-a:表示取消全部別名。
NAME:表示自定義的命令別名名稱。
若是別名同原命令同名,要執行原命令時,可以使用方式有多種:
\命令名稱
ʻ命令名稱ʼ
/PATH/COMMAND
"【例7】不使用ls的別名
使用:命令前加斜槓 \
命令加引號 ''
命令寫全路徑"服務器

【例5】設置命令別名爲bkmysql,實現拷貝/etc/my.cnf爲/home/back/my.cnf.bak,設置後查看別名session

[root@centos7 ~]#alias bkmysql='cp /etc/my.cnf /home/my.cnf.bak'
[root@centos7 ~]#alias
alias bkmysql='cp /etc/my.cnf /home/my.cnf.bak'
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 rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

【例6】把上例中別名設置寫在配置⽂件⾥,永久⽣效
有兩個文件能夠寫,~/.bashrc⽂件僅當對當前⽤戶,/etc/bashrc⽂件是對全部⽤戶有效。ssh

[root@centos7 ~]#echo "alias bkmysql='cp /etc/my.cnf /home/my.cnf.bak'" >>~/.bashrc
[root@centos7 ~]#tail -1 ~/.bashrc
alias bkmysql='cp /etc/my.cnf /home/my.cnf.bak'

一、date命令:顯示日期和時間 ide



格式:
date [OPTION]... [+FORMAT]
date 選項 參數/格式
clock -w (軟件時間同步到硬件時間)
clock -s (硬件時間同步到軟件時間) 學習

經常使用選項:
-s:指定時間和日期
[例8]設置日期爲

[root@centos7 ~]#date -s'20190318 17:07:50'
Mon Mar 18 17:07:50 CST 2019

顯示當前時間
[root@centos7 ~]#date
Mon Mar 18 17:18:13 CST 2019

2.cal 命令:顯示日曆

[root@centos7 ~]#cal
     March 2019     
Su Mo Tu We Th Fr Sa
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

關機命令: halt、poweroff

重啓命令:reboot

3.shutdown命令:關機或重啓
經常使用選項
-h :至關於halt關機
4.用戶信息查看命令

Whoami命令
Who命令
W命令

均可以查看用戶但稍有區別。

[root@centos7 ~]#whoami
root
[[17:23:59 root@centos7 ~]#who
root     :0           2019-03-18 23:08 (:0)
root     pts/0        2019-03-18 23:10 (:0)
root     pts/1        2019-03-18 23:13 (:0)
root     pts/2        2019-03-19 00:20 (192.168.36.1)
[[17:24:04 root@centos7 ~]#w
 17:24:06 up  2:18,  4 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     :0       :0               23:08   ?xdm?   2:03   0.55s /usr/libexec/gnome-session-binary --se
root     pts/0    :0               23:10     ?     0.05s  0.05s bash
root     pts/1    :0               23:13     ?     0.04s  0.04s bash
root     pts/2    192.168.36.1     00:20     ?     0.38s  0.07s w
  1. echo 命令 :顯示字符
    經常使用選項:
    -n:不自動換行;
    -e:啓用\字符的解釋功能
[root@centos7 ~]#echo $PATH
.:/data/testshell:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@centos7 ~]#echo 'abc'
abc
[root@centos7 ~]#echo -n "abc"
abc[17:39:15 root@centos7 ~]#

擴展命令:$() ``(在Tab鍵上面)

[root@centos7 ~]#echo "i am 'whoami'"
i am 'whoami'
[root@centos7 ~]#echo "i am `whoami`"
i am root
[root@centos7 ~]#echo "i am $(whoami)"
i am root

七、大括號擴展:{}
【例19】數字、字符擴展

[root@centos7 ~]#echo {1..10}
1 2 3 4 5 6 7 8 9 10
[root@centos7 ~]#echo {a..g}
a b c d e f g

8.連按兩次可補全命令或顯示可操做目錄/文件
9命令歷史:
重複前一個命令:

  1. 按方向鍵上,調出,按回車即執行
  2. 輸入!!並回車執行
  3. 輸入!-1並回車執行
  4. history命令查看命令歷史記錄
  5. 輸入!n,執行history命令輸出對應序號n的命令
  6. !string:

十、調用上一次命令最後一個參數:有多種方法
1.輸入:!$
2.按Esc鍵鬆開後,再按.(點號,同時按)

【例21】調用上一次命令最後一個參數

[root@centos7 data]#ll /etc/ssh//sshd_config 
-rw-------. 1 root root 3907 Apr 11  2018 /etc/ssh//sshd_config
[root@centos7 data]#file !$
file /etc/ssh//sshd_config
/etc/ssh//sshd_config: ASCII text

十一、命令歷史環境變量:
系統默認的環境變量都使用大寫字母,顯需環境變量的內容,則須要使用echo$環境變量名稱的形式查看其值。而要想修改或設置環境變量,有兩種方法,一種是可直接在命令行使用export命令方式設置環境變量,則當即生效,退出shell登陸或服務器重啓失效,另外一種是把export命令設置環境變量代碼寫到配置文件裏,重讀配置文件則永久生效。重讀配置文件生效有兩種方法:一種是使用source命令重讀配置文件生效,另使種使號。
點號。

HISTISIZE:命令歷史記錄條數
查看命令歷史條數

[root@centos7 data]#echo $HISTSIZE
1000

【例23】臨時修改環境變量歷史記錄條數

[root@centos7 data]#export HISTSIZE=5000
[root@centos7 data]#echo $HISTSIZE
5000

【例24】永久修改環境變量歷史記錄條數

[root@centos7 data]#echo "export HISTSIZE=5000" >>~/.bash_profile
[root@centos7 data]#source ~/.bash_profile
[root@centos7 data]#echo $HISTSIZE
5000

【例25】顯示歷史條件保存的完整路徑

[root@centos7 data]#echo $HISTFILE
/root/.bash_history

【例27】設置歷史命令顯示執行的日期和時間

[root@centos7 data]#export HISTTIMEFORMAT="%F %T"
[root@centos7 data]#history

十二、Bash中經常使用快捷鍵:Ctrl+l:清屏,至關於clear命令。Ctrl+c:終止命令。Ctrl+z:掛起命令。Ctrl+a:光標移到命令行首,至關於Home鍵。Ctrl+e:光標移到命令行尾,至關於End鍵。Ctrl+u:從光標處刪除至命令行尾。Ctrl+k:從光標處刪除至命令行尾。Alt+r:刪除當前命令行整行。注意在xshell軟件中Alt鍵衝突,若想要使用則須要重置。

相關文章
相關標籤/搜索