首页 > 代码库 > linux终端和shell

linux终端和shell

第一章 介绍和安装linux

请自行百度

第二章 linux终端和shell

linux终端

虚拟终端由mingetty程序产生

切换终端 ctrl+alt+fn  n=1-6

虚拟终端(输入tty显示当前是那个终端)

[root@learn ~]# tty

/dev/tty1

模拟终端 (使用ssh通过网络连接到linux

[root@learn ~]# tty

/dev/pts/0       

linuxshell

查看系统使用的shell

[root@learn ~]# echo $SHELL

/bin/bash

查看系统可用的shell

[root@learn ~]# cat /etc/shells

/bin/sh

/bin/bash

/sbin/nologin

/bin/dash

/bin/tcsh

/bin/csh

bash功能

命令行编辑

1) 光标快速移动

ctrl+a 跳到行首

ctrl+e 跳到行尾

2) 删除命令中的内容

ctrl+w 删除光标前一个单词

ctrl+u删除光标行首的字符

ctrl+k删除光标行尾的字符

3) 清屏

ctrl+l  windows命令提示符 cls

4) 取消不执行的命令

ctrl+c

内部命令和外部命令

内部命令shell自带的命令

外部命令 在系统的某个路径下的可执行程序,依赖于PATH环境变量进行查找命令

查看是内部命令还是外部命令

[root@learn ~]# type ls

ls is aliased to `ls --color=auto‘

[root@learn ~]# type alias

alias is a shell builtin

也可用以下命令查看哪些属于内部命令

[root@learn ~]# man builtin

查看外部命令搜索路径

[root@learn ~]# echo $PATH

/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

命令补全 路径补全

使用tab键进行命令和路径补全操作

命令历史

echo $HISTSIZE   缓存多少条命令

[root@learn ~]# echo $HISTSIZE

1000

echo $HISTFILE   查看历史文件

[root@learn ~]# echo $HISTFILE

/root/.bash_history

echo $HISTFILESIZE   命令历史文件能保存的条数

[root@learn ~]# echo $HISTFILESIZE

1000

cat  /root/.bash_history  查看保存命令历史

[root@learn ~]# cat /root/.bash_history |head -1    #通过管道过滤显示一条命令

ifconfig

1) 显示历史记录规则

[root@learn ~]# echo $HISTCONTROL

ignoredups

含义:

igonredups 不记录后面重复的命令,只记录一个

ignorespace 不记录以空格开头的命令

ignoreboth 不记录后面重复的命令和不记录以空格开头的命令

永久生效

echo ‘export HISTCONTROL=ignoreboth‘ >> /etc/profile

通过export 修改历史记录规则

[root@learn ~]# export HISTCONTROL=ignoreboth

[root@learn ~]#  history |tail -2  

  480  history |tail -1

  481  history |tail

前面有空格则不记录历史命令

 

2) 执行以前的命令(快捷键)

!!执行上一条命令

history查看历史命令

!6 执行命令历史中第6行命令

!-1执行命令历史中倒数第一条命令

上下键可以翻命令

ctrl+p向上翻

ctrl+n向下翻

 

3) 当前命令调用上一条命令的最后一个参数

方法一:

ESC松开按.

方法二:

新命令后加!$

[root@learn ~]# head !$

head /etc/passwd

history命令参数

-c 清空历史命令

-w覆盖命令到历史文件

-a 追加命令到历史文件

-d 4 删除命令历史的第4条命令

479  export HISTCONTROL=ignoreboth

  480  history |tail -1

  481  history |tail

  482  history |tail -3

[root@learn ~]# history -d 480

[root@learn ~]# history |tail -5

  479  export HISTCONTROL=ignoreboth

  480  history |tail

  481  history |tail -3

  482  history -d 480

  483  history |tail -5

文件名通配符

通配符是特殊的字符,不表示字符的表面意义而是能够匹配符号指定特征的字符

* 代表任意长度的字符

[root@learn lisi]# touch a acb addb cdb ateb

[root@learn lisi]# ls a*

a  acb  addb  ateb

[root@learn lisi]# ls a*b

acb  addb  ateb

 

? 代表任意单个字符

[root@learn lisi]# touch a acb addb cdb ateb

[root@learn lisi]# ls a?

ls: cannot access a?: No such file or directory

[root@learn lisi]# ls a?b

acb

[] 代表指定范围的单个字符

[root@learn lisi]# ls a[0-9]*

a23b  a3b  a4b  a9b

[root@learn lisi]# ls a[0-9]b

a3b  a4b  a9b

^ 排除

[root@learn lisi]# touch a23b a3b a9b a4b

[root@learn lisi]# ls a[^0-5]b

a9b  acb

查找文件中间有空格的文件

[root@learn lisi]# touch ‘a b‘

[root@learn lisi]# ls *[‘ ‘]b

a b

[root@learn lisi]# ls *[‘ ‘]*

a b

使用专门的方法识别特定字符

[root@learn lisi]# ls a[[:space:]]b      #空格

a b

[root@learn lisi]# ls a[[:digit:]]b       #[0-9]

a3b  a4b  a9b

特定字符的含义:

[[:space:]] 空格

[[:digit:]] [0-9]

[[:lower:]] [a-z]

[[::upper:]] [A-Z]

[[:alpha:]] [a-Z]

命令别名

格式: alias cmdalias=‘command [option] [argument]‘

查看别名类型

[root@learn ~]# type alias

查看定义的别名

[root@learn ~]# alias

定义别名

[root@learn ~]# alias if=‘ifconfig eth0‘

[root@learn ~]# alias ifconfig=‘ifconfig eth0‘

调用命令本身(去掉别名含义)

[root@learn ~]# alias ifconfig=‘ifconfig eth0‘

[root@learn ~]# ifconfig

eth0      Link encap:Ethernet  HWaddr 00:0C:29:41:FA:11  

          ......

[root@learn ~]# \ifconfig    #去掉别名,使用命令本身

eth0      Link encap:Ethernet  HWaddr 00:0C:29:41:FA:11  

  ......

eth1      Link encap:Ethernet  HWaddr 00:0C:29:41:FA:1B  

......

lo        Link encap:Local Loopback  

......

将别名添加到用户配置文件

[root@learn ~]# vim .bashrc

编辑/etc/bashrc添加别名,对所有用户生效(全局)

[root@learn ~]# vim /etc/bashrc

删除别名

[root@learn ~]# unalias if1

[root@learn ~]# if1

-bash: if1: command not found

命令替换

使用命令执行的结果替换该命令

格式: 命令 $(命令)

[root@learn home]# echo "The current directory is $(pwd)"

The current directory is /home

[root@learn home]# touch file-$(date +%Y-%m-%d-%H:%M:%S)

[root@learn home]# ls

file-2017-01-05-11:47:38

格式:使用反引号实现命令替换

[root@learn home]# touch file-`(date +%Y-%m-%d-%H:%M:%S)`

[root@learn home]# ls

file-2017-01-05-11:47:38

bash" "是弱引用能够实现命令替换

[root@learn home]# echo "The current directory is $(pwd)"

The current directory is /home

bash‘ ‘ 是强引用不能实现命令替换

[root@learn home]# echo ‘The current directory is $(pwd)‘

The current directory is $(pwd)

命令行展开{}多项式

工作中会遇到如新建很多文件,使用一条命令来解决此问题,这时就需要使用命令行展开来实现,例:

[root@learn lisi]# touch a.txt b.txt.c.txt.d.txt

[root@learn lisi]# ls

a.txt  b.txt.c.txt.d.txt

这样写很繁琐,他们有共通点,这时我们用命令行展开来写,需要用到{}来实现。

[root@learn lisi]# touch {a..d}.txt

[root@learn lisi]# ls

a.txt  b.txt  c.txt  d.txt

怎么样?这样写是不是很方便。还可以创建更复杂的。

比如我们想创建

/home/lisi/a.txt

/home/lisi/b.txt

/home/lisi/a/a.txt

/home/lisi/a/b.txt

/home/lisi/c

/home/lisi/d

但是不能创建目录,需要先创建目录,如果是创建目录则不会存在次问题。

[root@learn lisi]# mkdir a

[root@learn lisi]# touch {a,b}.txt a/{a,b}.txt {c,d}

[root@learn lisi]# touch {{a,b}.txt,a/{a,b}.txt,{c,d}}  #同上一条命令

[root@learn lisi]# ls

a  a.txt  b.txt  c  d

[root@learn lisi]# tree

.

├── a

   ├── a.txt

   └── b.txt

├── a.txt

├── b.txt

├── c

└── d

重定向

重定向的用途

可以将命令输出保存到文件,可以向配置文件增加内容,可以合并文件内容。

查看标准输入输出设备

[lisi@learn ~]$ ls -l /dev/std*

lrwxrwxrwx 1 root root 15 Jan  8 18:29 /dev/stderr -> /proc/self/fd/2

lrwxrwxrwx 1 root root 15 Jan  8 18:29 /dev/stdin -> /proc/self/fd/0

lrwxrwxrwx 1 root root 15 Jan  8 18:29 /dev/stdout -> /proc/self/fd/1

 

 

查看文件内容

[lisi@learn ~]$ tail -2f /etc/passwd

li:x:500:500::/home/li:/bin/bash

lisi:x:501:501::/home/lisi:/bin/bash

ctrl+z将进程转到后台

ps 查看运行的进程

[lisi@learn ~]$ ps

  PID TTY          TIME CMD

 5954 pts/0    00:00:00 bash

 6004 pts/0    00:00:00 tail

 6009 pts/0    00:00:00 ps

[lisi@learn ~]$ ls -l /proc/6004/fd  #查看6004进程下的文件描述符

total 0

lrwx------ 1 lisi lisi 64 Jan 10 22:01 0 -> /dev/pts/0   #标准输入

lrwx------ 1 lisi lisi 64 Jan 10 22:01 1 -> /dev/pts/0   #标准输出

lrwx------ 1 lisi lisi 64 Jan 10 22:00 2 -> /dev/pts/0   #标准错误输出

lr-x------ 1 lisi lisi 64 Jan 10 22:01 3 -> /etc/passwd   #进程指向的文件

lr-x------ 1 lisi lisi 64 Jan 10 22:01 4 -> inotify

重定向 标准输出

[root@learn ~]$ ifconfig eth0 1>abc

重定向 标准错误输出

[root@learn ~]$ ifconfig eth1 2>123

将标准输出和标准错误输出重定向到不同文件

[root@learn ~]$ find /etc -name "*.conf" 1>a 2>b

将标准输出和标准错误输出重定向到同一个文件

[root@learn ~]$ find /etc -name "*.conf" 1>a 2>b

重定向 追加

[root@learn ~]$ find /etc -name "*.conf" 1>>a 2>>b

 

管道

1.在管道后面的命令都不应该再跟文件名。如果跟的话则是后面命令的结果。

[lisi@learn ~]$ tail /etc/passwd |head -2 /etc/passwd  #管道后面这条命令加了文件名,列出后面命令的值

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin 

[lisi@learn ~]$ tail /etc/passwd |head -2  #不加则是需要的值

vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

abrt:x:173:173::/etc/abrt:/sbin/nologin

2.在管道中只有标准输出才传递给下一个命令;标准错误输出直接输出到终端,可以把标准错误输出给重定向。

[lisi@learn ~]$ find /etc/ -name "*.conf" 2>/dev/null |grep rc   #把标准错误输出到空设备去,过滤包含rc内容的文件

/etc/latrace.d/resource.conf

/etc/libreport/events.d/vimrc_event.conf

/etc/init/rcS.conf

/etc/init/rc.conf

/etc/init/rcS-emergency.conf

/etc/init/rcS-sulogin.conf

3.有些命令不支持管道,让xargs支持这些命令。

[root@learn home]# which cat |ls -l   #列出的是当前目录下的内容

total 8

drwx------ 2  500  500 4096 Jan 10 21:39 lisi

drwxr-xr-x 9 root root 4096 Feb 26  2016 vmware-tools-distrib

[root@learn home]# which cat |xargs ls -l  #加上xargs后得到需要的值

-rwxr-xr-x. 1 root root 48568 Oct 15  2014 /bin/cat

第三章 文件管理类命令

命令和选项

command [options] [arguments]

命令      选项    参数

选项

短选项-   例(-h -l -a

短选项可组合 例(-hla

有些命令短选项可以不带-, 通常称作BSD风格的选项 例(ps aux ,tar xf

有些选项需要带参数 tail -n 2 /etc/passwd

长选项不能组合 --help --list

如果需要参数 长选项的参数通常需要=号  --size=1G

命令后的参数就是命令的作用对象

例:ls /root

cat /etc/passwd

命令帮助

内部命令

  help command

外部命令

  command --help

通过type查看命令是内部命令还是外部命令

目录管理

cd(change directroy)

[root@learn ~]# cd ~  #切换目录到主(家)目录

[root@learn ~]# cd    #切换目录到主(家)目录

[root@learn ~]# cd /home/

[root@learn home]# cd -   #来回切换

/root

[root@learn ~]# echo $PWD #当前路径变量

/root

[root@learn ~]# echo $OLDPWD #上一次路径变量

/home

[root@learn ~]# cd ..  #上级目录

[root@learn /]# cd .  #当前目录

..上级目录

. 代表当前目录

绝对路径 以根目录开始的路径

相对路径 以当前目录为参照

lslist

列出文件的信息(默认为当前目录)

-a列出目录中所有文件,包含隐藏文件

-l 长格式显示

-R 递归显示

-d 只显示目录属性

-r 逆序输出

mkdir(make directory)

创建目录

-p --parents   需要时创建目标目录的上层目录

-v --verbose   每次创建新目录都显示信息(显示创建目录的过程)

rmdir(remove directory)

删除目录

-p --parents   删除指定目录及其上级目录

-v --verbose   输出处理的目录详情(显示删除目录的过程)

tree

显示目录结构

最小化安装系统则没有该命令,需要安装该命令

[root@learn ~]# mkdir /mnt/cdrom  #创建需要挂载光盘目录

[root@learn ~]# mount /dev/cdrom /mnt/cdrom/  #挂载光盘到创建的光盘目录

mount: block device /dev/sr0 is write-protected, mounting read-only

[root@learn ~]# find /mnt/cdrom/Packages/ -name "tree*"  #查找光盘内有无该命令的安装包

/mnt/cdrom/Packages/tree-1.5.3-3.el6.x86_64.rpm

[root@learn ~]# rpm -i /mnt/cdrom/Packages/tree-1.5.3-3.el6.x86_64.rpm

package tree-1.5.3-3.el6.x86_64 is already installed  #该提示是之前安装过该命令

[root@learn ~]# tree  #列出该目录结构

.

├── 2.txt

├── anaconda-ks.cfg

├── c

   ├── a.c

   ├── num

   ├── num.c

   └── sum

├── install.log

├── install.log.syslog

└── old.txt

 

1 directory, 9 files

文件管理

linux文件类型

1、普通文件 标识 -

2、目录文件 标识 d

3、链接文件

软链接 标识l

硬链接 标识-

4、特殊文件 用于作为硬件设备访问的文件

块设备 标识b  随机按块进行读取

字符设备 标识c  安装输入的顺序来读取

5、套接字文件(socket) 标识s  让两个进程进行通信使用软件模拟的设备

6、命名管道(pipe) 标识p

 

cpcopy

拷贝目录及文件

格式:  cp__目标

-d 保留文件的链接属性

-a 保留所有属性

-R/r:递归处理,将指定目录下的所有文件与子目录一并处理

-p 包含属性一起

拷贝过程不指定目标文件名,则目标文件名和源文件名一样

[root@learn lisi]# cp 91/1.txt 51cto/

拷贝过程指定目标文件名(改名)

[root@learn lisi]# cp 91/1.txt 51cto/2.txt

[root@learn lisi]# ls

123  51cto  91

51cto文件夹中扩展名是txt的文件拷贝到当前目录

[root@learn lisi]# cp 51cto/*.txt .

[root@learn lisi]# ls

123  1.txt  2.txt  51cto  91

51cto文件夹拷贝到/tmp文件夹

[root@learn lisi]# cp -R 51cto/ /tmp/

[root@learn lisi]# ls 123

123

只有管理员能够将文件拷贝权限不变

[root@learn lisi]# chmod 777 123   #改变123的权限

[root@learn lisi]# ll 123

-rwxrwxrwx 1 root root 0 Jan 16 14:03 123

[root@learn lisi]# cp 123 91/    #直接复制过去则文件属性会发生改变

[root@learn lisi]# ll 91/

total 0

-rwxr-xr-x 1 root root 0 Jan 16 14:08 123

-rw-r--r-- 1 root root 0 Jan 16 14:04 1.txt

[root@learn lisi]# cp -p 123 91/   #-p参数后文件属性不变

[root@learn lisi]# ll 91

total 0

-rwxrwxrwx 1 root root 0 Jan 16 14:03 123

-rw-r--r-- 1 root root 0 Jan 16 14:04 1.txt

rmremove

删除文件或目录

-r递归

-f强制

查看rm帮助

[root@learn ~]# rm --help

删除单个文件

[root@learn ~]# rm 2.txt

rm: remove regular file `2.txt‘? y

-r删除51cto目录中的文件和目录

[root@learn lisi]# ls 51cto/

1.txt  2.txt

[root@learn lisi]# mv -r 51cto/*

mv: invalid option -- ‘r‘

Try `mv --help‘ for more information.

[root@learn lisi]# rm -r 51cto/*

rm: remove regular empty file `51cto/1.txt‘? y

rm: remove regular empty file `51cto/2.txt‘? y

[root@learn lisi]# ls

123  1.txt  2.txt  51cto  91

删除目录

[root@learn lisi]# rm 51cto/

rm: cannot remove `51cto/‘: Is a directory

[root@learn lisi]# rm -r 51cto/

rm: remove directory `51cto‘? y

-f强制删除,无提示

[root@learn lisi]# ls 51cto/

123

[root@learn lisi]# rm -f 51cto/123

 

mvmove

移动文件或目录;重命名功能

查看mv帮助

[root@learn lisi]# mv --help

把目录123移动到91

[root@learn lisi]# mv 1

123    1.txt  

[root@learn lisi]# mv 123 91/

mv: overwrite `91/123‘? y

[root@learn lisi]# ls 91/

123  1.txt

91/123重命名为91/456

[root@learn lisi]# ls 91/

123  1.txt

[root@learn lisi]# mv 91/123 91/456

[root@learn lisi]# ls 91

1.txt  456

touch

改变文件的时间戳

最近一次访问时间(access time

最近一次修改时间(modify time)  改变文件的内容;内容变元数据变

最近一次改变时间(change time) 元数据(文件名、大小、权限)的改变

通过stat命令查询文件3个时间(元数据)

[root@learn lisi]# stat 1.txt

  File: `1.txt‘

  Size: 0          Blocks: 0          IO Block: 4096   regular empty file

Device: 802h/2050d Inode: 787306      Links: 1

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2017-01-16 14:13:43.566016527 +0800

Modify: 2017-01-16 14:13:43.566016527 +0800

Change: 2017-01-16 14:13:43.566016527 +0800

更改访问时间和改变时间

[root@learn lisi]# touch homework

更改修改时间和改变时间

[root@learn lisi]# touch -a 1.txt

[root@learn lisi]# stat 1.txt

  File: `1.txt‘

  Size: 5          Blocks: 8          IO Block: 4096   regular file

Device: 802h/2050d Inode: 787306      Links: 1

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2017-01-17 17:13:42.939001409 +0800

Modify: 2017-01-17 17:13:16.254001264 +0800

Change: 2017-01-17 17:13:42.939001409 +0800

使用指定的时间更改修改时间

[root@learn lisi]# touch -m 1.txt

[root@learn lisi]# stat 1.txt

  File: `1.txt‘

  Size: 5          Blocks: 8          IO Block: 4096   regular file

Device: 802h/2050d Inode: 787306      Links: 1

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2017-01-17 17:13:42.939001409 +0800

Modify: 2017-01-17 17:13:57.920001327 +0800

Change: 2017-01-17 17:13:57.920001327 +0800

如果没有则创建文件,如果存在则更改三个时间

[root@learn lisi]# touch -m -t 201611110930.12 1.txt

[root@learn lisi]# stat 1.txt

  File: `1.txt‘

  Size: 5          Blocks: 8          IO Block: 4096   regular file

Device: 802h/2050d Inode: 787306      Links: 1

Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2017-01-17 17:14:08.478001013 +0800

Modify: 2016-11-11 09:30:12.000000000 +0800

Change: 2017-01-17 17:21:07.863002188 +0800

如果文件不存在不创建

[root@learn lisi]# touch -c hjhj

[root@learn lisi]# ls

1.txt  2.txt  51cto  91  c  homework

file

可以查看文件类型

文件类型有ASCII ,  LSB(二进制)executable(可执行)

[root@learn lisi]# file 1.txt

1.txt: ASCII text

[root@learn lisi]# which cat

/bin/cat

[root@learn lisi]# file /bin/cat   #二进制可执行文件

/bin/cat: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped

find

查找文件

按名称和文件类型查找

-name 按名称

-type 按文件类型

-o 或者

! 非(取反)

文件时间查找

-amin n 查找系统中最后n分钟访问的文件

-atime n 查找系统中最后n*24小时访问的文件

-cmin n 查找系统中最后n分钟被改变文件状态的文件

-ctime n 查找系统中最后n*24小时被改变文件状态的文件

-mmin n 查找系统中最后n分钟被改变文件数据的文件

-mmtime n 查找系统中最后n*24小时被改变文件数据的文件

+n表示此值之前; n表示此值; -n表示此值以内

例: +7表示7天后;7表示第7天;-7表示7天内

默认查找当前路径

[root@learn ~]# find -name "*.txt"

./old.txt

默认查找列出当前路径下所有文件包含子目录内的文件

[root@learn ~]# find

.

./.tcshrc

./install.log

./anaconda-ks.cfg

./.cshrc

./old.txt

./.bash_logout

./.ssh

./.ssh/authorized_keys

./c

./c/num.c

./c/a.c

./c/sum

./c/num

./.viminfo

./install.log.syslog

./.bash_profile

./.bashrc

./.bash_history

查找指定路径的文件

[root@learn ~]# find /home -name "*.txt"

/home/lisi/2.txt

/home/lisi/91/1.txt

/home/lisi/1.txt

/home/vmware-tools-distrib/doc/open_source_licenses.txt

查找满足两个条件的文件  都满足

[root@learn ~]# find /home -name "*.txt"  -name "*.h"

查找满足两个条件的文件 或者

[root@learn ~]# find /home -name "*.txt" -o -name "*.h"

/home/lisi/2.txt

/home/lisi/91/1.txt

/home/lisi/1.txt

/home/vmware-tools-distrib/lib/include/vmci/vmci_sockets.h

/home/vmware-tools-distrib/doc/open_source_licenses.txt

除了某个条件的文件

[root@learn ~]# find /home ! -name "*.txt"

查找/home目录中最近一天更改过的文件

[root@learn ~]# find /home -mtime -1

/home

/home/123

查找/home目录中60分钟内(小于)修改过的文件

[root@learn ~]# find /home -mmin -60

/home

/home/123

查找/home目录中60分钟以前(大于)更改过的文件

[root@learn ~]# find /home -mmin +60

查找所属主(lisi)的文件

[root@learn ~]# find /home -user lisi

/home/lisi

/home/lisi/.bash_logout

/home/lisi/.bash_profile

/home/lisi/.bashrc

/home/lisi/.bash_history

查找所属组(lisi)的文件

[root@learn ~]# find /home -group lisi

/home/lisi

/home/lisi/.bash_logout

/home/lisi/.bash_profile

/home/lisi/.bashrc

/home/lisi/.bash_history

使用主id和组id查找文件

[root@learn ~]# id lisi

uid=501(lisi) gid=501(lisi) groups=501(lisi)

[root@learn ~]# find /home -uid 501

/home/lisi

/home/lisi/.bash_logout

/home/lisi/.bash_profile

/home/lisi/.bashrc

/home/lisi/.bash_history

[root@learn ~]# find /home -gid 501

/home/lisi

/home/lisi/.bash_logout

/home/lisi/.bash_profile

/home/lisi/.bashrc

/home/lisi/.bash_history

按大小查找

查找空文件

[root@learn ~]# find . -empty

./1

查找大于1M的文件

[root@learn ~]# find /home -size +1M |xargs ls -lh

-rwxr-xr-x 1 root root 1.2M Feb 26  2016 /home/vmware-tools-distrib/caf/usr/lib/vmware-caf/pme/lib/libCommAmqpIntegration.so

-rwxr-xr-x 1 root root 2.8M Feb 26  2016 /home/vmware-tools-distrib/caf/usr/lib/vmware-caf/pme/lib/libFramework.so

-r-xr-xr-x 1 root root 1.6M Feb 26  2016 /home/vmware-tools-distrib/caf/usr/lib/vmware-caf/pme/lib/libglib-2.0.so.0.3400.3

查找小于1M的文件

[root@learn ~]# find /home -size -1M |xargs ls -lh

-rw-r--r-- 1 root root 0 Jan 19 21:45 /home/123

-rw-r--r-- 1 root root 0 Jan 16 14:13 /home/lisi/2.txt

-rw-r--r-- 1 root root 0 Jan 16 14:04 /home/lisi/91/1.txt

-rwxrwxrwx 1 root root 0 Jan 17 09:19 /home/lisi/91/456

-rw-r--r-- 1 root root 0 Jan 17 17:14 /home/lisi/c

-rw-r--r-- 1 root root 0 Jan 17 17:14 /home/lisi/homework

-rw-r--r-- 1 root root 0 Feb 26  2016 /home/vmware-tools-distrib/etc/not_configured

文本查看命令

cat

显示文本文件内容单个或多个

-n 显示行号

-E 显示换行符

查看并显示当文件

[root@learn ~]# cat old.txt

111

222

333

444

555i name is

i what

what is

查看并显示多个文件

[root@learn ~]# cat old.txt oldboy.txt

111

222

333

444

555i name is

i what

what is

11111

显示文件内容行号

[root@learn ~]# cat -n old.txt

     1 111

     2 222

     3 333

     4 444

     5 555i name is

     6 i what

     7 what is

显示文件换行符

[root@learn ~]# cat -E old.txt

111$

222$

333$

444$

555i name is$

i what $

what is$

tac

按行号逆向显示文本内容

[root@learn ~]# tac old.txt

what is

i what

555i name is

444

333

222

111

more

分屏显示文本文件内容,从前往后翻;

b键往前翻一行,翻到文件的尾部显示结束则不能往前翻。

回车 往下翻一行;空格 往下翻一屏

less

分屏显示

向下翻一行   向下键或回车

向上翻一行   向上键

向下翻一屏   pgdn或空格键

向上翻一屏   pgupb

查找功能     /或?

退出less命令  q

head

显示文本文件的前几行;默认是10行,可加参数定义需要的行数  -n 2

[root@learn ~]# head old.txt

111

222

333

444

555i name is

i what

what is

[root@learn ~]# head -2 old.txt

111

222

[root@learn ~]# head -n 2 old.txt

111

222

tail

显示文本文件的后几行;默认是10行,可加参数定义需要的行数  -n 2

-ffollow) 一直(实时)跟踪 不再退出

默认显示

[root@learn ~]# tail /etc/passwd

vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

abrt:x:173:173::/etc/abrt:/sbin/nologin

haldaemon:x:68:68:HAL daemon:/:/sbin/nologin

ntp:x:38:38::/etc/ntp:/sbin/nologin

saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

tcpdump:x:72:72::/:/sbin/nologin

li:x:500:500::/home/li:/bin/bash

lisi:x:501:501::/home/lisi:/bin/bash

显示2

[root@learn ~]# tail -2 /etc/passwd

li:x:500:500::/home/li:/bin/bash

lisi:x:501:501::/home/lisi:/bin/bash

[root@learn ~]# tail -n 2 /etc/passwd

li:x:500:500::/home/li:/bin/bash

lisi:x:501:501::/home/lisi:/bin/bash

实时显示(一直跟踪)

[root@learn ~]# tail -f /etc/passwd

vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

abrt:x:173:173::/etc/abrt:/sbin/nologin

haldaemon:x:68:68:HAL daemon:/:/sbin/nologin

ntp:x:38:38::/etc/ntp:/sbin/nologin

saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

tcpdump:x:72:72::/:/sbin/nologin

li:x:500:500::/home/li:/bin/bash

lisi:x:501:501::/home/lisi:/bin/bash

文本编辑器

vi vim

nano

winscp

文本操作命令

按照我们想要的格式显示文件内容,可以对文件内容进行过滤、改变样式输出。

cut(切开)

将文件使用指定的字符切开,显示我们想要的

使用冒号分割passwd文件,显示第1,35

trtranslate翻译、转化)

能把文件中指定的字符进行替换,该命令后面只能使用标准输入重定向

使用A替换passwd文件中的a字符

[root@learn ~]# tr ‘a‘ ‘A‘ <passwd

root:x:0:0:root:/root:/bin/bAsh

删除passwd文件中的:字符

[root@learn ~]# tr -d ‘:‘ <passwd

rootx00root/root/bin/bash

binx11bin/bin/sbin/nologin

删除passwd文件中的空格,原理同上。

显示passwd文件中后2行,并以:号分割取1,5列,替换:号为空格

[root@learn ~]# tail -2 passwd |cut -d: -f 1,3 |tr ":" " "

li 500

lisi 501

wcword count

字数统计,可以统计文件的行数、单词数量、字符数量

 

-l统计行数

-w统计单词数

-c统计字符数量(包含空格)

 

统计行数、单词数、字符数

[root@learn ~]# wc passwd

  26   35 1126 passwd

统计行数

[root@learn ~]# wc -l passwd

26 passwd

统计单词数

[root@learn ~]# wc -w passwd

35 passwd

统计字符数

[root@learn ~]# wc -c passwd

1126 passwd

利用分割命令显示指定分割数

[root@learn ~]# wc -c passwd |cut -d ‘ ‘ -f 1

1126

[root@learn ~]# wc -c passwd |cut -d ‘ ‘ -f 2

passwd

[root@learn ~]# wc -c passwd |cut -d ‘ ‘ -f 3

 

sort

对文件中的行进行排序(默认按字符(a-z)排序(生序))

-r 反转(z-a)排序(降序)

-t 指定分割符

-k 指定列

-u 去重

-n 按数值排序

-f 忽略大小写

 

users文件的行进行排序输出(升序)

[root@learn ~]# sort users

jia aa 33

li bb 12

wang bb 23

zhang aa 12

users文件的行进行排序输出(降序)

[root@learn ~]# sort -r users

zhang aa 12

wang bb 23

li bb 12

jia aa 33

使用空格把users文件分割 使用第3列排序

[root@learn ~]# sort -t ‘ ‘ -k 3 users

li bb 12

zhang aa 12

wang bb 23

jia aa 33

使用空格把users文件分割 使用第3列排序 去掉重复行

[root@learn ~]# sort -t ‘ ‘ -k 3 -u users

zhang aa 12

wang bb 23

jia aa 33

按数值排序

[root@learn ~]# sort -t‘ ‘ -k3 -n users

bin aa 9

li bb 12

zhang aa 12

wang bb 23

jia aa 33

uniqunique

唯一的意思;针对文本中的连续的重复行进行操作

-d 只显示重复的行

-D 显示全部重复的行

-c 显示重复次数

 

不显示重复行

[root@learn ~]# uniq uniqtest

zhang aa 12

wang bb 11

li bb 11

zhang aa 12

li bb 11

只显示重复的行

[root@learn ~]# uniq -d uniqtest

zhang aa 12

li bb 11

显示全部重复的行

[root@learn ~]# uniq -D uniqtest

zhang aa 12

zhang aa 12

zhang aa 12

li bb 11

li bb 11

显示重复次数

[root@learn ~]# uniq -c uniqtest

      3 zhang aa 12

      1 wang bb 11

      1 li bb 11

      1 zhang aa 12

      2 li bb 11

      1

grep

grepglobal re printing)全局搜索正则表达式并将搜索到的行打印出来

reregular expression)正则表达式,由一些元字符和其他组成的表达式;元字符是由特定意义的字符并不代表这个字符本身。

文本过滤工具能够根据指定的正则表达式逐行扫描文件的内容,只要行中有满足正则表达式的内容则整行被显示。

re

basic re  基本正则表达式

extended re 扩展正则表达式

1) grep命令语法

grep[option]...‘re‘ file...

-v 显示不匹配的行

-i 不区分大小写

-o 只显示匹配的字符串

-A# 显示找到的行以及后面的行 #表示数字

-B# 显示找到的行以及前面的行 #表示数字

-C# 显示找到的行以及前后的行 #表示数字

-E 表示后面的表达式是扩展的正则表达式

--color=auto 将匹配的字符串改变颜色显示

 

显示匹配的行

[root@learn ~]# grep ‘cpu‘ /proc/cpuinfo

cpu family : 6

cpu MHz : 2600.069

cpu cores : 1

cpuid level : 13

显示匹配行进行取反

[root@learn ~]# grep -v ‘cpu‘ /proc/cpuinfo

processor : 0

vendor_id : GenuineIntel

model : 60

model name : Intel(R) Core(TM) i5-4210M CPU @ 2.60GHz

stepping : 3

microcode : 28

cache size : 3072 KB

physical id : 0

siblings : 1

core id : 0

apicid : 0

initial apicid : 0

fpu : yes

fpu_exception : yes

wp : yes

flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc up arch_perfmon pebs bts xtopology tsc_reliable nonstop_tsc aperfmperf unfair_spinlock pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm ida arat epb pln pts dts fsgsbase smep

bogomips : 5200.13

clflush size : 64

cache_alignment : 64

address sizes : 40 bits physical, 48 bits virtual

power management:

显示匹配行并将匹配的字符加色

[root@learn ~]# grep ‘cpu‘ --color=auto /proc/cpuinfo

cpu family : 6

cpu MHz : 2600.069

cpu cores : 1

cpuid level : 13

[root@learn ~]# grep ‘cpu‘ --color=auto -o /proc/cpuinfo

cpu

cpu

cpu

cpu

显示匹配行不区分大小写并显示颜色

[root@learn ~]# grep ‘cpu‘ --color=auto -i /proc/cpuinfo

cpu family : 6

model name : Intel(R) Core(TM) i5-4210M CPU @ 2.60GHz

cpu MHz : 2600.069

cpu cores : 1

cpuid level : 13

显示匹配行以及后一行

[root@learn ~]# grep ‘cpu‘ --color=auto -A1 /proc/cpuinfo

cpu family : 6

model : 60

--

cpu MHz : 2600.069

cache size : 3072 KB

--

cpu cores : 1

apicid : 0

--

cpuid level : 13

wp : yes

显示匹配行以及后两行

[root@learn ~]# grep ‘cpu‘ --color=auto -A2 /proc/cpuinfo

cpu family : 6

model : 60

model name : Intel(R) Core(TM) i5-4210M CPU @ 2.60GHz

--

cpu MHz : 2600.069

cache size : 3072 KB

physical id : 0

--

cpu cores : 1

apicid : 0

initial apicid : 0

--

cpuid level : 13

wp : yes

flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc up arch_perfmon pebs bts xtopology tsc_reliable nonstop_tsc aperfmperf unfair_spinlock pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm ida arat epb pln pts dts fsgsbase smep

显示匹配行以及前一行

[root@learn ~]# grep ‘cpu‘ --color=auto -B1 /proc/cpuinfo

vendor_id : GenuineIntel

cpu family : 6

--

microcode : 28

cpu MHz : 2600.069

--

core id : 0

cpu cores : 1

--

fpu_exception : yes

cpuid level : 13

显示匹配行以及前后一行

[root@learn ~]# grep ‘cpu‘ --color=auto -C1 /proc/cpuinfo

vendor_id : GenuineIntel

cpu family : 6

model : 60

--

microcode : 28

cpu MHz : 2600.069

cache size : 3072 KB

--

core id : 0

cpu cores : 1

apicid : 0

--

fpu_exception : yes

cpuid level : 13

wp : yes

将命令结果通过管道匹配相关行

[root@learn ~]# ifconfig |grep ‘addr‘

eth0      Link encap:Ethernet  HWaddr 00:0C:29:41:FA:11  

          inet addr:192.168.99.189  Bcast:192.168.99.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fe41:fa11/64 Scope:Link

eth1      Link encap:Ethernet  HWaddr 00:0C:29:41:FA:1B  

          inet addr:192.168.10.100  Bcast:192.168.10.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fe41:fa1b/64 Scope:Link

          inet addr:127.0.0.1  Mask:255.0.0.0

          inet6 addr: ::1/128 Scope:Host

基本正则表达式

文件名通配符

*代表任意长度任意字符

?代表任意单个字符

[]指定范围内的任意单个字符

[^]指定范围外的任意当字符

1) 正则表达式的元字符

.匹配任意单个字符

[]匹配指定范围内的任意单个字符

[^]匹配指定范围外的任意单个字符

.*任意长度任意字符

 

匹配任意单个字符

[root@learn ~]# echo ‘a2c a3c abc adb bad‘ |grep ‘a.c‘ --color=auto

a2c a3c abc adb bad

[root@learn ~]# echo ‘a2c a3c abc adb bad‘ |grep ‘[a.c]‘ --color=auto

a2c a3c abc adb bad

匹配指定范围内的任意单个字符

[root@learn ~]# echo ‘a2c a3c abc adb bad‘ |grep ‘[a]‘ --color=auto

a2c a3c abc adb bad

[root@learn ~]# echo ‘a2c a3c abc adb bad‘ |grep ‘a[a-z]c‘ --color=auto

a2c a3c abc adb bad

匹配指定范围外的任意单个字符

[root@learn ~]# echo ‘a2c a3c abc adb bad‘ |grep ‘a[^a-z]c‘ --color=auto

a2c a3c abc adb bad

匹配任意长度任意字符

[root@learn ~]# echo ‘ac asc assc asssc asdc‘ |grep ‘a.*c‘ --color=auto

ac asc assc asssc asdc

7) 匹配次数

* 匹配前一个字符01次或多次  写法:ab*c   ac abc abbc abbdc

\? 匹配前一个字符0次或1次  写法:ab\?c  ac abc abbc

\{m,n\} 匹配前一个字符最少m次,最多n

\{0,n\} 最多n

\{m\} 最少m

as\{0,2\}c  ac asc assc asssc asdc

 

匹配前一个字符0次或多次

[root@learn ~]# echo ‘ac abc abbc abbdc abbbc‘ |grep ‘ab*c‘ --color=auto

ac abc abbc abbdc abbbc

匹配前一个字符0次或1

[root@learn ~]# echo ‘ac abc abbc‘ |grep ‘ab\?c‘ --color=auto

ac abc abbc

匹配最少0次最多2

[root@learn ~]# echo ‘ac asc assc asssc asdc‘ |grep ‘as\{0,2\}c‘ --color=auto

ac asc assc asssc asdc

[root@learn ~]# echo ‘ac asc assc asssc asdc‘ |grep ‘a[a-z]\{0,2\}c‘ --color=auto

ac asc assc asssc asdc

8) 分组匹配

\(\)实现分组匹配

连续引用

[root@learn ~]# echo ‘91abcabcabcabc51 fdsfsf‘ |grep ‘91\(abc\)*51‘ --color=auto

91abcabcabcabc51 fdsfsf

[root@learn ~]# echo ‘91abcabcabcabc51 fdsfsf‘ |grep ‘91\(abc\)\{4\}51‘ --color=auto

91abcabcabcabc51 fdsfsf

[root@learn ~]# echo ‘91abcabcabcabc51 91abcabcabc51 fdsfsf‘ |grep ‘91\(abc\)\{0,4\}51‘ --color=auto

91abcabcabcabc51 91abcabcabc51 fdsfsf

不连续引用

[root@learn ~]# cat 123

zhangsan,math=good,english=bad

han,math=good,english=good

yixiu,math=bad,english=good

xiaomo,math=bad,english=bad,music=good,geography=good

yuan,math=good,english=bad,music=good,geography=good

[root@learn ~]# grep ‘.*,math=\(.*\),english=\1‘ 123 --color=auto

han,math=good,english=good

xiaomo,math=bad,english=bad,music=good,geography=good

向后引用;

前后取值一致,前面括号内正则的值通过数字(用数字表示第几个括号)取值到后面。

[root@learn ~]# grep ‘.*,math=\(.*\),english=\1,music=\(good\),geography=\2‘ 123 --color=auto

xiaomo,math=bad,english=bad,music=good,geography=good

9) 锚定符

\< 锚定词首

\> 锚定词尾

\<  \> 锚定单词

[root@learn ~]# grep ‘\<r..t‘ --color=auto t1

root chroot rooter

this is root

[root@learn ~]# grep ‘r..t\>‘ --color=auto t1

root chroot rooter

this is root

[root@learn ~]# grep ‘\<r..t\>‘ --color=auto t1

root chroot rooter

this is root

^ 锚定行首

$ 锚定行尾

 

匹配行首为空一个或多个后面是root开始的行

[root@learn ~]# grep ‘^[[:space:]]*root‘ --color=auto t1

      root chroot rooter

匹配r任意字符t结尾的行

[root@learn ~]# grep ‘r..t$‘ --color=auto t1

      root chroot rooter root

匹配标点字符(punct)结尾的行

[root@learn ~]# grep ‘root[[:punct:]]\?$‘ --color=auto t1

      root chroot rooter root

this is root.

[root@learn ~]# grep ‘root[[:punct:]]\{1\}$‘ --color=auto t1

this is root.

[root@learn ~]# grep ‘root[[:punct:]]‘ --color=auto t1

this is root.

10) POSIX定义了一些只能在正则表达式中使用的字符类

alnum 字母和数字

alpha 字母

blank 仅表示空格或制表符

cntrl 控制字符

digit 十进制数

graph 打印字符,不包含空格

lower 小写字母

print 打印字符,包括空格

punct 打印字符,不包含字母和数字。

space 空白

upper 大写字母

xdigit 十六进制数

11) 练习题

1.找出ifconfig命令结果中1-255之间的数字。

[root@learn ~]# ifconfig eth0|grep ‘[1-9]\{1,3\}‘ --color=auto -o |xargs

29 41 11 192 168 99 189 192 168 99 255 255 255 255 6 8 2 29 41 11 64 15 1 211 31 1 662 1 221 679 21 25 751 6 2 3

2.找出ifconfig命令结果中的ip地址。

[root@learn ~]# ifconfig eth0|grep ‘\([[:digit:]]\{1,3\}\.\)\{3\}[[:digit:]]\{1,3\}‘ --color=auto -o

192.168.99.189

192.168.99.255

255.255.255.0

3.分析文件rc  找出第4位字符数值和最后一位字符数值一样的行

[root@learn ~]# cat rc

L1:1:wait:/etc/rc.d/rc 1

L3:3:wait:/etc/rc.d/rc 3

L1:1:wait:/etc/rc.d/rc 3

L3:1:wait:/etc/rc.d/rc 3

[root@learn ~]# grep ‘L.:\(.\).*\1‘ rc

L1:1:wait:/etc/rc.d/rc 1

L3:3:wait:/etc/rc.d/rc 3

4.找出当前系统上名字为li的用户账户相关信息/etc/passwd,注意不要找到lisili1这样的用户

[root@learn ~]# grep ‘^li:‘ /etc/passwd

li:x:500:500::/home/li:/bin/bash

扩展正则表达式

1) 元字符

.匹配任意单个字符

[]匹配指定范围内的任意单个字符

[^]匹配指定范围外的任意单个字符

12) 匹配次数

* 匹配前一个字符01次或多次

\? 匹配前一个字符0次或1

新增

+至少匹配一次

[root@learn ~]# echo "rooteeeeer" |egrep ‘ro+te*‘ --color

rooteeeeer

{m,n}括号前不需要转义字符\

[root@learn ~]# echo ‘rooteeeeer‘ |egrep ‘e{2,6}‘ --color

rooteeeeer

[root@learn ~]# echo ‘rooteeeeer‘ |egrep ‘ro+te{2,6}‘ --color

rooteeeeer

13) 锚定符

^ 锚定行首

$ 锚定行尾

新增

\< or \b 锚定词首

\> or \b 锚定词尾

[root@learn ~]# echo ‘chroot root rooter123‘ |egrep ‘\bro+ter[[:digit:]]*\b‘ --color

chroot root rooter123

14) 或者

| abc|wer

[root@learn ~]# echo ‘abcde abwde abc wde‘ |egrep ‘abc|wde‘ --color

abcde abwde abc wde

[root@learn ~]# echo ‘abcde abwde abc wde‘ |egrep ‘ab(c|w)de‘ --color

abcde abwde abc wde

15) 分组

( )分组不用转义字符\

 

[root@learn ~]# echo ‘a=10b=20c=10d=20‘ |egrep ‘a=(..)b=(..)c=\1d=\2‘ --color

a=10b=20c=10d=20

[root@learn ~]# echo ‘a=10b=20c=10d=20‘ |egrep ‘a=(..)b=(..)c=\1‘ --color

a=10b=20c=10d=20

[root@learn ~]# echo ‘a=10b=20c=10d=20‘ |grep -E ‘a=(..)b=(..)c=\1‘ --color

a=10b=20c=10d=20

16) fgrep

不解析正则表达式直接搜索文本

[root@learn ~]# echo ‘abcfr123a{1,2}wde‘ |fgrep ‘wde‘ --color

abcfr123a{1,2}wde

[root@learn ~]# echo ‘abcfr123a{1,2}wde‘ |fgrep ‘abc‘ --color

abcfr123a{1,2}wde

17) 写正则表达式需要注意的两点

1. 转义字符\

[root@learn ~]# echo ‘91xueit\(han\)*51cto‘ |grep ‘91xueit\(han\)*51cto‘ --color

[root@learn ~]# echo ‘91xueit\(han\)*51cto‘ |grep ‘91xueit\\(han\\)\*51cto‘ --color

91xueit\(han\)*51cto

将正则表达式中元字符转换成字符本身

2. 如果正则表达式中有命令替换,正则表达式只能使用" " 弱引用

如果正则表示式中没命令替换,可以使用 ‘ ‘ 强引用 " " 弱引用

[root@learn ~]# echo ‘abc/root123‘ |grep ‘abc$(pwd)123‘ --color

[root@learn ~]# echo ‘abc/root123‘ |grep "abc$(pwd)123" --color

abc/root123

第四章 常用命令

不是所有命令

关机命令shutdownhaltpoweroff

提示所有终端要进行关机操作

shutdown -c 取消关机操作

shutdown -r 重启

shutdown -r now现在重启

shutdown +5 5分钟关机

shutdown -h 02:23 & 后台运行在223分关机

shutdown -r 02:23 & 后台运行在223分重启

日常使用shutdown命令关机,此命名有提示其他终端。其他两个命令无提示。

重启命令reboot

时间命令

cal

日历

[root@learn ~]# cal

    February 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

date

显示系统时间

-s 设置系统时间

[root@learn ~]# date -s ‘2017/02/04 21:27‘

Sat Feb  4 21:27:00 CST 2017

hwclock

查看主板时间

[root@learn ~]# hwclock

Sat 04 Feb 2017 09:26:19 PM CST  -0.798354 seconds

-w将系统时间写入主板时间

[root@learn ~]# hwclock -w

[root@learn ~]# hwclock

Sat 04 Feb 2017 09:27:19 PM CST  -0.768415 seconds

linux终端和shell