7月17日上课 简单命令、命令历史(history)、如何获取帮助、文件管理

一、简单命令

1、date命令

  • 显示和修改时间
[root@centos6 ~]#date                     ---显示时间
Mon Jul 17 20:23:54 CST 2017                 
[root@centos6 ~]#date 071720242017.30     ---修改时间,时间格式为月日小时分年.秒
Mon Jul 17 20:24:30 CST 2017
[root@centos6 ~]#date "+%F %T"             ---显示目前时间格式
2017-07-17 20:47:36
[root@centos6 ~]#date -d -10day +%A        ---显示10天前是星期几
Friday
  • 和服务器172.18.0.1时间同步
[root@centos6 ~]#ntpdate 172.18.0.1
17 Jul 20:31:33 ntpdate[7744]: step time server 172.18.0.1 offset 37.359560 sec
  • 显示硬件时间
[root@centos6 ~]#clock
Mon 17 Jul 2017 08:33:35 PM CST  -0.799711 seconds

选项

-w 以系统时间为准
-s 以硬件时间为准
  • 显示日历
[root@centos6 ~]#cal
      July 2017     
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
[root@centos6 ~]#cal 08 2008---显示某年某月日历
     August 2008    
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
  • 调整时区
[root@centos7 ~]#date
Mon Jul 17 20:57:21 CST 2017  ---目前时区
[root@centos7 ~]#timedatectl list-timezones ---查看时区
[root@centos7 ~]#timedatectl set-timezone Africa/Abidjan---修改时区
[root@centos7 ~]#date
Mon Jul 17 12:55:19 GMT 2017---时区改变

2、shutdown命令

用法:shutdown [OPTION]... TIME [MESSAGE]
-r: reboot
-h: halt
-c:cancel
TIME:无指定,默认相当于+1
now: 立刻,相当于+0
+m: 相对时间表示法,几分钟之后;例如+3
hh:mm: 绝对时间表示,指明具体时间

  • 设置3分钟之后关机
[root@centos7 ~]#shutdown +3 systerm will shutdown  ---关机
Shutdown scheduled for Mon 2017-07-17 21:06:37 CST, use 'shutdown -c' to cancel.
[root@centos7 ~]#
Broadcast message from root@centos7.magedu.com (Mon 2017-07-17 21:03:37 CST):
systerm will shutdown
The system is going down for power-off at Mon 2017-07-17 21:06:37 CST!
shutdown -c ---取消关机
Broadcast message from root@centos7.magedu.com (Mon 2017-07-17 21:04:01 CST):
The system shutdown has been cancelled at Mon 2017-07-17 21:05:01 CST!

3、screen命令

创建新screen会话
screen –S [SESSION]
加入screen会话
screen –x [SESSION]
退出并关闭screen会话
exit
剥离当前screen会话
Ctrl+a,d
显示所有已经打开的screen会话
screen -ls
恢复某screen会话
screen -r [SESSION]

  • yes 命令如果在执行的过程中断网,怎么恢复
[root@centos6 ~]#screen
[root@centos6 ~]# yes
断网-重现连接
[root@centos6 ~]#screen -ls
[root@centos6 ~]#screen -r

4、echo命令

语法:echo [-neE][字符串]
说明:echo会将输入的字符串送往标准输出。输出的字符串间以空白字符隔开, 并在最后加上换行号
选项:
-E (默认)不支持\解释功能
-n 不自动换行
-e 启用\字符的解释功能

  • 常用选项
[root@centos7 ~]#echo -e "\a"---发出声音
[root@centos7 ~]#echo -e "abca\bef"---退格键
abcef
[root@centos7 ~]#echo -e "abc\cdf"---启用不自动换行
abc[root@centos7 ~]#echo -e "abc\ndf"---启用自动换行
abc
df
[root@centos7 ~]#echo -e "abe\rd"---不换行光标移至行首
dbe
[root@centos7 ~]#echo -e "aa\tbb\tcc\ndd\tee\tff"---加入制表符
aa      bb      cc
dd      ee      ff
[root@centos7 ~]#echo -e "\0101"---八进制所代表的ASCII字符
A
[root@centos7 ~]#echo -e "\x61"---十六进制所代表的ASCII字符
a
  • 命令行扩展$( ) 或把一个命令的输出打印给另一个命令的参数
[root@centos7 ~]#echo "echo $UID"  
echo 0
[root@centos7 ~]#echo 'echo $UID'
echo $UID---当成字符串
[root@centos7 ~]#echo  `echo $UID` 
0---命令调用命令即命令行扩展

总结:单引号是最傻的符号,反向单引号最聪明,双引号介于两者之间。

  • 括号扩展:{ }
    打印重复字符串的简化形式
[root@centos7 ~]#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
[root@centos7 ~]#echo {20..10}
20 19 18 17 16 15 14 13 12 11 10
[root@centos7 ~]#echo {2,5,6}
2 5 6
[root@centos7 ~]#echo {a,b,c}.{txt,log}
a.txt a.log b.txt b.log c.txt c.log
[root@centos7 ~]#echo {1..100..3}
1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58 61 64 67 70 73 76 79 82 85 88 91 94 97 100
[root@centos7 ~]#echo {000..100..3}
000 003 006 009 012 015 018 021 024 027 030 033 036 039 042 045 048 051 054 057 060 063 066 069 072 075 078 081 084 087 090 093 096 099

二、命令历史(history)

history是一个内部命令,每个会话都有自己的命令历史,命令历史先存储在内存中,当退出重新登录后会把内存中的历史命令写入历史文件中( .bash_history)

1、调用历史

  • 重复执行上一条命令:使用上方向键,并回车执行
  • !string 重复前一个以“string”开头的命令
  • !?string 重复前一个包含string的命令
  • string1string2将上一条命令中的第一个string1替换为string2
  • command !^ : 利用上一个命令的第一个参数做cmd的参数
  • command !$ : 利用上一个命令的最后一个参数做cmd的参数
  • command !* : 利用上一个命令的全部参数做cmd的参数
  • !$ 表示:Esc, .(点击Esc键后松开,然后点击. 键)

2、命令history

history [-c] [-d offset] [n]
history -anrw[filename]
history -psarg[arg...]
-c: 清空命令历史
-d: 删除历史中指定的命令 如history -d 2 表示删除第二条历史
n: 显示最近的n条历史
-a: 追加本次会话新执行的命令历史列表至历史文件---注意是新执行的命令
-n: 读历史文件中未读过的行到历史列表---** 比如新开的会话,历史不多,会把历史文件中未读过的行到历史列表中,不重复**
-r: 读历史文件附加到历史列表,---输入一次就会读一次,会重复
-w: 保存历史列表到指定的历史文件---重新登录也会保存到历史文件中
-p: 展开历史参数成多行,但不存在历史列表中---隐藏历史
-s: 展开历史参数成一行,附加在历史列表后---伪造历史

  • 常用选项
[root@centos7 ~]#history -c ---删除内存中的历史,退出重新登录后会从历史文件中把历史读到内存中
[root@centos7 ~]#history
    1  history
[root@centos7 ~]#history -p `hostname`---执行命令但不记录历史
centos7.magedu.com
[root@centos7 ~]#history
    1  history
[root@centos7 ~]#history -p `rm -f /app/*`---但遇到删除命令等敏感词汇时还是会记录历史
[root@centos7 ~]#history
    1  history
    2  history -p `rm -f /app/*`---记录历史
    3  history
[root@centos7 ~]#history -s "rm -rf /"--- 命令不执行,但记录到历史中
[root@centos7 ~]#history 
    1  history
    2  history -n
    3  history
    4  ls
    5  pwd
    6  ll
    7  history
    8  rm -rf /---记录到历史中,可以伪造历史
    9  history 

总结:如果要想做坏事不被别人发现,可以先删除历史文件、再删除内存中的历史,然后exit。

3、命令历史相关环境变量

HISTSIZE:命令历史记录的条数
HISTFILE:指定历史文件,默认为~/.bash_history
HISTFILESIZE:命令历史文件记录历史的条数
HISTTIMEFORMAT=“%F %T “ 显示时间
HISTIGNORE=“str1:str2*:… “ 忽略str1命令,str2开头的历史
控制命令历史的记录方式:
环境变量:HISTCONTROL
ignoredups默认,忽略重复的命令,连续且相同为“重复”
ignorespace忽略所有以空白开头的命令
ignoreboth相当于ignoredups, ignorespace的组合
erasedups删除重复命令
export 变量名="值“
存放在/etc/profile 或~/.bash_profile,建议存放在自己的家目录里,然后.或者source让文件生效即可。

  • 举例
[root@centos7 ~]#HISTTIMEFORMAT="%F %T "---加时间
[root@centos7 ~]#history
    1  2017-07-18 10:27:18 history
    2  2017-07-18 10:27:20 l
    3  2017-07-18 10:27:21 ll
    4  2017-07-18 10:27:23 pwd
    5  2017-07-18 10:27:24 cd
    6  2017-07-18 10:31:09 HISTTIMEFORMAT="%F %T "
    7  2017-07-18 10:31:15 history
[root@centos7 ~]#HISTIGNORE="ll*"---忽略以ll开头的命令
[root@centos7 ~]#ll
total 8
-rw-------. 1 root root 1884 Jul 14 11:24 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Jul 14 11:54 Desktop
drwxr-xr-x. 2 root root    6 Jul 14 11:54 Documents
drwxr-xr-x. 2 root root    6 Jul 14 11:54 Downloads
-rw-r--r--. 1 root root 1915 Jul 14 11:54 initial-setup-ks.cfg
drwxr-xr-x. 2 root root    6 Jul 14 11:54 Music
drwxr-xr-x. 2 root root    6 Jul 14 11:54 Pictures
drwxr-xr-x. 2 root root    6 Jul 14 11:54 Public
drwxr-xr-x. 2 root root    6 Jul 14 11:54 Templates
drwxr-xr-x. 2 root root    6 Jul 14 11:54 Videos
[root@centos7 ~]#history
    1  2017-07-18 10:27:18 history
    2  2017-07-18 10:27:20 ls
    3  2017-07-18 10:27:21 ll
    4  2017-07-18 10:27:23 pwd
    5  2017-07-18 10:27:24 cd
    6  2017-07-18 10:31:09 HISTTIMEFORMAT="%F %T "
    7  2017-07-18 10:31:15 history
    8  2017-07-18 10:32:56 HISTIGNORE="passwd*"
    9  2017-07-18 10:33:38 HISTIGNORE="ll*"
   10  2017-07-18 10:33:49 history
[root@centos7 ~]#HISTCONTROL=ignoreboth---忽略重复和以空白开头的命令
[root@centos7 ~]#pwd
/root
[root@centos7 ~]#pwd
/root
[root@centos7 ~]# ls
anaconda-ks.cfg  Desktop  Documents  Downloads  initial-setup-ks.cfg  Music  Pictures  Public  Templates  Videos
[root@centos7 ~]#history 
    1  2017-07-18 10:27:18 history
    2  2017-07-18 10:27:20 ls
    3  2017-07-18 10:27:21 ll
    4  2017-07-18 10:27:23 pwd
    5  2017-07-18 10:27:24 cd
    6  2017-07-18 10:31:09 HISTTIMEFORMAT="%F %T "
    7  2017-07-18 10:31:15 history
    8  2017-07-18 10:32:56 HISTIGNORE="passwd*"
    9  2017-07-18 10:33:38 HISTIGNORE="ll*"
   10  2017-07-18 10:33:49 history
   11  2017-07-18 10:35:42 pwd
   12  2017-07-18 10:35:50 history
   13  2017-07-18 10:36:54 HISTCONTROL=ignoreboth
   14  2017-07-18 10:36:58 pwd
   15  2017-07-18 10:37:19 history 

3、如何实现屏幕录像

[root@centos7 ~]#script -t 2> /app/time.log -a /app/cmd.log---准备录屏的time.log和cmd.log两个文件,一个用来存时间,一个用来存执行的命令,并开始录屏
Script started, file is /app/cmd.log
---执行一些命令
[root@centos7 ~]#hostname
centos7.magedu.com
[root@centos7 ~]#ls
anaconda-ks.cfg  Desktop  Documents  Downloads  initial-setup-ks.cfg  Music  Pictures  Public  Templates  Videos
---执行退出命令
[root@centos7 ~]#exit
exit
Script done, file is /app/cmd.log
---执行以下命令开始播放录屏
[root@centos7 ~]#scriptreplay /app/time.log /app/cmd.log 

三、如何获取帮助

  • 内部命令获取帮助
    help command
    man bash
  • 外部命令获取帮助
    man command
    command -- help
  • 获取帮助的步骤
[root@centos7 ~]#type passwd---判读以下是内部命令还是外部命令
passwd is /usr/bin/passwd
[root@centos7 ~]#whatis passwd---查看章节,并显示每个章节的信息
passwd (1)           - update user's authentication tokens
sslpasswd (1ssl)     - compute password hashes
passwd (5)           - password file
[root@centos7 ~]#man 1 passwd ---最后用man来查帮助信息
  • 一些命令
[root@centos7 ~]#whereis passwd---显示路径和man帮助文档路径
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz
[root@centos7 ~]#man -a passwd---显示全部章节信息,按q后进入下一个章节
[root@centos7 ~]#man -k passwd 相当于搜索,比如不知道干什么,可以搜索的关键字
chpasswd (8)         - update passwords in batch mode
fgetpwent_r (3)      - get passwd file entry reentrantly
getpwent_r (3)       - get passwd file entry reentrantly
gpasswd (1)          - administer /etc/group and /etc/gshadow
grub2-mkpasswd-pbkdf2 (1) - Generate a PBKDF2 password hash.
lpasswd (1)          - Change group or user password
lppasswd (1)         - add, change, or delete digest passwords.
pam_localuser (8)    - require users to be listed in /etc/passwd
passwd (1)           - update user's authentication tokens
sslpasswd (1ssl)     - compute password hashes
passwd (5)           - password file
passwd2des (3)       - RFS password encryption
pwhistory_helper (8) - Helper binary that transfers password hashes from passwd or shadow to opasswd
saslpasswd2 (8)      - set a user's sasl password
smbpasswd (5)        - The Samba encrypted password file
vncpasswd (1)        - change the VNC password
  • man帮助文档行间跳转
    gg/1G 跳至第一行
    G 跳至最后一行
    100G 跳至第100行

三、文件系统分层结构

  • 目录结构


    Paste_Image.png
  • 文件类型-:普通文件
    d: 目录文件
    b: 块设备
    c: 字符设备
    l: 符号链接文件
    p: 管道文件pipe
    s: 套接字文件socket
  • 如何触发新增加的磁盘
    执行以下命令
    echo '- - -' /sys/class/scsi_host/host2/scan

说明:这个命令只是在实验环境使用,生产环境不会随意增加磁盘的。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,390评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,821评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,632评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,170评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,033评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,098评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,511评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,204评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,479评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,572评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,341评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,893评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,171评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,486评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,676评论 2 335

推荐阅读更多精彩内容