用戶登錄
root用戶
一個特殊管理帳戶 也被稱爲超級用戶
root已接近完整的系統控制
對系統損害幾乎有無限的能力
除非必要,不要登錄爲root
普通(非特權)用戶
權限有限
造成損害的能力比較有限
終端terminal
設備終端
鍵盤鼠標顯示器
虛擬終端(tty:/dev/tty# #爲[2-6])
tty 可有n個,ctrl+alt+F[2-6]
centos6:ctrl + alt +F2~F6
/dev/tty2~6
centos7:ctrl + alt + F2~F6
/dev/tty2~6
例如:登錄到系統下的虛擬終端
圖形終端(/dev/tty1)
Centos 6:ctrl + alt + F1
/dev/tty1
Centos 7:ctrl + alt + F1
/dev/tty1
僞終端(pty:pseudo-tty,/dev/pts/#)
pty,SSH遠程連接
例如:
[root@centos6 ~]# tty
/dev/pts/0
查看當前的終端設備:#tty
[root@centos6 ~]# tty
/dev/pts/0
交互式接口
交互式接口:啓動終端後,在終端設備附加一個交互式應用程序
GUI:Graphic User Interface #圖形用戶界面
X protocol,windows manager,desktop
Desktop:
GNOME (C,圖形庫gtk)
KDE (C++,圖形庫)
XFCE (輕量級桌面)
CLI:Command Line Interface
shell程序:sh(bourn 史蒂夫·伯恩) csh tcsh ksh(korn)
bash (bourn again shell)GPL zsh
什麼是shell?
shell是linux系統的用戶界面,提供了用戶與內核進行交互操作的一種接口。它接收用戶輸入的命令並把它送入內核去執行
shell也被稱爲linux命令解釋器(command interpreter)
shell是一種高級程序設計語言
bash shell
GNU Bourne-Again Shell(bash)是GNU計劃中重要的工具軟件之一,目前也是Linuxs標準的shell,與sh兼容
Centos默認使用
顯示當前使用的shell
echo $SHELL
例如:
[root@centos6 ~]# echo $SHELL #查看當前使用的shell
/bin/bash
顯示當前系統使用的所有shell
cat /etc/shells
例如:
[root@centos6 ~]# cat /etc/shells #查看當前系統使用的所有shell
/bin/sh
/bin/bash
/sbin/nologin
/bin/dash
/bin/tcsh
/bin/csh
命令提示符
提示符:prompt
[root@centos6 ~]# #管理員
[magedu@centos6 ~]$ $普通用戶
顯示提示符格式
[root@centos6 ~]# echo $PS1
[\u@\h \W]\$
修改提示符格式
PS1="\[\e[1;5;41;35m\][\u@\h \w]\\$\[\e[0m\]"
說明:
\[\e[1;5;41;35m\]
\e \035m
1 #高亮顯示
5 #閃爍
41 #背景顏色
35 #字體顏色
\u #當前用戶 \h #主機名簡稱
\w #當前工作目錄 \H #主機名
\W #當前工作目錄基名 \t #24小時時間格式
\T #12小時時間格式 \! #命令歷史數
\# #開機後命令歷史數
例如:
[root@centos6 centos6 ~]#PS1="[\u@\h \t \w]\\$"
[root@centos6 04:09:54 ~]#
[root@centos6 04:09:54 ~]#PS1="[\u@\h \H \w]\\$"
[root@centos6 centos6.magedu.com ~]#
執行命令
輸入命令後回車
提請shell程序找到鍵入命令所對應的可執行程序或代碼,並由其分析後提交給內核分配資源將其運行起來
在shell中可執行的命令有兩類
內部命令:由shell自帶的,而且通過某命令形式提供
格式:help #內部命令列表
[root@centos6 ~]# help
GNU bash, version 4.1.2(2)-release (x86_64-redhat-linux-gnu)
These shell commands are defined internally. Type `help' to see this list.
Type `help name' to find out more about the function `name'.
Use `info bash' to find out more about the shell in general.
Use `man -k' or `info' to find out more about commands not in this list.
A star (*) next to a name means that the command is disabled.
job_spec [&] history [-c] [-d offset] [n] or hist>
(( expression )) if COMMANDS; then COMMANDS; [ elif C>
. filename [arguments] jobs [-lnprs] [jobspec ...] or jobs >
: kill [-s sigspec | -n signum | -sigs>
[ arg... ] let arg [arg ...]
[[ expression ]] local [option] name[=value] ...
alias [-p] [name[=value] ... ] logout [n]
bg [job_spec ...] mapfile [-n count] [-O origin] [-s c>
bind [-lpvsPVS] [-m keymap] [-f filen> popd [-n] [+N | -N]
break [n] printf [-v var] format [arguments]
builtin [shell-builtin [arg ...]] pushd [-n] [+N | -N | dir]
caller [expr] pwd [-LP]
case WORD in [PATTERN [| PATTERN]...)> read [-ers] [-a array] [-d delim] [->
cd [-L|-P] [dir] readarray [-n count] [-O origin] [-s>
command [-pVv] command [arg ...] readonly [-af] [name[=value] ...] or>
compgen [-abcdefgjksuv] [-o option] > return [n]
complete [-abcdefgjksuv] [-pr] [-DE] > select NAME [in WORDS ... ;] do COMM>
compopt [-o|+o option] [-DE] [name ..> set [--abefhkmnptuvxBCHP] [-o option>
continue [n] shift [n]
coproc [NAME] command [redirections] shopt [-pqsu] [-o] [optname ...]
declare [-aAfFilrtux] [-p] [name[=val> source filename [arguments]
dirs [-clpv] [+N] [-N] suspend [-f]
disown [-h] [-ar] [jobspec ...] test [expr]
echo [-neE] [arg ...] time [-p] pipeline
enable [-a] [-dnps] [-f filename] [na> times
eval [arg ...] trap [-lp] [[arg] signal_spec ...]
exec [-cl] [-a name] [command [argume> true
exit [n] type [-afptP] name [name ...]
export [-fn] [name[=value] ...] or ex> typeset [-aAfFilrtux] [-p] name[=val>
false ulimit [-SHacdefilmnpqrstuvx] [limit>
fc [-e ename] [-lnr] [first] [last] o> umask [-p] [-S] [mode]
fg [job_spec] unalias [-a] name [name ...]
for NAME [in WORDS ... ] ; do COMMAND> unset [-f] [-v] [name ...]
for (( exp1; exp2; exp3 )); do COMMAN> until COMMANDS; do COMMANDS; done
function name { COMMANDS ; } or name > variables - Names and meanings of so>
getopts optstring name [arg] wait [id]
hash [-lr] [-p pathname] [-dt] [name > while COMMANDS; do COMMANDS; done
help [-dms] [pattern ...] { COMMANDS ; }
格式:enable #查看啓用內部命令
[root@centos6 ~]#enable|wc -l
61
格式:enable cmd #啓用內部命令
[root@centos6 ~]#enable pwd
[root@centos6 ~]#enable|wc -l
61
格式:enable -n cmd #禁用內部命令
[root@centos6 ~]#enable -n pwd
格式:enable -n #查看所有禁用的內部命令
[root@centos6 ~]#enable -n
enable -n pwd
格式:enable -a #查看所有啓用或禁用的內部命令
[root@centos6 ~]#enable -a|wc -l
61
外部命令:在文件系統路徑下有對應的可執行程序文件
查看路徑:which cmd ; whereis cmd
例如:
[root@centos6 ~]#which pwd #查看命令路徑
/bin/pwd
[root@centos6 ~]#whereis pwd #查看命令路徑與man幫助路徑
pwd: /bin/pwd /usr/include/pwd.h /usr/share/man/man1/pwd.1.gz /usr/share/man/man1p/pwd.1p.gz
區別指定的命令是內部或外部命令
格式:type cmd #查看內部或外部命令
[root@centos6 ~]#type pwd
pwd is a shell builtin #提示爲內部命令
執行外部命令
hash緩存表
系統初始hash表爲空,當外部命令執行時,默認會從PATH路徑下尋找該命令,找到後會將這條命令的路徑記錄到hash表中,當再次使用命令時,shell解釋器首先會查看hash表,存在將執行之,如果不存在,將會去PATH路徑下尋找。利用hash緩存表可大大提高命令的調用速率
hash常見用法
格式:
hash #顯示hash緩存
[root@centos6 ~]#hash
hits command
1 /bin/umount
1 /sbin/ifconfig
1 /bin/mount
1 /usr/sbin/useradd
1 /usr/bin/whereis
1 /bin/ls
1 /bin/su
格式:
hash -l #顯示hash緩存,可作爲輸入使用
[root@centos6 ~]#hash -l
builtin hash -p /bin/umount umount
builtin hash -p /bin/mount mount
builtin hash -p /usr/sbin/useradd useradd
builtin hash -p /usr/bin/whereis whereis
builtin hash -p /bin/ls ls
builtin hash -p /bin/su su
格式:
hash -d cmd #清除cmd緩存
[root@centos6 ~]#hash -d ifconfig
[root@centos6 ~]#hash
hits command
1 /bin/umount
1 /bin/mount
1 /usr/sbin/useradd
1 /usr/bin/whereis
1 /bin/ls
1 /bin/su
格式:
hash -r #清除緩存
[root@centos6 ~]# hash #查看hash緩存表
hits command
1 /bin/df
3 /sbin/chkconfig
3 /usr/bin/whatis
1 /usr/bin/file
2 /bin/date
1 /bin/cat
1 /bin/rpm
1 /bin/vi
3 /usr/bin/man
9 /bin/ls
1 /usr/bin/tree
[root@centos6 ~]# hash –r #清空hash緩存表
[root@centos6 ~]# hash #查看hash緩存表
hash: hash table empty
提示:hash緩存表爲空
命令別名
顯示當前shell進程所有可用的命令別名
alias
例子:
[root@centos6 ~]# alias
alias 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'
定義別名NAME,其相當於執行命令VALUE
alias NAME=’VALUE’
[root@centos6 ~]# alias copy='cp' #定義別名
[root@centos6 ~]# alias
alias copy='cp'
alias 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@centos6 ~]# copy /etc/hosts /tmp #測試別名效果
[root@centos6 ~]# ls /tmp/ #複製成功
hosts keyring-cmPiPj orbit-gdm orbit-root pulse-LSt1EPa9YHPT
在命令行中定義的別名,僅對當前shell進程有效
如果想永久有效,要定義在配置文件中
僅對當前用戶:~/.bashrc #某個用戶環境
對所有用戶有效:/etc/bashrc #全局用戶環境
編輯配置給出的新配置不會立即生效
bash進程重新讀取配置文件
source /etc/profile
. /etc/profile
提示:修改環境配置文件需要執行配置文件,否則不生效。
撤消別名
unalias [-a] name [name。。。]
-a #取消所有別名
例子:
[root@centos6 ~]#unalias copy
[root@centos6 ~]#alias
alias 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'
如果別名同原命令同名,如果要執行原命令,可使用如下方法:
"cmd"
\cmd
/bin/ls #外部命令路徑
例子:
[root@centos6 ~]#/bin/ls
anaconda-ks.cfg Documents install.log Music Public Videos
Desktop Downloads install.log.syslog Pictures Templates
[root@centos6 ~]#\ls
anaconda-ks.cfg Documents install.log Music Public Videos
Desktop Downloads install.log.syslog Pictures Templates
[root@centos6 ~]#"ls"
anaconda-ks.cfg Documents install.log Music Public Videos
Desktop Downloads install.log.syslog Pictures Templates
[root@centos6 ~]#'ls'
anaconda-ks.cfg Documents install.log Music Public Videos
Desktop Downloads install.log.syslog Pictures Templates
命令執行的優先順序
alias>內部命令>外部命令(hash>PATH)
系統執行命令過程
1、查看命令屬於內部或外部,若是內部命令直接調用bash程序集成在內存上的命令
2、若是外部命令首先查找hash表是否存在此命令,根據此命令路徑查找命令所在磁盤上的位置並執行結果
3、若hash表不存在命令所在路徑,根據系統環境變量路徑進行一個一個文件夾查找命令所在位置
命令格式
command [OPTIONS] [ARGUMENTS...]
選項: 用於啓用或關閉命令的某個或某些功能
短選項:-c 例如:-l,-h
[root@centos6 ~]#ls -l
-rw-------. 1 root root 1493 May 15 20:13 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 May 15 20:24 Desktop
drwxr-xr-x. 2 root root 4096 May 15 20:24 Documents
drwxr-xr-x. 2 root root 4096 May 15 20:24 Downloads
長選項:--color=auto
[root@centos6 ~]#ls --color=auto
anaconda-ks.cfg Documents install.log Music Public Videos
Desktop Downloads install.log.syslog Pictures Templates
參數:命令的作用對象,比如文件名,用戶名等
注意:
多個選項以及多參數和命令之間使用空白字符分隔
取消和結束命令執行:ctrl+c,ctrl+d
多個命令可以用;符號分開
[root@centos6 ~]# hostname ;pwd;whoami #執行多個命令間分隔符爲“:”
centos6.magedu.com
/root
root
一個命令可以用\分成多行執行
[root@centos6 ~]# if\
> con\
> fig
eth0 Link encap:Ethernet HWaddr 00:0C:29:38:96:E9
inet addr:172.18.118.195 Bcast:172.18.255.255 Mask:255.255.0.0
inet6 addr: fe80::20c:29ff:fe38:96e9/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:47887 errors:0 dropped:0 overruns:0 frame:0
TX packets:597 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:3490558 (3.3 MiB) TX bytes:48078 (46.9 KiB)
簡單命令
date顯示日期和時間
[root@centos6 ~]# date --help
Usage: date [OPTION]... [+FORMAT]
or: date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.
選項:
-s, --set=STRING set time described by STRING
-d, --date=STRING display time described by STRING, not `now'
格式:
%a locale's abbreviated weekday name (e.g., Sun)
%A locale's full weekday name (e.g., Sunday)
%b locale's abbreviated month name (e.g., Jan)
%B locale's full month name (e.g., January)
%d day of month (e.g, 01)
%D date; same as %m/%d/%y
%F full date; same as %Y-%m-%d
%H hour (00..23)
%m month (01..12)
%M minute (00..59)
%r locale's 12-hour clock time (e.g., 11:11:04 PM)
%R 24-hour hour and minute; same as %H:%M
%s seconds since 1970-01-01 00:00:00 UTC
%S second (00..60)
%T time; same as %H:%M:%S
%w day of week (0..6); 0 is Sunday
查看與設置日期
例子:
[root@centos6 ~]# date "+%Y年%m月%d日 %H時%M分%S秒 星期2" #設置年月日 時分秒 周
2018年05月15日 15時59分43秒 星期2
[root@centos6 ~]# date "+%F %T" #按年月日 時分秒查看當前日期
2018-05-15 16:00:07
%F #顯示年月日
%T #顯示時分秒
[root@centos6 ~]# date +%s #顯示從1970-01-01到當前秒數
1526371345
[root@centos6 ~]# date -d @1546732268 #通過上條秒數查年當前日期時間
Sun Jan 6 07:51:08 CST 2019
-d #顯示字符串所指的日期與時間
[root@centos6 ~]# date 100112202019.20 月日時分[年][.秒] #設置日期時間
Tue Oct 1 12:20:20 CST 2019
[root@centos6 ~]# date -s '2020-1-5 12:30:50' #設置日期時間
Sun Jan 5 12:30:50 CST 2020
Linux的兩種時鐘
系統時鐘:由linux內核通過CPU的工作頻率進行的
硬件時鐘:主板
hwclock,clock:顯示硬件時鐘
-s,#硬時間寫入軟時間
-w ,#軟時間寫入硬時間
例子:
#硬時間寫入軟時間
[root@centos6 ~]#clock
Fri 05 Jan 2018 12:32:17 PM CST -0.047334 seconds
[root@centos6 ~]#
[root@centos6 ~]#date
Sat Jan 5 12:31:34 CST 2019
[root@centos6 ~]#clock -s
[root@centos6 ~]#date
Fri Jan 5 12:33:25 CST 2018
#軟時間寫入硬時間
[root@centos6 ~]#date -s '2019-1-5 12:30:50'
Sat Jan 5 12:30:50 CST 2019
[root@centos6 ~]#date
Sat Jan 5 12:30:52 CST 2019
[root@centos6 ~]#clock
Fri 05 Jan 2018 12:34:14 PM CST -0.360156 seconds
[root@centos6 ~]#clock -w
[root@centos6 ~]#clock
Sat 05 Jan 2019 12:32:03 PM CST -0.907097 seconds
[root@centos6 ~]#date
Sat Jan 5 12:35:30 CST 2019
cal 顯示日曆
cal -y #顯示當前日曆
cal 2018 #顯示2018年日曆
cal 1 2018 #顯示2018年1月日曆
[root@centos6 ~]# cal 1 2018
January 2018
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
-f :強制,不調用shutdown
-p :切斷電源
關機或重啓:shutdown
shutdown [OPTION]... TIME
-r :reboot
[root@centos6 ~]# shutdown -r +1
Broadcast message from [email protected]
(/dev/pts/1) at 15:00 ...
The system is going down for reboot in 1 minute!
-h :halt
-c :cancel #在其它終端取消關機
TIME:無指定,默認相當於 +1
now #立刻,相當於+0
+m: #相對時間表示法,幾分鐘後;例如:+3
hh:mm #絕對時間表示,指明具體時間
shutdown -h +0
+xx 幾分鐘後關機
20:00
init 0 #關機
init 6 #重啓
[root@centos6 ~]#shutdown -h +1 #1分鐘後關機
Broadcast message from [email protected]
(/dev/pts/2) at 12:47 ...
The system is going down for halt in 1 minute!
^Cshutdown: Shutdown cancelled
[root@centos6 ~]#shutdown -h +20 #20分鐘後關機
Broadcast message from [email protected]
(/dev/pts/2) at 12:48 ...
The system is going down for halt in 20 minutes!
^Cshutdown: Shutdown cancelled
[root@centos6 ~]#
[root@centos6 ~]#shutdown -h 20:00 #晚上20:00整關機
Broadcast message from [email protected]
(/dev/pts/2) at 12:48 ...
The system is going down for halt in 432 minutes!
^Cshutdown: Shutdown cancelled
用戶登錄信息查看命令:
whoami #顯示當前的用戶名
[root@centos6 ~]#whoami
root
who am i #顯示當前的用戶名,終端,登錄時間(來源IP)
[root@centos6 ~]#who am i
root pts/2 2018-05-16 03:52 (192.168.31.1)
who #顯示當前系統的所有登錄信息
[root@centos6 ~]#who #顯示當前系統所有登錄信息
root tty2 2018-05-16 03:30
root pts/0 2018-05-16 02:35 (192.168.31.1)
root tty1 2018-05-16 03:29 (:0)
root pts/2 2018-05-16 03:52 (192.168.31.1)
w #詳細顯示當前系統的所有登錄信息及執行過的操作
[root@centos6 ~]#w
12:52:32 up 3:06, 4 users, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty2 - 16May18 234days 0.01s 0.01s -bash
root pts/0 192.168.31.1 16May18 234days 0.10s 0.10s -bash
root tty1 :0 16May18 234days 4.39s 4.39s /usr/bin/Xorg :
root pts/2 192.168.31.1 16May18 0.00s 0.70s 0.10s w
screen命令:
創建新screen會話
screen -S [SESSION]
screen –S help #創建help會話
加入screen會話
screen -x [SESSION]
screen –x help #加入help會話
退出並關閉screen會話
exit
剝離當前screen會話
ctrl+a,d
顯示所有已經打開的screen會話
screen -ls #查看所有打開的screen會話
恢復某screen會話
screen -r [SESSION]
screen –r help #恢復help會話(關閉某個終端使用此命令來恢復會話)
提示:系統默認沒有安裝。
安裝screen方法:
Centos6:
在虛擬機上操作,加載好光盤centos6-dvd1
[root@centos6 ~]# mount /dev/cdrom /media/
[root@centos6 media]# cd CentOS_6.9_Final/Packages/ #注意軟件包所在位置
[root@centos6 Packages]# rpm -ivh screen-4.0.3-19.el6.x86_64.rpm #安裝screen
Centos7:
在虛擬機上操作,加載好光盤centos7-everything
[root@centos7 ~]# mount /dev/cdrom /media/ #掛載cdrom到/media
[root@centos7 ~]# cd /media/Packages/
[root@centos7 Packages]# rpm -ivh screen-4.1.0-0.25.20120314git3c2946.el7.x86_64.rpm #安裝screen
[root@centos7 Packages]# rpm -qa screen #查看screen安裝包
screen-4.1.0-0.25.20120314git3c2946.el7.x86_64
[root@centos7 Packages]# screen -S help #創建screen會話,會話名爲help
[root@centos7 Packages]# screen –ls #查看screen會話
There is a screen on:
11294.help (Attached)
1 Socket in /var/run/screen/S-root.
[root@centos7 Packages]# screen -x help #另一個終端加入screen會話
Attaching from inside of screen?
echo命令
功能:顯示字符
語法:echo [-neE] [字符串]
說明:echo會將輸入的字符串送往標準輸出。輸出的字符串間以空白字符隔開,並在最後加上換行號
選項:
-E #(默認)不支持\解釋功能
-n #不自行換行
-e #啓用\字符的解釋功能
啓用命令選項-e,若字符串中出現以下字符,則特別加以處理,而不會將它當成一般文字輸出
\a #發出警告聲
\b #退格鍵
\c #最後不加上換行符號
\n #換行且光標移到行首
\r #回車,即光標移到行首,但不換行
\t #插入tab
\\ #插入\字符
例子:
[root@centos6 ~]# echo -e "\a" #發出警告聲
[root@centos6 ~]# echo -e "abc\b123" #刪除\b前一個字符串
ab123
[root@centos6 ~]# echo -e "abc\c" #\c回車不換行
abc[root@centos6 ~]#
[root@centos6 ~]# echo -e "abc\n" #\n換行且光標移到到行首
abc
提示:結果最後多出一空白行表示已換行。
[root@centos6 ~]# echo -e "abc\n123" #\n換行且光標移到到行首
abc
123
[root@centos6 ~]# echo -e "abc\r123" #\r回車,光標移到行首,不換行
123
[root@centos6 ~]# echo -e "abc\t123" #\t插入tab鍵功能
abc 123
[root@centos6 ~]# echo -e "abc\\123" #\\插入\字符號
abc\123
\0nnn #插入nnn(八進制)所代表的ASCII字符
echo –e ‘\033[43;31;5m magedu \033[0m’
格式:
echo –e “\033[字體背景顏色;文字顏色;字體實現效果m 字符串 \033[0m”
設置字體顏色:
字體顏色:30-37
echo -e "\033[30m 黑色字 \033[0m"
echo -e "\033[31m 紅色字 \033[0m"
echo -e "\033[32m 綠色字 \033[0m"
echo -e "\033[33m ×××字 \033[0m"
echo -e "\033[34m 藍色字 \033[0m"
echo -e "\033[35m 紫色字 \033[0m"
echo -e "\033[36m 天藍字 \033[0m"
echo -e "\033[37m 白色字 \033[0m"
字體背景顏色範圍:40—47
echo -e "\033[40;37m 黑底白字 \033[0m"
echo -e "\033[41;37m 紅底白字 \033[0m"
echo -e "\033[42;37m 綠底白字 \033[0m"
echo -e "\033[43;37m 黃底白字 \033[0m"
echo -e "\033[44;37m 藍底白字 \033[0m"
echo -e "\033[45;37m 紫底白字 \033[0m"
echo -e "\033[46;37m 天藍底白字 \033[0m"
echo -e "\033[47;30m 白底黑字 \033[0m"
最後控制選項說明:
\33[0m 關閉所有屬性
\33[1m 設置高亮度
\33[4m 下劃線
\33[5m 閃爍
\33[7m 反顯
\33[8m 消隱
\33[30m — \33[37m 設置前景色
\33[40m — \33[47m 設置背景色
例子:
[root@centos6 ~]# echo -e '\033[43;31;5m magedu \033[0m'
magedu
[root@centos6 ~]# echo -e '\033[31m magedu \033[0m' #字體爲紅色
magedu
提示:最後結果效果是背景×××,字體紅色,字體閃爍。
顯示變量
echo “$VAR_NAME” #變量會替換,弱引用
[root@centos6 ~]# echo "$PATH" #變量名加雙引號會解析變量
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
echo ‘VAR_NAME’ #變量不會替換,強引用
[root@centos6 ~]# echo '$PATH' #變量名加單引號不會解析變量
$PATH
命令行擴展、被括起來的集合
命令行擴展:$()或``
把一個命令的輸出打印給另一個命令的參數
echo “my computer name is $(hostname)”
echo “I am `whoami`”
[root@centos6 ~]# echo "my computer name is $(hostname)"
my computer name is centos6.magedu.com
[root@centos6 ~]# echo "I am `whoami`"
I am root
括號擴展:{}
打印重複字符串的簡化形式
echo file{1..5}
[root@centos6 ~]# echo file{1..5}
file1 file2 file3 file4 file5
echo {a..z}
[root@centos6 ~]# echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z
echo {A..Z} {a..z}
[root@centos6 ~]# echo {a..z} {A..Z}
a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
echo {0..20..2}
[root@centos6 ~]# echo {0..20..2} #打印0-20步長爲2的數字
0 2 4 6 8 10 12 14 16 18 20
[root@centos6 app]# echo file{a,b} #打印大括號a,b參數
filea fileb
[root@centos6 app]# echo file{,b} #打印大括號中參數,逗號前無參數將本身文件名及逗號後的參數
file fileb
強調變量名的起止範圍
[root@centos6 ~]# A=123 #定義變量A
[root@centos6 ~]# echo $A #查看A變量結果
123
[root@centos6 ~]# echo $A456 #打印A變量並追加456數字
提示:
1、執行結果無顯示,系統無法解析$A456變量
2、如果變量後面還跟其它參數,變量名需要加大括號{}
[root@centos6 ~]# echo ${A}456 #變量名加{}大括號
123456
tab鍵
命令補全
內部命令:內部命令直接調用bash程序集成在內存上的命令
外部命令:bash根據PATH環境變量定義的路徑,自左而右在每個路徑搜尋以給定命令名命名的文件,第一次找到的命令即爲要執行的命令
用戶給定的字符串只有一條惟一對應的命令,直接補全,否則,再次tab會給出列表
路徑補全
把用戶給出的字符串當做路徑開頭,並在其指定上級目錄下搜索以指定的字符串開頭的文件名
如果惟一:則直接補全
否則:再次tab給出列表
雙擊tab鍵
command 2tab #所有子命令或文件補全
[root@centos6 ~]# ls #按2Tab列出所有ls子命令
ls lsb_release lsinitrd lsof lsusb
lsattr lscpu lslogins lspci lsusb.py
lsblk lshal lsmod lspcmcia
[root@centos6 ~]# ls /etc/h #列出以/etc/h開頭的文件名
hal/ hosts hosts.deny httpd/
host.conf hosts.allow hp/
string2tab #以string開頭命令補全
[root@centos6 ~]# ifc #按2Tab列出所有以ifc開頭命令
ifcfg ifconfig
/2Tab #顯示所有根目錄下一級目錄,包括隱藏目錄
[root@centos6 ~]# /
app/ dev/ lib64/ mnt/ root/ sys/
bin/ etc/ lost+found/ net/ sbin/ tmp/
boot/ home/ media/ opt/ selinux/ usr/
.dbus/ lib/ misc/ proc/ srv/ var/
./2Tab #當前目錄下子目錄,不包括隱藏目錄
[root@centos6 ~]# ./
.abrt/ Desktop/ .gnome2/ .local/ Public/ Videos/
.cache/ Documents/ .gnote/ Music/ .pulse/
.config/ Downloads/ .gnupg/ .nautilus/ .ssh/
.dbus/ .gconf/ .gvfs/ Pictures/ Templates/
*2Tab #當前目錄 下子目錄,不包括隱藏目錄
~2Tab #所有用戶列表
$2Tab #所有變量
@2Tab #/etc/hosts記錄 (centos7不支持)
=2Tab #相當於ls –A(centos7不支持)
命令行歷史
保存你輸入的命令歷史。呆以用它來重複執行命令
登錄shell時,會讀取命令歷史文件中記錄下的命令
~/.bash_history
登錄進shell後新執行的命令只會記錄在緩存中,這些命令會用戶退出時“追加”至命令歷史文件中
重複前一個命令,有4種方法
1、重複前一個命令使用上方向鍵,並回車執行
2、按!!並回車執行
[root@centos6 ~]# !!
echo $SHELL
/bin/bash
3、輸入!-1並回車執行
[root@centos6 ~]# !-1
echo $SHELL
/bin/bash
4、按ctrl+p並回車執行
!n 執行history命令輸出對應號n的命令
[root@centos6 ~]# history
1 history
2 ls
3 pwd
4 hostname
5 history
[root@centos6 ~]# !3 #指定歷史記錄行號
pwd
/root
!-n 執行history歷史中倒數第n個命令
[root@centos6 ~]# history
history
1 history
2 ls
3 pwd
4 hostname
5 history
6 pwd
7 history
[root@centos6 ~]# !-4 #歷史記錄倒數第4個命令
hostname
centos6.magedu.com
!string重複前一個以“string”開頭的命令
[root@centos6 ~]# service postfix restart
Shutting down postfix: [ OK ]
Starting postfix: [ OK ]
[root@centos6 ~]# !service #重複前一個以service開頭的命令
service postfix restart
Shutting down postfix: [ OK ]
Starting postfix: [ OK ]
!?string 重複前一個包含string的命令
[root@centos6 ~]# !?postfix #模糊查找命令行歷史包含postfix命令參數
service postfix restart
Shutting down postfix: [ OK ]
Starting postfix: [ OK ]
!command:p 僅打印命令歷史,而不執行
[root@centos6 ~]# !service:p #僅打印命令歷史中包含以service的命令,並不執行
service postfix restart
[root@centos6 ~]# !postfix:p
-bash: !postfix: event not found
#此錯誤提示此方法調用命令行歷史查找不到所面命令,必須以開頭命令查找。
!$:p 打印輸出!$(上一條命令的最後一個參數)的內容
[root@centos6 ~]# echo a b c d e
a b c d e
[root@centos6 ~]# !$:p #打印上一條命令最後一個參數
e
!^:p 打印輸出!^(上一條命令的第一個參數)的內容
[root@centos6 ~]# echo a b c d e
a b c d e
[root@centos6 ~]# !^:p #打印上一條命令的第一個參數
a
!*:p 打印輸出!*(上一條命令的所有參數)的內容
[root@centos6 ~]# echo a b c d e
a b c d e
[root@centos6 ~]# !*:p #打印上一條命令中所有參數
a b c d e
^string 刪除上一條命令中的第一個string
[root@centos6 ~]# echo a b c d e
a b c d e
[root@centos6 ~]# ^a #刪除命令參數以a開頭的參數
echo b c d e
b c d e
^string1^string2 將上一條命令中的第一個string替換爲string2
[root@centos6 ~]# echo a b c d e
a b c d e
[root@centos6 ~]# ^a^A #將上機命令參數中a替換爲A
echo A b c d e
A b c d e
!:gs/string1/string2 將上一條命令中所有的string1都替換爲string2
[root@centos6 ~]# echo a b c d e a f a #打印echo輸出結果
a b c d e a f a
[root@centos6 ~]# !:gs/a/A #將上面命令參數所有a字符替換爲A
echo A b c d e A f A
A b c d e A f A
使用up(向上)和down(向下)鍵來上下瀏覽從前輸入的命令
ctrl+r來在命令歷史中搜索命令
(reverse-i-search)`': #在後面輸入需要搜索命令
ctrl+g:從歷史搜索模式退出
要重新調用前一個命令中最後一個參數
!$ #表示調用前一個命令最後一個參數
ESC,. #點擊ESC鍵後鬆開,然後點擊“.”鍵
Alt+. #按住Alt鍵的同時點擊“.”鍵
調用歷史參數
command !^ : 利用上一個命令的第一個參數做cmd的參數
[root@centos6 ~]# echo a b c d e #創建測試條件
a b c d e
[root@centos6 ~]# touch !^ #調用上條命令第一個參數做爲touch命令參數
touch a
command !$ : 利用上一個命令的最後一個參數做cmd的參數
[root@centos6 ~]# echo a b c d e
a b c d e
[root@centos6 ~]# touch !$ #調用上條命令最後一個參數做爲touch命令參數
touch e
command !* : 利用上一個命令的全部參數做cmd的參數
[root@centos6 ~]# echo a b c d e
a b c d e
[root@centos6 ~]# touch !* #調用上條命令所有參數做爲touch命令參數
touch a b c d e
command !:n : 利用上一個命令的第n個參數做cmd的參數
[root@centos6 app]# echo a b c d e 1 2 3
a b c d e 1 2 3
[root@centos6 app]# touch !:6 #指定調用上條命令n個參數做爲touch命令參數
touch 1
command !n:^ 調用第n條命令的第一個參數
[root@centos6 app]# history
。。。 。。。
40 cd /app
41 ehco a b c d e 1 2 3
42 echo a b c d e 1 2 3
43 touch 1
44 echo a b c d e 1 2 3
45 ls /etc/hosts;ls /etc/motd
46 ls /etc/hosts
47 history
[root@centos6 app]# touch !44:^ #調用命令行歷史行號爲44的命令爲第一個參數
touch a
command !n:$ 調用第n條命令的最後一個參數
[root@centos6 app]# touch !44:$ #調用命令行歷史行號爲44的命令爲最後一個參數
touch 3
command !n:m 調用第n條命令的第m個參數
[root@centos6 app]# touch !44:3 #調用命令行歷史行號爲44的命令爲第3個參數
touch c
command !n:* 調用第n條命令的所有參數
[root@centos6 app]# touch !44:* #調用命令行歷史行號爲44的命令爲所有參數
touch a b c d e 1 2 3
command !string:^ 從命令歷史中搜索以string 開頭的命令,並獲取它的第一個參數
[root@centos6 app]# history
。。。 。。。
40 cd /app
41 ehco a b c d e 1 2 3
42 echo a b c d e 1 2 3
43 touch 1
44 echo a b c d e 1 2 3
45 ls /etc/hosts;ls /etc/motd
46 ls /etc/hosts
47 history
[root@centos6 app]# touch !echo:^ #從命令歷史中搜索以echo開頭的命令,並獲取第一個參數
touch a
command !string:$ 從命令歷史中搜索以string 開頭的命令,並獲取它的最後一個參數
[root@centos6 app]# touch !echo:$ #從命令歷史中搜索以echo開頭的命令,並獲取最後一個參數
touch 3
command !string:n 從命令歷史中搜索以string 開頭的命令,並獲取它的第n個參數
[root@centos6 app]# touch !echo:3 #從命令歷史中搜索以echo開頭的命令,並獲取n個參數
touch c
command !string:* 從命令歷史中搜索以string 開頭的命令,並獲取它的所有參數
[root@centos6 app]# touch !echo:* #從命令歷史中搜索以echo開頭的命令,並獲取所有參數
touch a b c d e 1 2 3
命令history
history [-c] [-d offset] [n]
history -anrw[filename]
history -psarg[arg...]
-c: 清空命令歷史
-d offset: 刪除歷史中指定的第offset個命令
n: 顯示最近的n條歷史
-a: 追加本次會話新執行的命令歷史列表至歷史文件
-r: 讀歷史文件附加到歷史列表
-w: /path/filename 將當前終端的新命令追加到指定文件
-n: 讀歷史文件中未讀過的行到歷史列表
[root@centos6 app]# history
1 history
2 ls
3 pwd
4 hostname
5 history
6 pwd
7 history
8 hostname
9 service postfix restart
10 restart
11 echo a b c d e
[root@centos6 app]# history –c #清空所有歷史
[root@centos6 app]# history
1 history
[root@centos6 app]# history
1 history
2 11
3 111
4 1111
5 aa
6 aaa
7 history
[root@centos6 app]# history -d 5 #刪除指定歷史行號命令
[root@centos6 app]# history
1 history
2 11
3 111
4 1111
5 aaa
6 history
7 history -d 5
8 history
[root@centos6 ~]# history 5 #顯示最近的n條歷史
251 ifconfig
252 ifconfig eth1
253 gedit /etc/hosts
254 history
255 history 5
命令歷史相關環境變量
HISTSIZE:命令歷史記錄的條數
[root@centos6 ~]# echo $HISTSIZE #查看命令歷史記錄條數
1000
HISTFILE:指定歷史文件,默認爲~/.bash_history
[root@centos6 ~]# echo $HISTFILE #查看當前用戶默認歷史文件
/root/.bash_history
HISTFILESIZE:命令歷史文件記錄歷史的條數
[root@centos6 ~]# echo $HISTFILESIZE #查看命令歷史文件記錄歷史條數
1000
HISTTIMEFORMAT=“%F %T “ 顯示時間
[root@centos6 ~]# HISTTIMEFORMAT="%F %T " #設置命令歷史日期
[root@centos6 ~]# history
1 2019-01-06 01:26:32 vi /etc/sysconfig/network-scripts/ifcfg-eth
2 2019-01-06 01:26:32 vi /etc/sysconfig/network-scripts/ifcfg-eth0
3 2019-01-06 01:26:32 vi /etc/sysconfig/network-scripts/ifcfg-eth1
4 2019-01-06 01:26:32 /etc/init.d/network restart
5 2019-01-06 01:26:32 ifconfig
HISTIGNORE=“str1:str2*:… “ 忽略str1命令,str2開頭的歷史
控制命令歷史的記錄方式:
環境變量:HISTCONTROL
ignoredups #默認,忽略重複的命令,連續且相同爲“重複”
ignorespace #忽略所有以空白開頭的命令
ignoreboth #相當於ignoredups, ignorespace的組合
erasedups #刪除重複命令
[root@centos6 ~]# HISTCONTROL=ignoredups #定義HISTCONTROL變量
[root@centos6 ~]# history
271 aa
272 aaa
273 aa
274 aaa
275 HISTCONTROL=ignoredups
276 history
277 echo $HISTCONTROL
278 history
export 變量名="值“
[root@centos6 ~]# export A="hello" #定義A變量
[root@centos6 ~]# echo $A
hello
存放在/etc/profile 或~/.bash_profile
定義變量存放以下文件裏:
/etc/profile
~/.bash_profile
bash的快捷鍵
Ctrl + l #清屏,相當於clear命令
Ctrl + o #執行當前命令,並重新顯示本命令
Ctrl + s #阻止屏幕輸出,鎖定
Ctrl + q #允許屏幕輸出
Ctrl + c #終止命令
Ctrl + z #掛起命令
Ctrl + a #光標移到命令行首,相當於Home
Ctrl + e #光標移到命令行尾,相當於End
Ctrl + f #光標向右移動一個字符
Ctrl + b #光標向左移動一個字符
Alt + f #光標向右移動一個單詞尾
Alt + b #光標向左移動一個單詞首
Ctrl + xx #光標在命令行首和光標之間移動
Ctrl + u #從光標處刪除至命令行首
Ctrl + k #從光標處刪除至命令行尾
Ctrl + backspace #刪除交互式輸入錯誤命令
Alt + r #刪除當前整行
Ctrl + w #從光標處向左刪除至單詞首
Alt + d #從光標處向右刪除至單詞尾
Ctrl + d #刪除光標處的一個字符
Ctrl + h #刪除光標前的一個字符
Ctrl + y #將刪除的字符粘貼至光標後
Alt + c #從光標處開始向右更改爲首字母大寫的單詞
Alt + u #從光標處開始,將右邊一個單詞更改爲大寫
Alt + l #從光標處開始,將右邊一個單詞更改爲小寫
Ctrl + t #交換光標處和之前的字符位置
Alt + t #交換光標處和之前的單詞位置
Alt + N #提示輸入指定字符後,重複顯示該字符N次
注意:Alt組合快捷鍵經常和其它軟件衝突
gnome-terminal
應用程序->附件->終端
支持多個“標籤”shell的圖形化終端模擬器
Ctrl-Shift-n #創建新窗口
Ctrl-Shift-q #關閉當前窗口
Ctrl-Shift-t #創建一個標籤頁
Ctrl-Shift-w #關閉一個標籤頁
Ctrl-PaUp/PgUn #切換到下一個/前一個標籤頁
Ctrl-Shift-c #複製選中的文本
Ctrl-Shift-v #把文本粘帖到當前光標處