命令行基础

命令行基础

在Linux中执行大多数命令时,都要找到命令所对应的程序

  1. [root@localhost /]# hostname
  2. [root@localhost /]# which hostname #查询命令对应的程序
  3. [root@localhost /]# ls /usr/bin/hostname
  4. [root@localhost /]# which ifconfig #查询命令对应的程序
  5. [root@localhost /]# firefox
  6. [root@localhost /]# which firefox #查询命令对应的程序

命令的执行依赖于解释器(默认解释器/bin/bash)

用户--->解释器(shell)--->内核--->硬件

绿色:可以执行的程序

命令行完整格式:命令 [选项]… [参数1] [参数2]…

打 武器 奥特曼 ……

  1. [root@localhost /]# cat --help #查看命令帮助信息
  2. [root@localhost /]# cat -n /etc/shells #显示行号
  3. [root@localhost /]# cat -n /etc/passwd
  4. [root@localhost /]# cat -n /etc/redhat-release
  5. [root@localhost /]# cat -n /etc/fstab
  6. [root@localhost /]# cat -n /etc/shadow
  7. [root@localhost /]# cat -n /etc/hosts
  8. [root@localhost /]# cat -n /etc/group

命令行完整格式:命令 [选项]… [参数1] [参数2]…

  1. [root@localhost /]# ls -l /etc/passwd #长格式显示
  2. [root@localhost /]# ls -l /etc/fstab #显示详细属性
  3. [root@localhost /]# ls -l /etc/shells
  4. [root@localhost /]# ls -l /etc/shadow #长格式显示
  5. [root@localhost /]# ls -l /etc/hosts #长格式显示
  6. [root@localhost /]# ls -l /etc/group #长格式显示
  7. [root@localhost /]# ls -l /etc/gshadow #长格式显示
  8. [root@localhost /]# ls -l /root #默认显示目录内容的详细属性

Tab键自动补全

可补齐命令字、选项、参数、文件路径、软件名、服务名

  1. [root@localhost /]# if(tab) (tab) #列出以if开头的命令
  2. [root@localhost /]# ifco(tab)
  3. [root@localhost /]# cat /etc/re(tab) (tab)
  4. [root@localhost /]# cat /etc/red(tab)
  5. [root@localhost /]# ls /etc/sysconfig/network-scripts/
  6. [root@localhost /]# ls /et(tab)/sysco(tab)/netw(tab)- (tab)

快捷键

  1. Ctrl + c:结束正在运行的命令
  2. Esc + .或Alt + .:粘贴上一个命令的参数
  3. [root@localhost ~]# ls /etc/hosts
  4. [root@localhost ~]# ls -l Alt + .
  5. [root@localhost ~]# cat Alt + .
  6. [root@localhost ~]# cat -n Alt + .
  7. Ctrl + l:清空整个屏幕
  8. Ctrl + u:从光标处清空至行首
  9. Ctrl + k:从光标处清空至行尾
  10. Home键:从光标处快速到行首
  11. end键:从光标处快速到行尾
  12. Ctrl + w:往回删除一个单词(以空格界定)

mount挂载操作

/dev/cdrom ---> /dev/sr0

  1. [root@localhost /]# ls -l /dev/cdrom
  2. lrwxrwxrwx. 1 root root 3 3月 8 09:09 /dev/cdrom -> sr0
  3. [root@localhost /]# ls /dev/cdrom
  4. /dev/cdrom
  5. [root@localhost /]# ls /dev/sr0
  6. /dev/sr0
  7. [root@localhost /]#

Linux显示光盘内容:

光盘---->光驱设备---->访问点(目录)

mount挂载操作:让目录成为设备的访问点

  1. [root@localhost /]# mkdir /dvd
  2. [root@localhost /]# ls /dvd #显示内容为空
  3. [root@localhost /]# mount /dev/cdrom /dvd
  4. mount: /dvd: WARNING: device write-protected, mounted read-only.
  5. [root@localhost /]# ls /dvd/
  6. [root@localhost /]# ls /dvd/A(Tab键)/P(Tab键)
  7. [root@localhost /]# ls /dvd/B(Tab键)/P(Tab键)

卸载操作

 
  1. [root@localhost /]# umount /dvd
  2. [root@localhost /]# ls /dvd/ #显示内容为空
  3. [root@localhost /]# mkdir /mydvd
  4. [root@localhost /]# mount /dev/cdrom /mydvd
  5. mount: /mydvd: WARNING: device write-protected, mounted read-only.
  6. [root@localhost /]# ls /mydvd
  7. [root@localhost /]# mount #查看当前系统所有挂载设备

注意事项:

1.卸载:当前所在的路径是挂载点目录

[root@localhost mydvd]# umount /mydvd

umount: /mydvd:目标忙。

(有些情况下通过 lsof(8) 或 fuser(1) 可以

找到有关使用该设备的进程的有用信息)

2.允许:一个设备,挂载到不同的挂载点目录

3.不允许:一个挂载点目录,挂载多个设备

4.建议挂载点目录,最好自行创建

目录与文件

cd的命令使用

. 当前目录

.. 父目录(上一层)

~ 表示用户的家目录

  1. 家目录:专门存放用户个性化信息的目录
  2. ~user:用户user的家目录
  3. #cold_bold/root:Linux管理员的家目录
  4. #cold_bold/home:存放所有普通用户的家目录
  5. [root@localhost /]# cd ~root #去往root用户的家目录
  6. [root@localhost /]# pwd
  7. [root@localhost /]# useradd dc #创建用户dc
  8. [root@localhost /]# cd ~dc #去往dc用户的家目录
  9. [root@localhost /]# pwd
  10. [root@localhost /]# useradd tom #创建用户tom
  11. [root@localhost /]# cd ~tom #去往tom用户的家目录
  12. [root@localhost /]# pwd

ls — List

格式:ls [选项]... [目录或文件名]

常用命令选项:

-l:以长格式显示,显示详细信息

-h:提供易读的容量单位(K、M等)

-d:显示目录本身(而不是内容)的属性

  1. [root@localhost /]# ls -ld /home
  2. [root@localhost /]# ls -ld /root
  3. [root@localhost /]# ls -ld /
  4. [root@localhost /]# ls -l /etc/passwd
  5. [root@localhost /]# ls -lh /etc/passwd

-A:显示所有内容,包含隐藏数据

-R:递归显示目录内容

  1. [root@localhost /]# ls -A /root
  2. [root@localhost /]# touch /opt/.nsd.txt
  3. [root@localhost /]# ls /opt/
  4. [root@localhost /]# ls -A /opt/
  5. [root@localhost /]# ls -R /boot

通配符使用,针对不确定的文档名称,以特殊字符表示

*:任意多个任意字符

  1. [root@localhost /]# ls /root/a*
  2. [root@localhost /]# ls /boot/vm*
  3. [root@localhost /]# ls /etc/*tab
  4. [root@localhost /]# ls /etc/*.conf
  5. [root@localhost /]# ls /etc/re*.conf
  6. [root@localhost /]# ls /dev/tty*
  7. [root@localhost /]# ls -d /etc/r*
  8. [root@localhost /]# ls -d /etc/*as*

?:单个字符(必须要有一个)

  1. [root@localhost /]# ls /etc/??tab
  2. [root@localhost /]# ls /dev/tty?
  3. [root@localhost /]# ls /dev/tty??

[a-z]:多个字符或连续范围中的一个,若无则忽略

{a,min,xy}:多组不同的字符串,全匹配

  1. [root@localhost /]# ls /dev/tty[3-9]
  2. [root@localhost /]# ls /dev/tty[1-7]
  3. [root@localhost /]# ls /dev/tty{1,17,20}
  4. [root@localhost /]# ls /dev/tty{10,18,22,33}
  5. [root@localhost /]# ls /dev/tty{26,19,21,30,40}
  6. [root@localhost /]# ls /etc/{rw,fs}tab

别名的定义

别名的定义:简化复杂的命令

查看已设置的别名

alias [别名名称]

定义新的别名

alias 别名名称= '实际执行的命令行'

取消已设置的别名

unalias [别名名称]

  1. [root@localhost /]# hostname
  2. [root@localhost /]# alias hn='hostname' #临时定义别名
  3. [root@localhost /]# hn
  4. [root@localhost /]# alias #查看系统中有哪些别名
  5. [root@localhost /]# unalias hn #删除hn别名
  6. [root@localhost /]# hn
  7. bash: hn: 未找到命令...
  8. [root@localhost /]# alias myls='ls -l'
  9. [root@localhost /]# myls /etc/passwd

目录与文件管理

mkdir — Make Directory

格式:mkdir [/路径/]目录名…

[-p]:连同父目录一并创建

  1. [root@localhost /]# mkdir -p /opt/aa/bb/cc/dd
  2. [root@localhost /]# ls -R /opt/aa #递归显示目录内容
  3. [root@localhost /]# mkdir -p /nsd04/test04
  4. [root@localhost /]# ls -R /nsd04
  5. [root@localhost /]# mkdir -p /cbd/abc01
  6. [root@localhost /]# ls -R /cbd
  7. [root@localhost /]# mkdir -p /xtt/stu01
  8. [root@localhost /]# ls -R /xtt

rm 删除 Remove

格式:rm [选项]... 文件或目录…

常用命令选项

-r、-f:递归删除(含目录)、强制删除

  1. [root@localhost /]# ls /opt
  2. [root@localhost /]# rm -rf /opt/1.txt
  3. [root@localhost /]# ls /opt
  4. [root@localhost /]# rm -rf /opt/aa
  5. [root@localhost /]# ls /opt

mv — Move移动:源数据会消失

格式:mv 原文件… 目标路径

  1. [root@localhost /]# rm -rf /opt/*
  2. [root@localhost /]# mkdir /opt/nsd01
  3. [root@localhost /]# touch /opt/1.txt
  4. [root@localhost /]# ls /opt/
  5. [root@localhost /]# mv /opt/1.txt /opt/nsd01
  6. [root@localhost /]# ls /opt/
  7. [root@localhost /]# ls /opt/nsd01

重命名:路径不变的移动

  1. [root@localhost /]# ls /opt/
  2. [root@localhost /]# mv /opt/nsd01 /opt/abc
  3. [root@localhost /]# ls /opt/
  4. [root@localhost /]# mv /opt/abc /opt/student
  5. [root@localhost /]# ls /opt/
  6. [root@localhost /]# mv /opt/student /mnt/stu01
  7. [root@localhost /]# ls /mnt/

cp — Copy:源数据不会消失

格式:cp [选项]... 原文件… 目标路径

常用命令选项

-r:递归,复制目录时必须有此选项

 
  1. [root@localhost /]# cp /etc/passwd /opt
  2. [root@localhost /]# ls /opt
  3. [root@localhost /]# cp -r /boot /opt
  4. [root@localhost /]# ls /opt
  5. [root@localhost /]# cp -r /home /opt
  6. [root@localhost /]# ls /opt

进行强制覆盖

 
  1. [root@localhost /]# \cp -r /boot /opt #本次操作临时取消别名
  2. [root@localhost /]# \cp -r /boot /opt #本次操作临时取消别名

复制可以重新命名,目标路径下数据的名称

 
  1. [root@localhost /]# cp -r /home /opt/myhome
  2. [root@localhost /]# ls /opt
  3. [root@localhost /]# cp /etc/redhat-release /opt/r.txt
  4. [root@localhost /]# ls /opt
  5. [root@localhost /]# ls /opt
  6. [root@localhost /]# cp -r /root /opt/myroot
  7. [root@localhost /]# ls /opt
  8. [root@localhost /]# cp -r /root /opt/myroot
  9. [root@localhost /]# ls /opt/myroot

复制可以支持两个以上的参数,永远把最后一个参数作为目标,其他的所有的参数都作为源数据

 
  1. [root@localhost /]# mkdir /nsd05
  2. [root@localhost /]# ls /nsd05
  3. [root@localhost /]# cp -r /home /boot /etc/passwd /etc/shells /nsd05
  4. [root@localhost /]# ls /nsd05

复制与一个点进行连用,将数据复制到当前路径下

 
  1. [root@localhost /]# cd /etc/sysconfig/network-scripts/
  2. [root@localhost /]# pwd
  3. [root@localhost /]# cp /etc/passwd .
  4. [root@localhost /]# ls
  5. [root@localhost /]# cd /mnt
  6. [root@localhost /]# pwd
  7. [root@localhost /]# cp /etc/shells .
  8. [root@localhost /]# ls
  9. [root@localhost /]# cp /etc/shells ./myshell
  10. [root@localhost /]# ls

grep命令的高级使用

格式:grep [选项] '字符串' 文本文件

基本使用

 
  1. [root@svr1 /]# grep root /etc/passwd
  2. [root@svr1 /]# grep bash /etc/passwd
  3. [root@svr1 /]# grep lisi /etc/passwd
  4. [root@svr1 /]# grep zhangsan /etc/passwd

常用命令选项

-v,取反匹配(不包含)

-i,忽略大小写

 
  1. [root@localhost /]# grep root /etc/passwd
  2. [root@localhost /]# grep -v root /etc/passwd
  3. [root@localhost /]# grep ROOT /etc/passwd
  4. [root@localhost /]# grep -i ROOT /etc/passwd

高级使用

^word 以字符串word开头

word$ 以字符串word结尾

 
  1. [root@localhost /]# grep ^root /etc/passwd
  2. [root@localhost /]# grep ^bin /etc/passwd
  3. [root@localhost /]# grep root$ /etc/passwd
  4. [root@localhost /]# grep bash$ /etc/passwd

^$ 表示空行

  1. [root@localhost /]# cat /etc/default/useradd
  2. [root@localhost /]# grep ^$ /etc/default/useradd #过滤空行
  3. [root@localhost /]# grep -v ^$ /etc/default/useradd#过滤不要空行的内容
  4. [root@localhost /]# cat /etc/login.defs
  5. [root@localhost /]# grep ^$ /etc/login.defs #过滤空行
  6. [root@localhost /]# grep -v ^$ /etc/login.defs #过滤不要空行的内容

删除交换文件,swap文件效果如图-4所示。

 
  1. [root@localhost ~]# vim /opt/1.txt
  1. [root@localhost ~]# rm -rf /opt/.1.txt.swp
  2. [root@localhost ~]# vim /opt/1.txt

命令行基础技巧

 

9.1 问题

本例要求掌握Linux命令行环境的基本操作,完成下列任务:

  1. 利用Tab键快速找出下列文件:/etc/sysconfig/network-scripts/ifcfg-*、/etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
  2. 练习以下快捷编辑操作:Ctrl + l、Ctrl + u、Ctrl + w;Ctrl + c、Esc + .

9.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:利用Tab键快速补全文档路径

1)找出现有的网络连接配置文件

 
  1. [root@server0 ~]# ls /etc/sysco<TAB>
  2. [root@server0 ~]# ls /etc/sysconfig/netw<TAB>
  3. [root@server0 ~]# ls /etc/sysconfig/network-s<TAB>
  4. [root@server0 ~]# ls /etc/sysconfig/network-scripts/ifc<TAB>
  5. [root@server0 ~]# ls /etc/sysconfig/network-scripts/ifcfg-<TAB><TAB>
  6. ifcfg-br0 ifcfg-br1 ifcfg-lo
  7. ifcfg-br0:253 ifcfg-eno16777736

2)找出Linux校验软件包的密钥文件

 
  1. [root@server0 ~]# ls /etc/pki/rp<TAB>
  2. [root@server0 ~]# ls /etc/pki/rpm-gpg/RP<TAB>
  3. [root@server0 ~]# ls /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-r<TAB>
  4. [root@server0 ~]# ls /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
  5. /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

步骤二:练习以下快捷编辑操作

1)清理编辑的命令行

快速清屏:Ctrl + l

从当前光标处删除到行首:Ctrl + u

从当前光标处往前删除一个单词:Ctrl + w

2)放弃编辑的命令行

中止当前命令行:Ctrl + c

3)参数复用

在当前光标处粘贴上一条命令行的最后一个参数:Esc + .

案例2:挂载并访问光盘设备

10.1 问题

本例要求学会mount挂载操作。主要完成下列任务:

  1. 连接光盘镜像文件
  2. 将光盘挂载到 /mnt 目录,检查 /mnt 目录内容
  3. 卸载光盘设备,再次检查目录内容

10.2 步骤

实现此案例需要按照如下步骤进行。

步骤一:使用ls命令列出指定的文件

1)连接光盘镜像文件

 
  1. [root@server0 ~]# mount /dev/cdrom /mnt     //挂载设备
  2. mount: /dev/sr0 写保护,将以只读方式挂载

2)将光盘挂载到 /mnt 目录,检查 /mnt 目录内容

 
  1. [root@server0 ~]# ls /mnt                 //访问设备内容
  2. addons images Packages RPM-GPG-KEY-redhat-release
  3. EFI isolinux release-notes TRANS.TBL
  4. EULA LiveOS repodata
  5. GPL media.repo RPM-GPG-KEY-redhat-beta

3)卸载光盘设备,再次检查目录内容

 
  1. [root@server0 ~]# umount /mnt/        //卸载设备
  2. [root@server0 ~]# ls /mnt/         //确认结果
  3. [root@server0 ~]#

案例:ls列表及文档创建

11.1 问题

本例要求学会列表查看目录内容、新建文档相关技能,并熟悉通配符机制的应用。主要完成下列任务:

  1. 使用ls命令列出指定的文件:/etc/目录下以re开头.conf结尾的文件、/dev/目录下编号是个位数的tty控制台设备
  2. 一条命令创建文件夹 /protected/project/tts10

11.2 方案

对于通配符使用,需理解每个通配符的作用:

  • *:任意多个任意字符
  • ?:单个字符
  • [a-z]:多个字符或连续范围中的一个,若无则忽略
  • {a,min,xy}:多组不同的字符串,全匹配

11.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:使用ls命令列出指定的文件

1)列出/etc/目录下以re开头.conf结尾的文件

使用通配符 * 代替未知的字符串。

  1. [root@server0 ~]# ls /etc/re*.conf
  2. /etc/request-key.conf /etc/resolv.conf

2)列出/dev/目录下编号是个位数的tty控制台设备

使用通配符 ? 代替单个未知的字符。

  1. [root@server0 ~]# ls /dev/tty?
  2. /dev/tty0 /dev/tty2 /dev/tty4 /dev/tty6 /dev/tty8
  3. /dev/tty1 /dev/tty3 /dev/tty5 /dev/tty7 /dev/tty9

或者更严谨一些,使用 [0-9] 代替单个数字。

 
  1. [root@server0 ~]# ls /dev/tty[0-9]
  2. /dev/tty0 /dev/tty2 /dev/tty4 /dev/tty6 /dev/tty8
  3. /dev/tty1 /dev/tty3 /dev/tty5 /dev/tty7 /dev/tty9

步骤二:新建文档

1)使用mkdir新建文件夹

 
  1. [root@server0 ~]# mkdir -p /protected/project/tts10
  2. [root@server0 ~]# ls -ld /protected/project/tts10/
  3. drwxr-xr-x. 2 root root 6 Aug 30 10:11 /protected/project/tts10/

案例:复制、删除、移动

12.1 问题

本例要求学会对文档进行复制、删除、移动/改名相关操作,依次完成下列任务:

  • 在当前目录下创建一个子目录dir1
  • 将文件夹/boot/grub2/复制到目录dir1下
  • 将目录/root/下以 .cfg结尾的文件复制到dir1下
  • 将文件/etc/redhat-release复制到/root/下,同时改名为version.txt
  • 将文件/root/version.txt移动到dir1目录下
  • 删除dir1目录下的grub2子目录

12.2 步骤

实现此案例需要按照如下步骤进行。

1)在当前目录下创建一个子目录 dir1

 
  1. [root@server0 ~]# mkdir dir1

2)将文件夹 /boot/grub2/ 复制到目录dir1下

  1. [root@server0 ~]# cp -r /boot/grub2/ dir1/
  2. [root@server0 ~]# ls dir1                                 //检查复制结果
  3. grub2

3)将目录 /root/ 下以 .cfg 结尾的文件复制到dir1下

  1. [root@server0 ~]# cp /root/*.cfg dir1/
  2. [root@server0 ~]# ls dir1                                 //检查复制结果
  3. anaconda-ks.cfg grub2

4)将文件 /etc/redhat-release复制到 /root/ 下,同时改名为 version.txt

  1. [root@server0 ~]# cp /etc/redhat-release /root/version.txt
  2. [root@server0 ~]# ls -l /root/version.txt                 //检查复制结果
  3. -rw-r--r--. 1 root root 52 Aug 30 10:30 /root/version.txt

5)将文件 /root/version.txt 移动到dir1目录下

  1. [root@server0 ~]# cp /root/version.txt dir1/
  2. [root@server0 ~]# ls dir1                             //检查移动/改名结果
  3. anaconda-ks.cfg grub2 version.txt

6)删除 dir1 目录下的grub2子目录

 
  1. [root@server0 ~]# rm -rf dir1/grub2/
  2. [root@server0 ~]# ls dir1                        //检查删除结果
  3. anaconda-ks.cfg version.txt

案例:文本内容操作

13.1 问题

本例要求学会对文档进行复制、删除、移动/改名相关操作,依次完成下列任务:

  • 在根目录下创建一个子目录 tedu
  • 利用vim建立文件/tedu/stu.txt并写入内容“I Love Goddess”
  • 利用grep过滤/etc/passwd中包含root的行
  • 利用grep过滤/etc/passwd中以bash结尾的行

13.2 方案

  • vim是Linux系统上最常用的命令行交互式文本编辑器,主要工作在三种模式:命令模式、输入模式、末行模式。
  • 通过vim打开一个文件时,默认处于命令模式;从命令模式按i键可以进入编辑状态,按Esc键返回命令模式;从命令模式输入冒号:可以进入末行模式,在末行模式下主要执行wq存盘与退出等基本操作。

13.3 步骤

实现此案例需要按照如下步骤进行。

1)在根目录下创建一个子目录 tedu

  1. [root@server0 ~]# mkdir /tedu

2)利用vim建立文件/tedu/stu.txt并写入内容“I Love Goddess”

 
  1. [root@server0 ~]# vim /tedu/stu.txt
  2. I Love Goddess

3)利用grep过滤/etc/passwd中包含root的行

  1. [root@server0 ~]# grep root /etc/passwd

4)利用grep过滤/etc/passwd中以bash结尾的行

 
  1. [root@server0 ~]# grep bash$ /etc/passwd
posted @ 2023-10-11 08:27  kihdd  阅读(39)  评论(0)    收藏  举报