首先,我们来了解下,什么是linux命令?
命令本身是一个可执行的二进制格式程序文件(Linux哲学思想:一切皆文件)。
在命令行的命令提示符后键入命令,即意味着“命令请求kernel将某个二进制程序运行为一个进程”。程序本身只是一个静态文件,只有通过这个请求,才会转化为有生命周期的动态进程。
命令一般都存放在这些目录下:
普通命令:/bin,/usr/bin,/usr/local/bin
管理命令:/sbin,/usr/sbin,/usr/local/sbin
命令往往会调用到许多库文件,这些库可能许多程序都会遇到,我们形象的称之为共享库。这些共享库也是二进制文件,但是由于其没有程序执行入口,所以不能独立执行,能被其他有执行入口的程序调用时才能执行。
一般这些库都存放在这些录下:
32bits的库:/lib,/usr/lib,/usr/local/lib
64bits的库:/lib64,/usr/lib64,/usr/local/lib64
注意:并非所有命令都有一个在某目录与之相对应的可执行程序文件
接下来,我们介绍linux命令的一些有关知识:
一,CLI命令行接口
[jeffrey@localhost ~]$
[root@localhost jeffrey]#
我们以[jeffrey@localhost ~]$
为例,对各个元素的意义进行解释;
root 或 jeffrey : 指的是当前登陆的账户(root说明登陆的是管理员,jeffrey说明现在登陆的是一个名为Jeffrey的普通用户)
localhost : 指的是我当前的主机名为localhost
~ : 指的是我当前所在目录为(代表的是当前用户的家目录)
# : 命令提示符
对于linux而言,命令行提示符有如下两种:
# : 表明当前登陆的是管理员账户,
$ : 表明当前登陆的是普通用户
不同的命令提示符代表着不同的用户:
# : 管理员账号,拥有最高权限,能执行所有操作
$ : 普通用户,不具有管理权限
二,命令组成
COMMAND [Options] [Arguments]
COMMAND :命令的主体
Options :选项,分为长选项和短选项。
长选项形式例如- -a,
短选项形式为-a -b,还可写为-ab.
Arguments :参数,即命令的作用对象.
三,linux命令类型
Linux命令大致可分为两种:外部命令和shell内嵌命令。
-
外部命令 :
在系统中,有与该命令同名的文件存在;
-
shell内嵌命令:
不依赖于外部文件;
由此可以一处今天的第一个命令:
type : 查看命令类型
用法:
type COMMAND
示例:
[root@localhost jeffrey]# type mkdir
mkdir 是 /usr/bin/mkdir
[root@localhost jeffrey]# type alias
alias is a shell builtin
由示例可知:mkdir
文件路径为 /usr/bin/mkdir,所以为外部命令;而 alias
为shell内嵌命令;
,命令使用帮助
1,对于内部命令,使用‘help’
help COMMAND
2,对于外部命令,使用长选项‘- - help’
>COMMAND - -help
3 , 公用命令手册manual(一般用于外部命令)
man共分为8个章节(chapters):
(1)用户命令(/bin,/usr/bin,/usr/local/sbin)
(2)系统调用
(3)库调用
(4)特殊文件(设备文件)
(5)文件格式(配置文件的语法)
(6)游戏
(7)杂项(Miscellaneous)
(8)管理命令(/bin,/usr/bin,/usr/local/sbin)
man COMMAND
显示8个章节中包含的第一个COMMAND的详细信息
man N COMMAND
显示第N个章节中包含的COMMAND的详细信息
4,帮助文件符号的意义
< > :必选
[ ] :可选可省
······· :可出现多次,即多参数
| :多选一,即或
{ } :分组
5,帮助文档的信息模块
NAME : 命令名称及功能简要说明
SYNOPSIS : 用法说明,包括可用的选项
DESCRIPTION : 命令功能的详尽说明,可能包含每个选项的意义
OPTIONS : 说明每个选项的意义
FILES : 此命令相关的配置文件
BUGS : 有关BUG的相关处理
EXAMPLES : 命令的使用示例
6,帮助文档的基本操作
向前翻一屏 Space
向后翻一屏 b
向前翻一行 Enter/J/向上方向键
向后翻一行 K/向下方向键
查找字符串 /KEYWORD
n 切换到前一个字符串
N 切换到下一个字符串
7, 其他获得帮助的方法
在线帮助文档 info COMMAND
本地文档 /usr/share/doc
网络资源 Google/Baidu
五,基本命令
su : 用户之间的切换
用法:
[root#]su USER 切换到用户USER
选项:
-l 显示详细信息
示例:
[root@localhost jeffrey]# su -l jeffrey
Last login: Mon Sep 3 10:13:53 PDT 2018 on :0
exit : 切换到前一个用户
示例:
[root@localhost jeffrey]# exit
exit
[jeffrey@localhost ~]$ exit
logout
[root@localhost jeffrey]# exit
exit
passwd : 修改用户密码
用法:
passwd USER 修改USER的密码(仅限于root超级用户)
passwd 修改当前用户的密码
示例:
[jeffrey@localhost ~]$ passwd
Changing password for user jeffrey.
Changing password for jeffrey.
(current) UNIX password:
New password:
[root@localhost jeffrey]# passwd jeffrey
Changing password for user jeffrey.
New password:
注:
用户设置密码的几点原则:
1,密码至少使用大、小写字母,数字,特殊字符这四种字符中的三种
2,密码长度要大于7位
3,密码要是使用随机字符串
4,密码要定期更换
5,密码循环周期要大,即尽量不使用近期已使用过的密码
hash : 显示缓存的命令
示例:
[root@localhost jeffrey]# hash
hits command
1 /usr/bin/passwd
######linux系统启动后系统时钟从硬件时钟读取日期和时间信息,读取完成以后,就不再与硬件相关联。
date : 显示当前系统时间
用法:
# date [OPTION]… [+FORMAT] 按指定格式显示日期
FORMAT: 格式符
%F : full date; same as %Y-%m-%d
%T : time; same as %H:%M:%S
%Y : year
%m : month (01…12)
%d : day of month (e.g., 01)
%H : hour (00…23)
%M : minute (00…59)
%S : second (00…60)
%s :seconds since 1970-01-01 00:00:00 UTC,显示从1970年1月1日(unix元年)0时0分0秒到命令执行那一刻的秒数
示例:
[root@localhost jeffrey]# date
Thu Sep 6 17:02:41 PDT 2018
[root@localhost jeffrey]# date +%F
2018-09-06
[root@localhost jeffrey]# date +%T
17:02:58
[root@localhost jeffrey]# date +"%F %T"
2018-09-06 17:03:34
[root@localhost jeffrey]# date +%Y
2018
[root@localhost jeffrey]# date +%m
09
[root@localhost jeffrey]# date +%d
06
[root@localhost jeffrey]# date +%h
Sep
[root@localhost jeffrey]# date +%H
17
[root@localhost jeffrey]# date +%M
04
[root@localhost jeffrey]# date +%S
31
[root@localhost jeffrey]# date +%s
1536278677
# date [-u|–utc|–universal] [MMDDhhmm[[CC]YY][.ss]] 修改系统时间为指定时间
示例:
[root@localhost jeffrey]# date 090616572018.50
Thu Sep 6 16:57:50 PDT 2018
[root@localhost jeffrey]# date
Thu Sep 6 16:57:53 PDT 2018
clock : 显示当前硬件时间
选项:
-s : 以硬件时钟设置系统时间(–hctosys Set the System Time from the Hardware Clock.)
-w : 以系统时间设置硬件时间(–systohc Set the Hardware Clock to the current System Time.)
示例:
[root@localhost jeffrey]# date
Thu Sep 6 17:16:51 PDT 2018
[root@localhost jeffrey]# clock
Thu 06 Sep 2018 02:18:18 AM PDT -0.839017 seconds
[root@localhost jeffrey]# clock -w
[root@localhost jeffrey]# clock
Thu 06 Sep 2018 05:17:03 PM PDT -0.393197 seconds
cal : 显示日历
用法:
# cal [[month] year]
示例:
[root@localhost jeffrey]# cal
September 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
[root@localhost jeffrey]# cal 2018
2018
January February March
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 1 2 3 1 2 3
7 8 9 10 11 12 13 4 5 6 7 8 9 10 4 5 6 7 8 9 10
14 15 16 17 18 19 20 11 12 13 14 15 16 17 11 12 13 14 15 16 17
21 22 23 24 25 26 27 18 19 20 21 22 23 24 18 19 20 21 22 23 24
28 29 30 31 25 26 27 28 25 26 27 28 29 30 31
April May June
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7 1 2 3 4 5 1 2
8 9 10 11 12 13 14 6 7 8 9 10 11 12 3 4 5 6 7 8 9
15 16 17 18 19 20 21 13 14 15 16 17 18 19 10 11 12 13 14 15 16
22 23 24 25 26 27 28 20 21 22 23 24 25 26 17 18 19 20 21 22 23
29 30 27 28 29 30 31 24 25 26 27 28 29 30
July August September
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7 1 2 3 4 1
8 9 10 11 12 13 14 5 6 7 8 9 10 11 2 3 4 5 6 7 8
15 16 17 18 19 20 21 12 13 14 15 16 17 18 9 10 11 12 13 14 15
22 23 24 25 26 27 28 19 20 21 22 23 24 25 16 17 18 19 20 21 22
29 30 31 26 27 28 29 30 31 23 24 25 26 27 28 29
30
October November December
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 1 2 3 1
7 8 9 10 11 12 13 4 5 6 7 8 9 10 2 3 4 5 6 7 8
14 15 16 17 18 19 20 11 12 13 14 15 16 17 9 10 11 12 13 14 15
21 22 23 24 25 26 27 18 19 20 21 22 23 24 16 17 18 19 20 21 22
28 29 30 31 25 26 27 28 29 30 23 24 25 26 27 28 29
30 31
[root@localhost jeffrey]# cal 11 2018
November 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
ifconfig 或者 ip addr list : 查看IP地址
示例:IP地址为192.168.81.138
[root@localhost jeffrey]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.81.138 netmask 255.255.255.0 broadcast 192.168.81.255
inet6 fe80::7e9a:949c:77a0:537b prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:8a:27:86 txqueuelen 1000 (Ethernet)
RX packets 586233 bytes 669518835 (638.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 258152 bytes 17773974 (16.9 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 214 bytes 18244 (17.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 214 bytes 18244 (17.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:31:8d:e8 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost jeffrey]# ip addr list
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:8a:27:86 brd ff:ff:ff:ff:ff:ff
inet 192.168.81.138/24 brd 192.168.81.255 scope global dynamic ens33
valid_lft 1082sec preferred_lft 1082sec
inet6 fe80::7e9a:949c:77a0:537b/64 scope link
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
link/ether 52:54:00:31:8d:e8 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000
link/ether 52:54:00:31:8d:e8 brd ff:ff:ff:ff:ff:ff
ping : 查看网络连通性
示例:
[root@localhost jeffrey]# ping www.sohu.com
PING fwh.a.sohu.com (119.97.155.2) 56(84) bytes of data.
64 bytes from 2.155.97.119.broad.wh.hb.dynamic.163data.com.cn (119.97.155.2): icmp_seq=1 ttl=128 time=92.8 ms
64 bytes from 2.155.97.119.broad.wh.hb.dynamic.163data.com.cn (119.97.155.2): icmp_seq=2 ttl=128 time=95.1 ms
64 bytes from 2.155.97.119.broad.wh.hb.dynamic.163data.com.cn (119.97.155.2): icmp_seq=3 ttl=128 time=83.7 ms
64 bytes from 2.155.97.119.broad.wh.hb.dynamic.163data.com.cn (119.97.155.2): icmp_seq=4 ttl=128 time=82.6 ms
64 bytes from 2.155.97.119.broad.wh.hb.dynamic.163data.com.cn (119.97.155.2): icmp_seq=5 ttl=128 time=79.8 ms
64 bytes from 2.155.97.119.broad.wh.hb.dynamic.163data.com.cn (119.97.155.2): icmp_seq=6 ttl=128 time=79.1 ms
64 bytes from 2.155.97.119.broad.wh.hb.dynamic.163data.com.cn (119.97.155.2): icmp_seq=7 ttl=128 time=106 ms
64 bytes from 2.155.97.119.broad.wh.hb.dynamic.163data.com.cn (119.97.155.2): icmp_seq=8 ttl=128 time=95.4 ms
64 bytes from 2.155.97.119.broad.wh.hb.dynamic.163data.com.cn (119.97.155.2): icmp_seq=9 ttl=128 time=94.5 ms
64 bytes from 2.155.97.119.broad.wh.hb.dynamic.163data.com.cn (119.97.155.2): icmp_seq=10 ttl=128 time=92.2 ms
64 bytes from 2.155.97.119.broad.wh.hb.dynamic.163data.com.cn (119.97.155.2): icmp_seq=11 ttl=128 time=88.3 ms
echo : 回显
用法:
# echo [SHORT-OPTION]… [STRING]…
选项:
-n : 不进行换行
-e : 让转义符生效
\n : 换行
\t : 横向制表符
STRING可以使用单引号和双引号。
若使用单引号,表示强引用,变量引用不执行替换
[root@localhost jeffrey]# echo '$SHELL'
$SHELL
若使用双引号,表示弱引用,变量引用执行替换
[root@localhost jeffrey]# echo "$SHELL"
/bin/bash
示例:
[root@localhost jeffrey]# echo hello
hello
[root@localhost jeffrey]# echo hello world
hello world
echo $SHELL : 查看shell的类型
示例:
[root@localhost jeffrey]# echo $SHELL
/bin/bash
**tty ** : 查看终端设备
示例:
[root@localhost jeffrey]# tty
/dev/pts/0
poweroff : 立刻关机
halt : 立刻关机
reboot : 重启
shutdown: 关机或重启
sync:将内存中的数据写回到磁盘中,以保存当前的工作。常常在关机之前执行此命令,以防数据丢失。
主要介绍一下shutdown:
用法:
# shutdown [OPTIONS…] [TIME] [WALL…]
OPTIONS:
-H : 等价于halt
-P : 等价于poweroff
-h : 等价于poweroff
-r : 等价于reboot
-c : 取消上一次shutdown操作
TIME
now : 立刻执行
hh : mm 指定某时某分执行
+m : m分钟后执行
wall :
向每一个终端都发送信息( send a message to everybody’s terminal.)
示例:定时18:00执行关机操作,并取消
[root@localhost jeffrey]# shutdown -h 18:00
Shutdown scheduled for Thu 2018-09-06 18:00:00 PDT, use 'shutdown -c' to cancel.
[root@localhost jeffrey]#
Broadcast message from root@localhost.localdomain (Thu 2018-09-06 17:50:55 PDT):
The system is going down for power-off at Thu 2018-09-06 18:00:00 PDT!
[root@localhost jeffrey]# shutdown -c
Broadcast message from root@localhost.localdomain (Thu 2018-09-06 17:51:25 PDT):
The system shutdown has been cancelled at Thu 2018-09-06 17:52:25 PDT!
[root@localhost jeffrey]# shutdown -r +5
Shutdown scheduled for Thu 2018-09-06 17:57:50 PDT, use 'shutdown -c' to cancel.
[root@localhost jeffrey]#
Broadcast message from root@localhost.localdomain (Thu 2018-09-06 17:52:50 PDT):
The system is going down for reboot at Thu 2018-09-06 17:57:50 PDT!
[root@localhost jeffrey]# shutdown -c
Broadcast message from root@localhost.localdomain (Thu 2018-09-06 17:53:03 PDT):
The system shutdown has been cancelled at Thu 2018-09-06 17:54:02 PDT!
Ctrl + c : 终止命令的执行
下面我们再来介绍一些基础命令;
which : 显示命令的完整路径
用法:
which [options] program [......]
示例:查看·ls·命令的完整路径
[root@localhost jeffrey]# which ls
/usr/bin/ls
[root@localhost jeffrey]# which rm
alias rm='rm -i'
/usr/bin/rm
[root@localhost jeffrey]# which mv
alias mv='mv -i'
/usr/bin/mv
选项:
--skip-alias : 忽略别名
示例:与上面对比,没有了关于别名的提示;
[root@localhost jeffrey]# which --skip-alias rm
/usr/bin/rm
[root@localhost jeffrey]# which --skip-alias mv
/usr/bin/mv
**whereis 😗*显示命令的二进制文件、源码文件以及man手册页
用法:
whereis [options] name…
示例:查看·ls·命令的二进制文件、源码文件以及man手册页;
[root@localhost jeffrey]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
选项:
-b : 只显示二进制文件路径
-m : 只现实man手册文件路径
示例:
[root@localhost jeffrey]# whereis -b ls
ls: /usr/bin/ls
[root@localhost jeffrey]# whereis -m ls
ls: /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
who: 查看登录当前系统的用户(用户名、登录的终端、登陆时间及远程登陆的IP)
用法:
who [options]
示例:
[root@localhost jeffrey]# who
jeffrey :0 2018-08-29 07:27 (:0)
jeffrey pts/0 2018-08-29 07:28 (:0)
选项:
-b : 查看系统此次启动时间
-r : 查看用户运行级别
示例:
[root@localhost jeffrey]# who -b
system boot 2018-08-29 07:13
[root@localhost jeffrey]# who -r
run-level 5 2018-08-29 07:14
w : 显示登陆的用户正在进行的工作状态
示例:
[root@localhost jeffrey]# w
08:12:32 up 58 min, 2 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
jeffrey :0 :0 07:27 ?xdm? 1:19 0.29s gdm-session-worker [
jeffrey pts/0 :0 07:28 8.00s 0.21s 2.65s /usr/libexec/gnome-t
#####命令与别名
Linux中的命令允许设置别名,并且别名可以与原命令名相同;对于拥有别名的命令,其原名会被隐藏。
若要运行原命令而不是其设置的别名,可使用:
\COMMAND
例如:
[root@localhost jeffrey]# type ls
ls is aliased to `ls --color=auto'
[root@localhost jeffrey]# ls
Desktop Downloads Pictures Templates
Documents Music Public Videos
[root@localhost jeffrey]# \ls
Desktop Downloads Pictures Templates
Documents Music Public Videos
使用·type·命令查看·ls·的命令类型,可知,·ls·命令并不是真正的·ls·,而是’ls --color=auto’ 的别名; 所以,我们执行·ls·命令的时候,相当于执行的是’ls --color=auto’,这样,命令执行结果会带有颜色;如果我们想要运行原命令,则需要执行· \ ls ·。
结果如示例结果。(颜色的变化这里没有体现出来)
那么,怎么来设置别名呢?
alias : 设置命令的别名
用法:
- 直接运行·alias·可获取所有的别名
[root@localhost jeffrey]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
- 为指定命令设置别名
alias NAME=‘COMMAND’
示例:将’ ls -l ‘的别名设置为’ ls ',然后执行,观察结果;
[root@localhost jeffrey]# alias ls='ls -l'
[root@localhost jeffrey]# ls
total 0
drwxr-xr-x. 2 jeffrey jeffrey 6 Aug 29 07:27 Desktop
drwxr-xr-x. 2 jeffrey jeffrey 6 Aug 29 07:27 Documents
drwxr-xr-x. 2 jeffrey jeffrey 6 Aug 29 07:27 Downloads
drwxr-xr-x. 2 jeffrey jeffrey 6 Aug 29 07:27 Music
drwxr-xr-x. 2 jeffrey jeffrey 6 Aug 29 07:27 Pictures
drwxr-xr-x. 2 jeffrey jeffrey 6 Aug 29 07:27 Public
drwxr-xr-x. 2 jeffrey jeffrey 6 Aug 29 07:27 Templates
drwxr-xr-x. 2 jeffrey jeffrey 6 Aug 29 07:27 Videos
我们想要撤销’ ls -l '的别名,该怎么做呢?
unalias : 撤销命令的别名
用法:
unalias NAME
示例:当我们撤销掉’ ls ‘这个别名时,我们再次执行,发现’ ls '已经回到了原本的结果;
[root@localhost jeffrey]# unalias ls
[root@localhost jeffrey]# ls
Desktop Downloads Pictures Templates
Documents Music Public Videos
注意:关于命令别名的设置,只对当前shell进程有效。若想要长期有效,则需要将设置写入配置文件中。