一、环境准备
开启CentOS7虚拟机
二、命令基础
•Linux命令
–用来实现某一类功能的指令或程序
Linux执行大多数命令,都要找到对应的执行程序
绿色:可以执行的程序
[root@localhost ~]# which ifconfig #查询命令对应的执行程序
/usr/sbin/ifconfig
[root@localhost ~]#
–命令的执行依赖于解释器(默认的解释器:/bin/bash)
用户—》解释器—》内核—》硬件
[root@localhost ~]# cat /etc/shells #查看当前系统有哪些解释器
•命令行的一般格式基本用法
–命令字 [选项]… [参数1] [参数2]…
[root@localhost ~]# cat /etc/shells
[root@localhost ~]# cat --help
[root@localhost ~]# cat -n /etc/shells #显示行号
[root@localhost ~]# cat -n /etc/passwd
[root@localhost ~]# cat -n /etc/fstab
[root@localhost ~]# cat -n /etc/hosts
[root@localhost ~]# ls -l /etc/shells #长格式显示(显示详细属性)
[root@localhost ~]# ls -l /root/ #显示目录内容的详细属性
•Tab键自动补全
–可补齐命令字、选项、参数、文件路径、软件名、服务名
[root@localhost ~]# if(Tab)(Tab) #列出if开头的所有命令
if ifconfig ifenslave ifup
ifcfg ifdown ifstat
[root@localhost ~]# ifco(Tab)
[root@localhost ~]# cat /et(Tab) /red(Tab)
[root@localhost ~]# ls /etc/sysconfig/network-scripts/
[root@localhost ~]# ls /et(Tab)/sysco(Tab) /netw(Tab) -(Tab)
•快捷键
–Ctrl + c:废弃当前编辑的命令行(结束正在运行的命令)
–Esc + . 或 Alt + .:粘贴上一个命令的参数
–Ctrl + l:清空整个屏幕
[root@localhost ~]# ls /etc/shells
[root@localhost ~]# ls -l Alt + .
[root@localhost ~]# cat Alt + .
[root@localhost ~]# cat -n Alt + .
–Ctrl + u:光标处清空至行首
–Ctrl + w:往回删除一个单词(以空格界定)
三、mount挂载操作:让目录成为设备的访问点(挂载点)
光盘镜像文件有众多的软件包
Windows:显示光盘内容
光盘-----》光驱设备-----》DVD图标(访问点)
Linux:显示光盘内容
光盘镜像文件—》虚拟的光驱设备----》建立一个目录(访问点)
1.将光盘光盘镜像文件,放到虚拟的光驱设备
2.查看Linux的光驱设备
[root@localhost ~]# ls /dev/sr0 #光驱设备实际名字
/dev/sr0
[root@localhost ~]# ls /dev/cdrom #快捷方式,颜色:青色
/dev/cdrom
[root@localhost ~]# ls -l /dev/cdrom
lrwxrwxrwx. 1 root root 3 6月 1 08:57 /dev/cdrom -> sr0
3.建立一个目录,进行挂载
[root@localhost ~]# mkdir /dvd
[root@localhost ~]# ls /dvd
[root@localhost ~]# mount /dev/cdrom /dvd
mount: /dev/sr0 写保护,将以只读方式挂载
[root@localhost ~]# ls /dvd/
[root@localhost ~]# ls /dvd/Packages/
4.卸载
[root@localhost ~]# umount /dvd
[root@localhost ~]# ls /dvd/
[root@localhost ~]# mkdir /nsd01
[root@localhost ~]# ls /nsd01
[root@localhost ~]# mount /dev/cdrom /nsd01
[root@localhost ~]# ls /nsd01
注意事项:
1.卸载当前位置不能是挂载点目录
[root@localhost nsd01]# umount /nsd01
umount: /nsd01:目标忙。
(有些情况下通过 lsof(8) 或 fuser(1) 可以
找到有关使用该设备的进程的有用信息)
2.挂载点目录,最后自己新建。不要用根目录下已存在的目录
3.允许一个设备的挂载点有多个,不允许一个挂载点下有多个设备
[root@localhost /]# mount /dev/cdrom /dvd/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@localhost /]# mount /dev/cdrom /nsd01
mount: /dev/sr0 写保护,将以只读方式挂载
[root@localhost /]# ls /nsd01
[root@localhost /]# ls /dvd/
四、目录及文件管理
•cd — Change Directory
–用途:切换工作目录
–格式:cd [目标文件夹位置]
. 当前目录
… 父目录(上一层)
~ 用户家目录(专门存放用户个性化信息的目录)
~user 用户user的家目录
/root:管理员root的家目录
/home:存放所有普通用户的家目录
[root@localhost /]# cd ~root #去往用户root的家目录
[root@localhost ~]# pwd
[root@localhost lisi]# useradd tom #创建普通用户tom
[root@localhost lisi]# cd ~tom #去往用户tom的家目录
[root@localhost tom]# pwd
/home/tom
[root@localhost tom]# useradd zhangsan #创建普通用户zhangsan
[root@localhost tom]# cd ~zhangsan #去往用户zhangsan的家目录
[root@localhost zhangsan]# pwd
/home/zhangsan
[root@localhost zhangsan]#
•ls — List
–格式:ls [选项]… [目录或文件名]
•常用命令选项
– -l:以长格式显示,显示详细属性
– -A:显示所有内容包含隐藏数据
– -d:显示目录本身(而不是内容)的属性
– -h:提供易读的容量单位(K、M等)
– -R:递归显示内容
[root@localhost /]# ls -l /etc/passwd
[root@localhost /]# ls -lh /etc/passwd #显示详细属性加上易读单位
[root@localhost /]# ls -l /opt/
[root@localhost /]# ls -ld /opt/ #显示目录本身的详细属性
[root@localhost /]# ls -ld /root
[root@localhost /]# ls -ld /
[root@localhost /]# ls -A /root #显示隐藏的数据
[root@localhost /]# ls -A /boot
[root@localhost /]# ls -A /opt
[root@localhost /]# ls -R /boot #显示目录本身以及目录下所有目录的内容
[root@localhost /]# ls -R /
统计目录大小:
[root@localhost /]# du -sh /boot
125M /boot
[root@localhost /]# ls -lh /etc/passwd #显示文件本身的大小
-rw-r–r--. 1 root root 2.3K 6月 1 11:54 /etc/passwd
[root@localhost /]# du -sh /etc/passwd #显示文件占用磁盘空间的大小
4.0K /etc/passwd
[root@localhost /]#
•针对不确定的文档名称,以特殊字符表示
–:任意多个任意字符
–?:单个字符
[root@localhost /]# ls /root/a
[root@localhost /]# ls /root/g
[root@localhost /]# ls /etc/.conf
[root@localhost /]# ls /etc/*tab
[root@localhost /]# ls /etc/?tab
[root@localhost /]# ls /etc/??tab
[root@localhost /]# ls /dev/tty?
[root@localhost /]# ls /dev/tty??
•针对不确定的文档名称,以特殊字符表示
–[a-z]:多个字符或连续范围中的一个,若无则忽略
–{a,min,xy}:多组不同的字符串,全匹配
[root@localhost /]# ls /dev/tty[1-8]
[root@localhost /]# ls /dev/tty[3-6]
[root@localhost /]# ls /dev/tty[0-9]
[root@localhost /]# ls /dev/tty{1,2,5,12,33}
[root@localhost /]# ls /dev/tty{20,21,22,23}
•别名的定义:简化复杂的命令
•查看已设置的别名
–alias [别名名称]
•定义新的别名
–alias 别名名称= ‘实际执行的命令行’
•取消已设置的别名
–unalias [别名名称]
[root@localhost /]# hostname
[root@localhost /]# alias hn=‘hostname’ #定义别名
[root@localhost /]# hn
[root@localhost /]# alias myls=‘ls -ld’ #定义别名
[root@localhost /]# myls /root
[root@localhost /]# alias #显示当前系统中有哪些别名
[root@localhost /]# unalias hn #删除别名
[root@localhost /]# hn
bash: hn: 未找到命令…
[root@localhost /]#
•rm — Remove
–格式:rm [选项]… 文件或目录…
•常用命令选项
– -r、-f:递归删除(含目录)、强制删除
[root@localhost /]# rm -rf /root/1.txt
[root@localhost /]# rm -rf /root/nsd01
[root@localhost /]# ls /root
•mkdir — Make Directory
–格式:mkdir [-p] [/路径/]目录名…
[root@localhost /]# mkdir -p /opt/aa/bb/cc/dd #创建多层的目录
[root@localhost /]# ls -R /opt/aa
[root@localhost /]# rm -rf /opt/aa
•mv — Move
–格式:mv 原文件… 目标路径
[root@localhost /]# touch /opt/a.txt
[root@localhost /]# mkdir /opt/nsd01
[root@localhost /]# ls /opt/
[root@localhost /]# mv /opt/a.txt /opt/nsd01
[root@localhost /]# ls /opt/
[root@localhost /]# ls /opt/nsd01
重命名:路径不变的移动
[root@localhost /]# ls /opt/
[root@localhost /]# mv /opt/nsd01/ /opt/abc01
[root@localhost /]# ls /opt/
[root@localhost /]# mv /opt/abc01/ /opt/stu01
[root@localhost /]# ls /opt/
•cp — Copy
–格式:cp [选项]… 原文件… 目标路径
•常用命令选项
–-r:递归,复制目录时必须有此选项
[root@localhost /]# cp /etc/shells /opt/
[root@localhost /]# ls /opt/
[root@localhost /]# cp /etc/redhat-release /opt/
[root@localhost /]# ls /opt/
[root@localhost /]# cp -r /home/ /opt
[root@localhost /]# ls /opt/
重复数据:取消提示
[root@localhost /]# cp -r /boot/ /opt/
[root@localhost /]# \cp -r /boot/ /opt/ #在本次操作临时取消别名
复制支持多个参数:
永远会把最后一个参数作为目标,其他参数都作为源数据
[root@localhost /]# cp /etc/fstab /etc/passwd /etc/hosts /opt
[root@localhost /]# ls /opt/
复制支持重新命名:
[root@localhost /]# cp /etc/redhat-release /opt/r.txt
[root@localhost /]# ls /opt/
[root@localhost /]# cp -r /home/ /opt/myhome
[root@localhost /]# ls /opt/
复制与一个点的连用:将源数据复制到当前路径下
[root@localhost /]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# pwd
[root@localhost network-scripts]# cp /etc/passwd .
[root@localhost network-scripts]# ls
[root@localhost network-scripts]# cd /mnt/
[root@localhost mnt]# pwd
[root@localhost mnt]# ls
[root@localhost mnt]# cp /etc/passwd .
[root@localhost mnt]# ls
•根据字符串模式提取文本行
–grep [选项] ‘匹配模式’ 文本文件…
•常用命令选项
– -v,取反匹配
– -i,忽略大小写
[root@localhost /]# grep root /etc/passwd
[root@localhost /]# grep -v root /etc/passwd
[root@localhost /]# grep man /etc/man_db.conf
[root@localhost /]# grep -i man /etc/man_db.conf
•常用的匹配模式
–word 包含字符串word
–^word 该行以字符串word开头
–word$ 该行以字符串word结尾
[root@localhost /]# grep ^root /etc/passwd
[root@localhost /]# grep root$ /etc/passwd
[root@localhost /]# grep bash$ /etc/passwd