一个特殊管理帐户 也被称为超级用户
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
例如 登录到系统下的虚拟终端
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 Interfaceshell程序 sh(bourn 史蒂夫·伯恩) csh tcsh ksh(korn)
bash (bourn again shell)GPL zsh
什么是shell shell是linux系统的用户界面 提供了用户与内核进行交互操作的一种接口。它接收用户输入的命令并把它送入内核去执行 shell也被称为linux命令解释器 command interpreter shell是一种高级程序设计语言echo $SHELL
例如
[root centos6 ~]# echo $SHELL #查看当前使用的shell
/bin/bash
显示当前系统使用的所有shellcat /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 其相当于执行命令VALUEalias 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 root centos6.magedu.com
(/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 root centos6.magedu.com
(/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 root centos6.magedu.com
(/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 root centos6.magedu.com
(/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
命令historyhistory [-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 #把文本粘帖到当前光标处
转载于:https://blog.51cto.com/2067926/2121257





本文链接: http://testbourne.immuno-online.com/view-772434.html