马哥博客作业第一周

# 基础命令
# tty
tty命令可以查看当前所在的终端

范例:[root@Centos8[ ~]#tty
/dev/pts/0

# echo ${SHELL}
显示当前使用的shell
范例:[root@Centos8[ ~]#echo ${SHELL}
/bin/bash

# cat /etc/shells
显示当前系统使用的所有shell
范例:[root@Centos8[ ~]#cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
/usr/bin/tmux
/bin/tmux

# hostname NAME
设置主机名
范例:[root@centos8 ~]#hostname  bj-yz-k8s-node1- 100-10.magedu.com
注意:主机名不要使用下划线

# type, -a
区别指定的命令是内部或外部命令
内部命令不显示路径
范例:[root@Centos8[ ~]#type echo
echo is a shell builtin
外部命令显示路径
范例:[root@Centos8[ ~]#type hostname
hostname is /usr/bin/hostname

# help
内部命令列表
# enable
管理内部命令
范例:[root@Centos8[ ~]#enable
enable .
enable :
enable [
enable alias
enable bg
enable bind
enable break
## enable CMD
启用内部命令
## enable -n CMD
禁用内部命令
## enable -n
查看所有禁用的内部命令
范例:[root@Centos8[ ~]#enable -n cd
[root@Centos8[ ~]#enable -n
enable -n cd

。enable cmd 启用内部命令
。enable –n cmd 禁用内部命令
。enable –n 查看所有禁用的内部命令

# which
查看外部命令路径
范例:[root@Centos8[ ~]#which hostname
/usr/bin/hostname

# hash
缓存表
## hash -l
显示hash缓存,可作为输入使用
范例:[root@Centos8[ ~]#hash -l
builtin hash -p /usr/bin/tty tty
builtin hash -p /usr/bin/echo echo
builtin hash -p /usr/bin/cat cat
builtin hash -p /usr/bin/cd cd
## hash 命令常见用法
。hash 显示hash缓存
。hash -l 显示hash缓存,可作为输入使用
。hash -p path name 将命令全路径path起别名为name
。hash -t name 打印缓存中name的路径
。hash -d name 清除name缓存
。hash -r 清除缓存
范例:[root@Centos8[ ~]#hash -p huang name
[root@Centos8[ ~]#hash -t name
huang
[root@Centos8[ ~]#hash -d name
[root@Centos8[ ~]#hash -t name
-bash: hash: name: not found

# alias
定义别名 对于经常执行的较长的命令,可以将其定义成较短 的别名,以方便执行
显示当前shell进程所有可用的命令别名
范例:[root@Centos8[ ~]#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定义别名
格式:alias NAME='VALUE'
范例:[root@Centos8[ ~]#alias name='echo'
[root@Centos8[ ~]#name aaa
aaa
## unalias 取消别名
## unalias -a 取消所有别名
范例:[root@Centos8[ ~]#alias name='echo'
[root@Centos8[ ~]#name aaa
aaa
[root@Centos8[ ~]#unalias name
[root@Centos8[ ~]#name aaa
bash: name: command not found...

# 命令格式

[root@Centos8[ ~]#id -u huang
1000
[root@Centos8[ ~]#ls -a
[root@Centos8[ ~]#ls --all
[root@Centos8[ ~]#free -h
[root@Centos8[ ~]#free --human
[root@Centos8[ ~]#ps a

# 常见命令
查看硬件信息
# cpu
lscpu命令可以查看cpu信息
cat /proc/cpuinfo也可看查看到
```
范例:[root@Centos8[ ~]#lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 4
On-line CPU(s) list: 0-3
Thread(s) per core: 1
Core(s) per socket: 2
Socket(s): 2
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 158
Model name: Intel(R) Core(TM) i5-7300HQ CPU @ 2.50GHz
Stepping: 9
CPU MHz: 2495.999
BogoMIPS: 4991.99
Hypervisor vendor: VMware
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 6144K
NUMA node0 CPU(s): 0-3

```
# free 或 cat /proc/meminfo
查看内存大小
```
[root@Centos8[ ~]#free
total used free shared buff/cache available
Mem: 2012252 867568 621700 10920 522984 969012
Swap: 4194300 0 4194300
```
# cat /proc/meminfo
```
[15:21:28 root@Centos8[ ~]#cat /proc/meminfo
MemTotal: 2012252 kB
MemFree: 621616 kB
MemAvailable: 968928 kB
Buffers: 2184 kB
Cached: 454384 kB
SwapCached: 0 kB
Active: 479580 kB
Inactive: 355108 kB
Active(anon): 378948 kB
Inactive(anon): 10088 kB
Active(file): 100632 kB
Inactive(file): 345020 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 4194300 kB
SwapFree: 4194300 kB
Dirty: 0 kB
Writeback: 0 kB
AnonPages: 372112 kB
Mapped: 191452 kB
Shmem: 10920 kB
KReclaimable: 66416 kB
Slab: 205032 kB
SReclaimable: 66416 kB
SUnreclaim: 138616 kB
KernelStack: 8880 kB
PageTables: 25432 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 5200424 kB
Committed_AS: 2040784 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 0 kB
VmallocChunk: 0 kB
HardwareCorrupted: 0 kB
AnonHugePages: 133120 kB
ShmemHugePages: 0 kB
ShmemPmdMapped: 0 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
Hugetlb: 0 kB
DirectMap4k: 208768 kB
DirectMap2M: 1888256 kB
DirectMap1G: 0 kB
```
# lsblk
查看分区情况
```
范例:[root@Centos8[ ~]#lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 100G 0 part /
├─sda3 8:3 0 50G 0 part /data
├─sda4 8:4 0 1K 0 part
└─sda5 8:5 0 4G 0 part [SWAP]
sr0 11:0 1 7G 0 rom
```
# cat /proc/partitions
```
范例:[root@Centos8[ ~]#cat /proc/partitions
major minor #blocks name

8 0 209715200 sda
8 1 1048576 sda1
8 2 104857600 sda2
8 3 52428800 sda3
8 4 1 sda4
8 5 4194304 sda5
11 0 7377920 sr0

```
# uname -r
查看版本内容
```
[root@Centos8[ ~]#uname -r
4.18.0-147.el8.x86_64
```
# centos8 查看发行版本
cat /etc/redhat-release
```
[root@Centos8[ ~]#cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core)

[root@Centos8[ ~]#cat /etc/os-release
NAME="CentOS Linux"
VERSION="8 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Linux 8 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-8"
CENTOS_MANTISBT_PROJECT_VERSION="8"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="8"

范例:
lsb_release -a

```
# ubuntu 查看发行版本
```
cat /etc/os-release

huang@ubuntu1804:~$ cat /etc/os-release
NAME="Ubuntu"
VERSION="18.04.4 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.4 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic
```
```
cat /etc/is

[root@ubuntu1804 ~]#cat /etc/is
iscsi/   issue   issue.net
```
```
cat /etc/issue

huang@ubuntu1804:~$ cat /etc/issue
Ubuntu 18.04.4 LTS \n \l
```
```
lsb_release -a

huang@ubuntu1804:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.4 LTS
Release: 18.04
Codename: bionic

```

# 时间和日期

## date 显示和设置系统时间

date +%s

范例:[root@Centos8[ ~]#date +%s
1584778180

```
```
date -d @`date +%s`

范例:[root@Centos8[ ~]#date -d @`date +%s`
Sat Mar 21 16:12:17 CST 2020

```
```
date -d @1584689051

范例:[root@centos8 ~]#date -d @1584689051
Fri Mar 20 15:24:11 CST 2020
```
```
date -d @1584689051 +%F_%T

范例:[root@centos8 ~]#date -d @1584689051 +%F_%T
2020-03-20_15:24:11
```
# /etc/localtime
查看时区
```
范例:[root@Centos8[ ~]#ll /etc/localtime
lrwxrwxrwx. 1 root root 35 Mar 16 23:28 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai
```
```
cat /etc/timezone

root@ubuntu1804:~# cat /etc/timezone
Asia/Shanghai
```
# cal -y
显示日历
```
范例:[16:47:36 root@centos8 ~]#cal 9 1752
 September 1752
Su Mo Tu We Th Fr Sa
   1  2 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
```
# 关机
。halt
。poweroff
# 重启
reboot
-f: 强制,不调用shutdown
-p: 切断电源
# 关机或重启
格式:shutdown [OPTION]... [TIME] [MESSAGE]
-r: reboot
-h: halt
-c:cancel
TIME:无指定,默认相当于+1(CentOS7)
now: 立刻,相当于+0
+#: 相对时间表示法,几分钟之后;例如 +3
hh:mm: 绝对时间表示,指明具体时间

# 用户登录信息查看命令
。whoami: 显示当前登录有效用户
。who: 系统当前所有的登录会话
。w: 系统当前所有的登录会话及所做的操作
```
whoami

范例:[root@Centos8[ ~]#whoami
root
```
```
who

范例:[root@Centos8[ ~]#who
root pts/0 2020-03-21 12:41 (10.0.0.1)

```
```

w
范例:[root@Centos8[ ~]#w
17:28:11 up 4:50, 1 user, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 10.0.0.1 12:41 1.00s 6.42s 0.21s w

# screen

范例:安装screen
[root@centos7 ~]#yum -y install screen
```

# tmux

```
安装:yum install tmux
```
```
启动退出
[root@centos8 ~]#tmux
[root@centos8 ~]#exit
logout
```

# \转义符吧特殊符号的原有含义转换
```
范例:[root@Centos8[ ~]#echo \
> c
c
[22:18:50 root@Centos8[ ~]#echo \\
\

```

# echo 输出信息

```
语法:echo [-neE][字符串]
```

``
echo "$VAR_NAME”  #用变量值替换,弱引用 (双引号)
echo '$VAR_NAME’  #变量不会替换,强引用 (单引号)
```
# 命令扩展“”,‘’,``或 $( )
```
三者比较的范例:
[11:28:04 root@centos7[ ~]#echo '$PATH'
$PATH
[11:28:18 root@centos7[ ~]#echo "PATH"
PATH
[11:28:48 root@centos7[ ~]#echo `$PATH`
-bash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin: No such file or directory
```

以创建一个带日期的文件
[19:04:32 root@Centos8[ ~]#touch `date +%F.txt`
[19:05:06 root@Centos8[ ~]#ll
total 16
-rw-r--r--. 1 root root 0 Mar 22 19:05 2020-03-22.txt
-rw-------. 1 root root 1582 Mar 16 23:30 anaconda-ks.cfg

```
```
创建带主机名日期的文件
[19:07:56 root@Centos8[ ~]#touch `hostname`_`date +%F`.txt
[19:10:14 root@Centos8[ ~]#ll
total 16
-rw-r--r--. 1 root root 0 Mar 22 19:07 2020-03-22.txt
-rw-------. 1 root root 1582 Mar 16 23:30 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Mar 22 19:07 Centos8.magedu.org_
-rw-r--r--. 1 root root 0 Mar 22 19:10 Centos8.magedu.org_2020-03-22.txt


```
```
$( )可以嵌套
范例:[19:15:22 root@Centos8[ ~]#ll $(echo $(date +%F).txt)
-rw-r--r--. 1 root root 0 Mar 22 19:07 2020-03-22.txt

```

# {} 花括号
```
[19:25:37 root@Centos8[ ~]#echo file{1..10}.{txt.log}
file1.{txt.log} file2.{txt.log} file3.{txt.log} file4.{txt.log} file5.{txt.log} file6.{txt.log} file7.{txt.log} file8.{txt.log} file9.{txt.log} file10.{txt.log}
```
```
[19:23:29 root@Centos8[ ~]#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

```
```
[19:20:59 root@Centos8[ ~]#echo {1..10}
1 2 3 4 5 6 7 8 9 10
[19:21:08 root@Centos8[ ~]#echo {20..10..2}
20 18 16 14 12 10
[19:21:51 root@Centos8[ ~]#echo {20..10..3}
20 17 14 11

# tab 键补全

```
范例:[root@centos8 ~]#nmcli connection 2TAB
add   delete  edit   help   load   monitor show
clone  down   export  import  modify  reload  up
```

# history
可以列出所有执行过的命令历史
```
命令历史存在这个文件中 .bash_history
cat .bash_history
正常退出才会存住
```
可以反复调用 !(1..) 倒数 !(-1..)
```
范例: 132 w
133 nmcli
134 nmcli connection
135 nmcli connection
136 hisory
137 histroy
138 history
139 cat .bash_history
140 logout
141 cat .bash_history
142 history
[20:14:23 root@Centos8[ ~]#!132
w
20:14:34 up 1:50, 1 user, load average: 0.00, 0.02, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 10.0.0.1 20:10 2.00s 0.42s 0.01s w

```
默认只记录最近的1000条 可以更改
```
nano /etc/profile
```

# whatis

```
#CentOS 7 版本以后
mandb
#CentOS 6 版本之前
makewhatis
```

范例:
[20:58:51 root@Centos8[ ~]#whatis ls
ls (1) - list directory contents
ls (1p) - list directory contents
[20:59:00 root@Centos8[ ~]#man -f ls
ls (1) - list directory contents
ls (1p) - list directory contents

```

# 查看命令的帮助
内部命令:
。help COMMAND
。man bash

# man 帮助

``
#CentOS 6 之前版 man 的配置文件
/etc/man.config
#CentOS 7 之后版 man 的配置文件
/etc/man_db.conf
#ubuntu man 的配置文件
/etc/manpath.config
```

# 文件管理
bin ->usr/bin 二进制文件
boot 启动文件
dev 硬件设备
etc 配置文件
home 用户家目录
lib -> usr/lib 库
lib64 -> usr/lib64
media 充当外围设备挂载点
mnt 充当外围设备挂载点
misc 杂项 可以光盘挂载 用的时候自动挂
net
opt 第三方软件
proc 虚拟文件夹
root 是root账号的家目录
run 放一些正在运行的相关文件
sbin -> usr/sbin 给管理员运行的可执行程序
srv 放一些服务器数据
sys 和硬件相关
tmp 临时
usr
var 可变数据

# pwd
查看当前在哪个目录
```
范例:[22:55:44 root@Centos8[ ~]#pwd
/root
```

# cd
切换目录
```
范例:[23:33:09 root@Centos8[ ~]#cd /etc
[23:33:11 root@Centos8[ etc]#

```

# dirname和basename
```
范例:
[root@centos8 ~]#dirname /etc/sysconfig/network-scripts/ifcfg-eth0/etc/sysconfig/network-scripts
[root@centos8 ~]#basename /etc/sysconfig/network-scripts/ifcfg-eth0
ifcfg-eth0
```

# 更改目录

范例:
[root@centos8 ~]#cd /etc/sysconfig
[root@centos8 sysconfig]#pwd
/etc/sysconfig
[root@centos8 sysconfig]#cd ../../data
[root@centos8 data]#pwd
/data
[root@centos8 data]#cd /bin
[root@centos8 bin]#pwd
/bin
[root@centos8 bin]#cd -P /bin
[root@centos8 bin]#pwd
/usr/bin

 

posted @ 2020-03-23 00:26  huangqiushi  阅读(173)  评论(0)    收藏  举报