1.Linux 命令行

1.1控制台

Console,也就是控制台,也称为终端,提供命令行输入数据和展示命令行输出数据。

  • 物理控制台,用于与系统交互的硬件接口。
  • 虚拟控制台, 名称为tty1--tty6,通过ctrl + alt + F1...F6切换。tty1是图形化程序所在的终端。

ctrl + alt + F3 进入3号虚拟控制台,登录后,执行tty命令,效果如下:

输入密码的时候,屏幕是不显示任何字符的。

1.2shell 介绍

shell,用于解析终端中输入的字符串,交由kernel执行,并将执行结果显示到终端中。Linux命令行由shell程序提供。在Linux发展过程中,产生了多种shell程序。Bash是类UNIX系统中最成功的shell,也是用户的默认shell。

#配置命令提示符和历史命令格式
cat >> /etc/bashrc <<'EOF'
PS1='[\[\e[91m\]\u\[\e[93m\]@\[\e[92;1m\]\h\[\e[0m\] \[\e[94m\]\W\[\e[0m\] \[\e[35m\]\t\[\e[0m\]]\[\e[93m\]\$\[\e[0m\] '
HISTTIMEFORMAT="%F %T "
EOF

1.3命令提示符

1.4作用:提示计算机正在等待用户的输入。

# 代表普通用户提示符,提示符最后是有一个空格。
[laoma@CentOS7 ~]$ date

# 代表管理员提示符
[root@CentOS7 ~]# 

1.4shell 解析字符串的语法

shell命令行,通过空格分隔,包涵三个部分:

  • 命令,第一部分必须是命令,代表要执行的程序,其后可能跟着选项或参数。
  • 选项调整命令的行为或作用。通常以一个或两个 - 符号开头,例如(-a --all)。
  • 参数,典型的参数是命令的目标,命令后面可能接多个参数。

Enter键,shell开始解析命令行中字符串,并交由kernel执行。

示例:

[laoma@CentOS7 ~]$ ls -1 /home
# ls,是命令
# -l,是选项
# /home,是参数

# 只用命令情况
[laoma@CentOS7 ~]$ ls
公共  模板  视频  图片  文档  下载  音乐  桌面

# 使用不同格式选项,-a和--all都是用于显示所有文件,包涵.开头的隐藏文件
[laoma@CentOS7 ~]$ ls -a
.              .bashrc  .esd_auth      .mozilla     模板  下载
..             .cache   .gnupg         .ssh         视频  音乐
.bash_logout   .config  .ICEauthority  .Xauthority  图片  桌面
.bash_profile  .dbus    .local         公共         文档

[laoma@CentOS7 ~]$ ls --all
.              .bashrc  .esd_auth      .mozilla     模板  下载
..             .cache   .gnupg         .ssh         视频  音乐
.bash_logout   .config  .ICEauthority  .Xauthority  图片  桌面
.bash_profile  .dbus    .local         公共         文档

# 使用参数,指定目标,查看目录/etc/yum文件清单
[laoma@CentOS7 ~]$ ls /etc/yum
fssnap.d  pluginconf.d  protected.d  vars  version-groups.conf

1.5命令行语法示例

[laoma@CentOS7 ~]$ date --help
用法:date [选项]... [+格式]
 或:date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.

Mandatory arguments to long options are mandatory for short options too.
  -d, --date=STRING         display time described by STRING, not 'now'
  -f, --file=DATEFILE       like --date once for each line of DATEFILE
... ...

[laoma@CentOS7 ~]$ date
2022年 11月 03日 星期四 13:38:59 CST

# 显示UTC时间
[laoma@CentOS7 ~]$ date -u
2022年 11月 03日 星期四 05:39:09 UTC
[laoma@CentOS7 ~]$ date --utc
2022年 11月 03日 星期四 05:40:06 UTC
[laoma@CentOS7 ~]$ date --universal
2022年 11月 03日 星期四 05:40:31 UTC

# 根据给定时间字符串显示日志
[laoma@CentOS7 ~]$ date -d '-1 day'
2022年 11月 02日 星期三 13:42:03 CST
[laoma@CentOS7 ~]$ date --date='-1 day'
2022年 11月 02日 星期三 13:43:04 CST
[laoma@CentOS7 ~]$ date --date='100 day'
2023年 02月 11日 星期六 13:44:00 CST

# 特殊案例
[laoma@CentOS7 ~]$ date -d "-1 year +3 month +10 days"
2022年 02月 13日 星期日 14:20:59 CST

# 以给定格式显示时间
[laoma@CentOS7 ~]$ date +%Y%m%d
20221103
[laoma@CentOS7 ~]$ date +%Y-%m-%d
2022-11-03
[laoma@CentOS7 ~]$ date +%Y/%m/%d
2022/11/03

一行要执行多个命令,使用分号(;)分隔。

[laoma@CentOS7 ~]$ ls
公共  模板  视频  图片  文档  下载  音乐  桌面
[laoma@CentOS7 ~]$ date
2022年 11月 03日 星期四 13:52:05 CST
[laoma@CentOS7 ~]$ ls;date
公共  模板  视频  图片  文档  下载  音乐  桌面
2022年 11月 03日 星期四 13:52:08 CST

# 错误的示例
[laoma@CentOS7 ~]$ ls date
ls: 无法访问date: 没有那个文件或目录

2.bash 执行命令

2.1 bash 执行命令

2.1.1 passwd

作用:管理用户密码。

[laoma@CentOS7 ~]$ passwd --help
用法: passwd [选项...] <帐号名称>

# 普通用户修改密码
[laoma@CentOS7 ~]$ passwd
更改用户 laoma 的密码 。
为 laoma 更改 STRESS 密码。
(当前)UNIX 密码:`123`
新的 密码:`redhat`
无效的密码: 密码少于 8 个字符
新的 密码:`12345678`
无效的密码: 密码未通过字典检查 - 过于简单化/系统化
新的 密码:`55TurnK3y`
重新输入新的 密码:`55TurnK3y`
passwd:所有的身份验证令牌已经成功更新。

# root 用户管理账户密码
[root@CentOS7 ~]# passwd laoma
更改用户 laoma 的密码 。
新的 密码:`redhat`
无效的密码: 密码少于 8 个字符
重新输入新的 密码:`redhat`
passwd:所有的身份验证令牌已经成功更新。

# 删除用户密码,实现用户登录不需要密码
[root@CentOS7 ~]# passwd -d laoma
清除用户的密码 laoma。
passwd: 操作成功

# 锁定用户密码,用户将无法登录
[root@CentOS7 ~]# passwd -l laoma
锁定用户 laoma 的密码 。
passwd: 操作成功

# 解锁用户密码,用户可以正常登录
[root@CentOS7 ~]# passwd -u laoma
解锁用户 laoma 的密码。
passwd: 操作成功
2.1.2 file

作用:判定文件类型。

[laoma@CentOS7 ~]$ file --help
Usage: file [OPTION...] [FILE...]
Determine type of FILEs.

[laoma@CentOS7 ~]$ file /etc/yum
/etc/yum: directory

[laoma@CentOS7 ~]$ file /etc/fstab
/etc/fstab: ASCII text

# /bin/file是二进制程序
[laoma@CentOS7 ~]$ file /bin/file
/bin/file: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=58f6c8b82ee70887d7136a7181ffc8fa18030cde, stripped
2.1.3 cat

作用:一次性读取文件内容

[laoma@CentOS7 ~]$ cat --help
用法:cat [选项]... [文件]...
将[文件]或标准输入组合输出到标准输出。

[laoma@CentOS7 ~]$ cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.1.8.88   CentOS7.redhat.fun  CentOS7

# -A选项等于-vET,用于显示行尾加个$,用^I显示制表符(tab键)
[laoma@CentOS7 ~]$ cat -A /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4$
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6$
10.1.8.88^ICentOS7.redhat.fun^ICentOS7$

# 一次性查看多个文件
[laoma@CentOS7 ~]$ cat /etc/hosts /etc/fstab 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.1.8.88   CentOS7.redhat.fun  CentOS7

#
# /etc/fstab
# Created by anaconda on Wed Nov  2 17:31:41 2022
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=047729d6-683f-4aec-b614-28d20ce0a90c /boot                   xfs     defaults        0 0
/dev/mapper/centos-home /home                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0

作用:显示文件头部内容。

[laoma@CentOS7 ~]$ head --help
用法:head [选项]... [文件]...
Print the first 10 lines of each FILE to standard output.
With more than one FILE, precede each with a header giving the file name.
With no FILE, or when FILE is -, read standard input.

[laoma@CentOS7 ~]$ head /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

# 只看前4行内容
[laoma@CentOS7 ~]$ head -n 4 /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# 查看文件所有内容,除了最后3行
[laoma@CentOS7 ~]$ head -n -3 /etc/profile
2.1.5 tail

作用:显示文件尾部内容。

[laoma@CentOS7 ~]$ tail --help
用法:tail [选项]... [文件]...
Print the last 10 lines of each FILE to standard output.
With more than one FILE, precede each with a header giving the file name.
With no FILE, or when FILE is -, read standard input.

[laoma@CentOS7 ~]$ tail /etc/profile
        if [ "${-#*i}" != "$-" ]; then 
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

unset i
unset -f pathmunge

[laoma@CentOS7 ~]$ tail -n 4 /etc/profile
done

unset i
unset -f pathmunge

# 从第四行到最后所有行内容
[laoma@CentOS7 ~]$ tail -n +4 /etc/profile
2.1.6 less

作用:交互式、翻页查看文件内容。

# 使用less查看多页文档,可以翻页,搜索等
[laoma@CentOS7 ~]$ less -N /etc/profile
# 在less查看文件过程中常见指令:
# / 搜索,n(next)搜索下一个,N搜索上一个。
# 上下方向键 逐行查看,左右键左右翻页
# pageup和pagedown 上下翻页
# q,退出文档查看
2.1.7 wc

作用:统计文件行数、单词数、字节数。

[laoma@CentOS7 ~]$ wc --help
用法:wc [选项]... [文件]...
 或:wc [选项]... --files0-from=F
Print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified.  With no FILE, or when FILE is -, read standard input.  A word is a non-zero-length sequence of characters delimited by white space.
The options below may be used to select which counts are printed, always in the following order: newline, word, character, byte, maximum line length.
  -c, --bytes            print the byte counts
  -m, --chars            print the character counts
  -l, --lines            print the newline counts
      --files0-from=文件  从指定文件读取以NUL 终止的名称,如果该文件被
                    指定为"-"则从标准输入读文件名
  -L, --max-line-length 显示最长行的长度
  -w, --words           显示单词计数
      --help        显示此帮助信息并退出
      --version     显示版本信息并退出

[laoma@CentOS7 ~]$ cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.1.8.88   CentOS7.redhat.fun  CentOS7
[laoma@CentOS7 ~]$ wc /etc/hosts
  3  13 197 /etc/hosts

# 统计文件line数量
[laoma@CentOS7 ~]$ wc -l /etc/hosts
3 /etc/hosts

# 统计文件word数量
[laoma@CentOS7 ~]$ wc -w /etc/hosts
13 /etc/hosts

# 统计文件bytes数量
[laoma@CentOS7 ~]$ wc -c /etc/hosts
197 /etc/hosts

# 统计多个文件数据
[laoma@CentOS7 ~]$ wc /etc/hosts /etc/fstab 
  3  13 197 /etc/hosts
 12  60 541 /etc/fstab
 15  73 738 总用量

2.2 tab 按键

补全功能:补全命令、补全选项、补全参数

# tab 一次,就出结果,说明匹配结果
# tab 两次,才补全出结果,说明匹配结果不唯一

# 补全命令
[laoma@CentOS7 ~]$ pas<tab><tab>
passwd       paste        pasuspender

[laoma@CentOS7 ~]$ pass<tab>
# 自动显示为
[laoma@CentOS7 ~]$ passwd

# 补全选项
[laoma@CentOS7 ~]$ passwd --d<tab>
# 自动显示为
[laoma@CentOS7 ~]$ passwd --delete

# 补全参数
[laoma@CentOS7 ~]$ file /e<tab>
# 自动显示为
[laoma@CentOS7 ~]$ file /etc/

2.3 history 命令

# history命令查看帮助不能使用 history --help
[laoma@centos7 ~]$ history --help
-bash: history: --: 无效选项
history: 用法:history [-c] [-d 偏移量] [n] 或 history -anrw [文件名] 或 history -ps 参数 [参数...]


[laoma@CentOS7 ~]$ help history 
history: history [-c] [-d 偏移量] [n] 或 history -anrw [文件名] 或 history -ps 参数 [参数...]
    显示或操纵历史列表。

    带行号显示历史列表,将每个被修改的条目加上前缀 `*'。
    参数 N 会仅列出最后的 N 个条目。

    选项:
      -c    删除所有条目从而清空历史列表。
      -d    偏移量 从指定位置删除历史列表。

      -a    将当前绘画的历史行追加到历史文件中
      -n    从历史文件中读取所有未被读取的行
      -r    读取历史文件并将内容追加到历史列表中
      -w    将当前历史写入到历史文件中,并追加到历史列表中

      -p    对每一个 ARG 参数展开历史并显示结果,而不存储到历史列表中
      -s    以单条记录追加 ARG 到历史列表中

    如果给定了 FILENAME 文件名,则它将被作为历史文件。否则
    如果 $HISTFILE 变量有值的话使用之,不然使用 ~/.bash_history 文件。

    如果 $HISTTIMEFORMAT 变量被设定并且不为空,它的值会被用于
    strftime(3) 的格式字符串来打印与每一个显示的历史条目想关联的时
    间戳,否则不打印时间戳。

    退出状态:
    返回成功,除非使用了无效的选项或者发生错误。

[laoma@CentOS7 ~]$ history 
    1  date
    2  date -u
    3  date --utc
    4  date --universal
    5  date -d '-1 day'
... ...

# 情况历史操作记录
[laoma@CentOS7 ~]$ history -c

# 使用上下键查找历史命令的时候,只能查找现有的历史命令中命令

[laoma@CentOS7 ~]$ history 
    1  history 
    2  echo hello world
    3  ls
    4  hostname
    5  history

# 调用历史第2条命令,显示第2条命令后,然后执行该命令
[laoma@CentOS7 ~]$ !2
echo hello world
hello world

# 从下网上匹配最后一个以ho开头的命令,显示并执行该命令
[laoma@CentOS7 ~]$ !ho
hostname
CentOS7.wanho.net

# 使用-w选将历史命令写入到文件~/.bash_history
[laoma@CentOS7 ~]$ history -w
[laoma@CentOS7 ~]$ cat ~/.bash_history 
history 
echo hello world
ls
hostname
history 
echo hello world
hostname
history 
history -w

2.4 script 命令(自学)

[laoma@centos7 ~]$ script --help

用法:
 script [选项] [文件]

选项:
 -a, --append            追加输出
 -c, --command <命令>    运行命令而不是交互式 shell
 -e, --return            返回子进程的退出代码
 -f, --flush             每次 write(写) 后运行 flush(冲刷)
     --force             即使输出文件是链接也依然使用
 -q, --quiet             安静模式
 -t, --timing[=<文件>]   将时间数据输出到标准错误(或文件)
 -V, --version           输出版本信息并退出
 -h, --help              显示此帮助并退出

# 记录终端中历史操作,并保存到指定文件,还可以同时记录操作时间
[laoma@centos7 ~]$ script --timing=time.log record.log
# 或者
[laoma@centos7 ~]$ script -ttime.log record.log

[laoma@centos7 ~]$ echo hello world
hello world
[laoma@centos7 ~]$ hostname
centos79.linux.com
[laoma@centos7 ~]$ date
2022年 12月 13日 星期二 17:10:13 CST
[laoma@centos7 ~]$ history -c
[laoma@centos7 ~]$ echo goodbye
goodbye
[laoma@centos7 ~]$ exit
exit
Script done, file is record.log

# 查看日志内容
[laoma@centos7 ~]$ cat record.log 
脚本启动于 2022年12月13日 星期二 17时10分02秒
[laoma@centos7 ~]$ echo hello world
hello world
[laoma@centos7 ~]$ hostname
centos79.linux.com
[laoma@centos7 ~]$ date
2022年 12月 13日 星期二 17:10:13 CST
[laoma@centos7 ~]$ history -c
[laoma@centos7 ~]$ echo goodbye
goodbye
[laoma@centos7 ~]$ exit
exit

Script done on 2022年12月13日 星期二 17时10分27秒

# 动态回放日志
[laoma@centos7 ~]$ scriptreplay -t time.log record.log

2.5 bash 快捷键

快捷键 作用
Ctrl+a 或者 Home 光标定位到命令行开头
Ctrl+e 或者 End 光标定位到命令行末尾
Ctrl+u 删除光标位置到命令行开头
Ctrl+k 删除光标位置到命令行末尾
Ctrl+<- 光标向左跳转一个单词
Ctrl+-> 光标向右跳转一个单词
Ctrl+r 搜索历史命令
Ctrl+w 向左删除一个参数
Esc(松开)+. 打印上一个命令的最后一个参数
Ctrl+l(L的小写) 清空屏幕
Ctrl+d 登出终端
Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐