第二章Linux基础入门和帮助
命令格式:
command [option]... [argument]...
command [options] [arguments]
具体说明:
1、command: 表示命令的名称,如 ls
2、option:定义命令的执行特性,可以有长短两种选项:
- 长选项:用 -- 引导,后面跟完整的单词,如 --help
- 短选项:用 - 引导,后面跟单个的字符, 如 –a
3、argument:命令作用参数;例如 文件夹 ls /etc 后面的/etc就是 ls的参数对象。
注意:
- 多个命令放在一行,可以使用”;”隔开
- 一个命令可以用“\”分开
[root@localhost ~]# free -m ; df -h total used free shared buff/cache available Mem: 7803 176 7436 11 189 7369 Swap: 2047 0 2047 Filesystem Size Used Avail Use% Mounted on /dev/sda2 50G 1.1G 49G 3% / devtmpfs 3.8G 0 3.8G 0% /dev tmpfs 3.9G 0 3.9G 0% /dev/shm tmpfs 3.9G 12M 3.8G 1% /run tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup /dev/sda3 10G 33M 10G 1% /home /dev/sda6 37G 33M 37G 1% /data /dev/sda1 1014M 137M 878M 14% /boot tmpfs 781M 0 781M 0% /run/user/0
[root@localhost ~]# fr\ > ee total used free shared buff/cache available Mem: 7990280 180560 7615328 11972 194392 7546920
虚拟机的终端类型:
控制台终端:/dev/console 串行终端:/dev/tty# 伪终端:/dev/pts# (例如SSH连接)
查看当前的终端设备:
[root@localhost ~]# tty /dev/pts/0 [root@localhost ~]# who //查看用户登录 root tty1 2021-01-15 21:47 root pts/0 2021-01-17 08:48 (192.168.31.216) iverson pts/1 2021-01-17 10:14 (192.168.31.216)
查看当前shell版本
[root@localhost ~]# echo $SHELL
系统其他SHELL
[root@localhost ~]# cat /etc/shells /bin/sh /bin/bash /usr/bin/sh /usr/bin/bash
shell中可执行的命令
1.内部命令:shell自带,运行在内存中
2.外部命令:在文件系统路径下可执行的程序文件
3.whatis 命令(适用于 7 和 8)
[root@localhost ~]# whatis rm rm: nothing appropriate. //无法使用?? [root@localhost ~]# mandb //生成man相关数据库、重新执行 [root@localhost ~]# whatis rm rm (1) - remove files or directories
区别内部命令查看:
type 命令
which
内部:builtin 外部命令存放路径 echo $PATH 外部命令执行过一次后,会存在hash(缓存),提高系统命令执行效率。hash命令显示了执行的次数及外部命令的路径。
[root@localhost ~]# hash hits command 3 /usr/bin/tty 1 /usr/bin/lscpu 1 /usr/bin/df 2 /usr/bin/lsblk 2 /usr/bin/cat 3 /usr/bin/ls 1 /usr/sbin/ip
[root@localhost ~]# lsblk //lsblk显示所有块设备 NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 100G 0 disk ├─sda1 8:1 0 1G 0 part /boot ├─sda2 8:2 0 50G 0 part / ├─sda3 8:3 0 10G 0 part /home ├─sda4 8:4 0 1K 0 part ├─sda5 8:5 0 2G 0 part [SWAP] └─sda6 8:6 0 37G 0 part /data sr0 11:0 1 10G 0 rom /dev/sr0是光驱的设备名,块设备不需要挂载
TYPE= part 表示标准分区
查看CPU
[root@localhost ~]# lscpu Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 4 //cpu数量 On-line CPU(s) list: 0-3 //在线的cpu数量 有些时候为了省电或者过热的时候,某些CPU会停止运行 Thread(s) per core: 1 //每个核心的线程数 Core(s) per socket: 2 //每个插槽上有几个核心 Socket(s): 2 //cpu插槽数 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 //CPU系列 Model: 166 Model name: Intel(R) Core(TM) i7-10710U CPU @ 1.10GHz Stepping: 0 CPU MHz: 1607.999 BogoMIPS: 3215.99 Hypervisor vendor: VMware Virtualization type: full L1d cache: 32K L1i cache: 32K L2 cache: 256K L3 cache: 12288K NUMA node0 CPU(s): 0-3 Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch ssbd ibrs ibpb stibp ibrs_enhanced fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap clflushopt xsaveopt xsavec xgetbv1 arat spec_ctrl intel_stibp flush_l1d arch_capabilities
CPU类型到核:
[root@localhost ~]# cat /proc/cpuinfo | more processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 166 model name : Intel(R) Core(TM) i7-10710U CPU @ 1.10GHz stepping : 0 microcode : 0xb4 cpu MHz : 1607.999 cache size : 12288 KB physical id : 0 siblings : 2 core id : 0 cpu cores : 2 apicid : 0 initial apicid : 0 fpu : yes fpu_exception : yes cpuid level : 22 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht sysc all nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx 16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch ss bd ibrs ibpb stibp ibrs_enhanced fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap clflushopt xsaveopt xsavec xg etbv1 arat spec_ctrl intel_stibp flush_l1d arch_capabilities bogomips : 3215.99 clflush size : 64 cache_alignment : 64 address sizes : 45 bits physical, 48 bits virtual power management:
。。。。。。。。。。
以下省略
系统关机
shutdown [-t seconds] [-rkhncfF] time [message]
- -t seconds : 设定在几秒钟之后进行关机程序。
- -r : 关机后重新开机。
- time : 设定关机的时间
- message : 传送给所有使用者的警告讯息。
- -c : 取消目前已经进行中的关机动作。
[root@localhost ~]# shutdown -h 11:06 guanji Shutdown scheduled for Sat 2020-11-14 11:06:00 EST, use 'shutdown -c' to cancel. [root@localhost ~]# Broadcast message from root@localhost.localdomain (Sat 2020-11-14 11:05:15 EST): guanji The system is going down for power-off at Sat 2020-11-14 11:06:00 EST! 系统将在美国东部时间2020-11-14 11:06:00关闭电源! [root@localhost ~]# shutdown -c Broadcast message from root@localhost.localdomain (Sat 2020-11-14 11:05:27 EST): The system shutdown has been cancelled at Sat 2020-11-14 11:06:27 EST! 系统关闭已于美国东部时间2020-11-14 11:06:27取消!:
uname 系统版本
[root@localhost ~]# uname -a // -all 查看全部信息 Linux localhost.localdomain 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux [root@localhost ~]# uname -r //-release 查看发行版本 3.10.0-957.el7.x86_64
[root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core) [root@localhost ~]# cat /etc/os-release NAME="CentOS Linux" VERSION="7 (Core)" ID="centos" ID_LIKE="rhel fedora" VERSION_ID="7" PRETTY_NAME="CentOS Linux 7 (Core)" ANSI_COLOR="0;31" CPE_NAME="cpe:/o:centos:centos:7" HOME_URL="https://www.centos.org/" BUG_REPORT_URL="https://bugs.centos.org/" CENTOS_MANTISBT_PROJECT="CentOS-7" CENTOS_MANTISBT_PROJECT_VERSION="7" REDHAT_SUPPORT_PRODUCT="centos" REDHAT_SUPPORT_PRODUCT_VERSION="7"
echo【选项】 【字符串】
- -n 不自动换行
- -e 启用 \ 字符的解释功能
- -E 不启用 \ 字符的解释功能
[root@localhost ~]# echo -e "$PATH\n$SHELL" \\ -n 换行输出,加-e选项 /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin /bin/bash [root@localhost ~]# echo -E "$PATH\n$SHELL" /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin\n/bin/bash
符号学习
` ` $ ( ) { }
[root@localhost ~]# echo `echo $SHELL` /bin/bash 反引号的作用就是将反引号内的Linux命令先执行,然后将执行结果赋予变量
[root@localhost ~]# mkdir `date +%F`
[root@localhost ~]# mkdir $(date +%T)
[root@localhost ~]# ls
09:41:23 2020-11-15
反引号等价于$( )
[root@localhost ~]# echo {0..10}
0 1 2 3 4 5 6 7 8 9 10
[root@localhost ~]# 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@localhost ~]# 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 [ ] ^ _ ` 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 //取决于SCII码排序问题
[root@localhost ~]# echo {0000..20}
0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015 0016 0017 0018 0019 0020
[root@localhost ~]# echo {0000..20..2} //以000格式,在数字20之内以2基数跳转。
0000 0002 0004 0006 0008 0010 0012 0014 0016 0018 0020
[root@localhost ~]# echo {A..Z..2}
A C E G I K M O Q S U W Y
{ }实现打印重复字符串得简化形式
查看历史命令
history
[root@localhost ~]# ls -al total 12 dr-xr-x---. 4 root root 97 Nov 16 12:09 . dr-xr-xr-x. 18 root root 236 Nov 12 19:30 .. drwxr-xr-x. 2 root root 6 Nov 15 09:41 09:41:23 drwxr-xr-x. 2 root root 6 Nov 15 09:41 2020-11-15 -rw-------. 1 root root 661 Nov 16 12:09 .bash_history //带 . 的文件是隐藏文件。 -rw-r--r--. 1 root root 193 Nov 16 12:09 .bash_profile -rw-r--r--. 1 root root 231 Nov 16 12:09 .bashrc [root@localhost ~]# cat .bash_history //存放历史记录文件,只有正常退出,内存中的命令才会保存
命令历史相关环境变量
持久保存变量路径:export变量名=”值”形式存放在 /etc/profile 或者家目录的.bash_profile。路径不同,生效不同,在/etc 的是系统配置文件,全局生效。编辑文件完成后source生效。
[root@localhost ~]# cat .bash_profile # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/.local/bin:$HOME/bin export PATH export HISTTIMEFORMAT="%F %T" //写进去的变量,显示时间。 [root@localhost ~]# history 1 2020-11-16 12:09:44date 2 2020-11-16 12:09:44echo $hostname 3 2020-11-16 12:09:44mkdir $(date +%F) 4 2020-11-16 12:09:44ls 5 2020-11-16 12:09:44rm -rf 2020* 6 2020-11-16 12:09:44ls
man命令:
man命令的章节:
查看某文件或者命令章节:
[root@localhost ~]# whatis passwd //查看章节 openssl-passwd (1ssl) - compute password hashes passwd (1) - update user's authentication tokens [root@localhost ~]# man 1 passwd
data命令:
- -s <字符串>:根据字符串来设置日期与时间。字符串前后必须加上双引号;
[root@localhost ~]# date -s "2019-08-07 06:05:10" Wed Aug 7 06:05:10 EDT 2019 [root@localhost ~]# date Wed Aug 7 06:05:13 EDT 2019
- -d 显示字符串所指的日期与时间。字符串前后必须加上双引号;
[root@localhost ~]# date Sun Jan 17 11:11:37 EST 2021 [root@localhost ~]# date -d "1 day" +"%Y-%m-%d" 2021-01-18 //显示明天日期 [root@localhost ~]# date -d "-1 day" +"%Y-%m-%d" 2021-01-16 //显示昨天日期
clock 显示硬件时间
- -s 以硬件时间为准。校准系统时间
- -w 以系统时间为准。校准硬件时间
2020-11-15 0:27 (岁月不言|时间不语)