一、图形化浏览目录
二、linux命令格式及帮助命令
2.1、linux命令格式
command options arguments
命令、选项、参数之间用空格隔开
选项可以有多个,参数也可以有多个
多个命令连续执行,命令和命令之间可以用分号
2.2、linux帮助命令
help cd
ls --help
man cp
|---------|--------------------------|---------------|
| [] | 中括号代表可选大写或<>里的是需要提供的参数 | 如 cp --help |
| ... | 表示可以重复多个 | |
| a|b|c | 管道符表示以下选项选其中之一 | 如 date --help |


三、linux目录管理
3.1、树形目录结构
"/"根目录:
所有分区、目录。文件等的位置起点
整个树形目录结构中,使用独立的一个"/"表示
3.2、常见子目录
|----|-----------|-----------------------------------------------|
| 序号 | 目录名称说明 | 说明 |
| 1 | /root | 管理员的宿主(家)目录 |
| 2 | /home/xxx | 普通用户的家目录 |
| 3 | /bin | 命令文件目录,存放所有用户可执行的命令 |
| 4 | /sbin | 管理员操作目录,存放只有管理员可执行的管理命令 |
| 5 | /boot | 系统内核、启动文件目录 |
| 6 | /dev | 存放设备文件(光驱。硬盘等) |
| 7 | /etc | 存放系统程序和大部分应用程序(rpm/yum安装)的配置文件(一般以.conf、.ini) |
| 8 | /var | 存放可以变化的文件,包括各种日志文件 (/var/log) |
| 9 | /lib | 存放系统程序的动态连接共享库文件 (类似于Windows里的DLL文件) |
| 10 | /usr | 存放系统用户工具和程序 |
| 11 | /media | 可拆卸的媒介挂载点,例如U盘。光驱等 |
| 12 | /proc | 存放映射系统信息的文件 |
| 13 | /mnt | 用于临时挂载存储设备的目录 (不要放重要文件) |
| 14 | /opt | 第三方应用程序安装所存放的目录 |
| 15 | /tmp | 存放系统的临时文件 |
3.3、目录操作命令
pwd :显示当前工作目录的绝对路径。在操作过程中,随时可以使用该命令查看当前所在位置。
[root@centos7 ~]# pwd
/root
[root@centos7 ~]# cd /home/ob
[root@centos7 ob]# pwd
/home/ob
cd: 切换目录。使用
cd 目录名可以进入指定目录,cd ..返回上一级目录,cd ~返回用户主目录。
bash
#cd .. 返回上一级目录
[root@centos7 ~]# cd /home/ob
[root@centos7 ob]# pwd
/home/ob
[root@centos7 ob]# cd ..
[root@centos7 home]# pwd
/home
#cd ~ 返回用户主目录
[root@centos7 ob]# pwd
/home/ob
[root@centos7 ob]# cd
[root@centos7 ~]# pwd
/root
ls :列出目录中的文件和文件夹。常用选项如 -l(详细列表)、-a(显示隐藏文件)、-h(以易读格式显示文件大小)等。
例如,
ls -l可以查看文件的详细信息,包括权限、所有者、大小等;ls -a可以显示包括隐藏文件(以.开头的文件)在内的所有文件。
bash
#ls 列出当前目录文件
[root@centos7 ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg 下载 公共 图片 文档 桌面 模板 视频 音乐
#ls -l 列出文件详细信息
[root@centos7 ~]# ls -l
total 8
-rw-------. 1 root root 2123 Oct 30 15:48 anaconda-ks.cfg
-rw-r--r--. 1 root root 2154 Oct 30 15:49 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 Oct 30 16:07 下载
drwxr-xr-x. 2 root root 6 Oct 30 16:07 公共
drwxr-xr-x. 2 root root 6 Oct 30 16:07 图片
drwxr-xr-x. 2 root root 6 Oct 30 16:07 文档
drwxr-xr-x. 2 root root 6 Oct 30 16:07 桌面
drwxr-xr-x. 2 root root 6 Oct 30 16:07 模板
drwxr-xr-x. 2 root root 6 Oct 30 16:07 视频
drwxr-xr-x. 2 root root 6 Oct 30 16:07 音乐
#ls -a 显示所有
[root@centos7 ~]# ls -la
total 44
dr-xr-x---. 14 root root 4096 Nov 4 01:47 .
dr-xr-xr-x. 17 root root 224 Oct 30 15:47 ..
-rw-------. 1 root root 2123 Oct 30 15:48 anaconda-ks.cfg
-rw-r--r--. 1 root root 18 Dec 28 2013 .bash_logout
-rw-r--r--. 1 root root 176 Dec 28 2013 .bash_profile
-rw-r--r--. 1 root root 176 Dec 28 2013 .bashrc
drwx------. 13 root root 277 Oct 30 15:49 .cache
drwx------. 14 root root 261 Oct 30 16:07 .config
-rw-r--r--. 1 root root 100 Dec 28 2013 .cshrc
drwx------. 3 root root 25 Oct 30 15:48 .dbus
-rw-------. 1 root root 16 Oct 30 15:49 .esd_auth
-rw-------. 1 root root 1240 Nov 4 01:47 .ICEauthority
-rw-r--r--. 1 root root 2154 Oct 30 15:49 initial-setup-ks.cfg
drwx------. 3 root root 19 Oct 30 15:49 .local
-rw-r--r--. 1 root root 129 Dec 28 2013 .tcshrc
-rw-------. 1 root root 106 Nov 4 01:35 .Xauthority
drwxr-xr-x. 2 root root 6 Oct 30 16:07 下载
drwxr-xr-x. 2 root root 6 Oct 30 16:07 公共
drwxr-xr-x. 2 root root 6 Oct 30 16:07 图片
drwxr-xr-x. 2 root root 6 Oct 30 16:07 文档
drwxr-xr-x. 2 root root 6 Oct 30 16:07 桌面
drwxr-xr-x. 2 root root 6 Oct 30 16:07 模板
drwxr-xr-x. 2 root root 6 Oct 30 16:07 视频
drwxr-xr-x. 2 root root 6 Oct 30 16:07 音乐
#ls -ld 查看目录本身属性
[root@centos7 ~]# ls -ld
dr-xr-x---. 14 root root 4096 Nov 4 01:47 .
[root@centos7 ~]# ls -ld /
dr-xr-xr-x. 17 root root 224 Oct 30 15:47 /
[root@centos7 ~]# ls -ld /home
drwxr-xr-x. 3 root root 16 Oct 30 15:47 /home
#ls -lh 以易读格式显示
[root@centos7 ~]# ls -lh
total 8.0K
-rw-------. 1 root root 2.1K Oct 30 15:48 anaconda-ks.cfg
-rw-r--r--. 1 root root 2.2K Oct 30 15:49 initial-setup-ks.cfg
drwxr-xr-x. 2 root root 6 Oct 30 16:07 下载
drwxr-xr-x. 2 root root 6 Oct 30 16:07 公共
drwxr-xr-x. 2 root root 6 Oct 30 16:07 图片
drwxr-xr-x. 2 root root 6 Oct 30 16:07 文档
drwxr-xr-x. 2 root root 6 Oct 30 16:07 桌面
drwxr-xr-x. 2 root root 6 Oct 30 16:07 模板
drwxr-xr-x. 2 root root 6 Oct 30 16:07 视频
drwxr-xr-x. 2 root root 6 Oct 30 16:07 音乐
#ls -R递归显示子目录中的内容
[root@centos7 ~]# ls -R /home
/home:
ob
/home/ob:
#l. 只查看隐藏文件
[root@centos7 ~]# l.
. .bash_logout .bashrc .config .dbus .ICEauthority .tcshrc
.. .bash_profile .cache .cshrc .esd_auth .local .Xauthority
#只查看目录
[root@centos7 ~]# ls -d */
下载/ 公共/ 图片/ 文档/ 桌面/ 模板/ 视频/ 音乐/
通配符* ?:*匹配多个字符,?匹配单个字符
bash
[root@centos7 ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg 下载 公共 图片 文档 桌面 模板 视频 音乐
[root@centos7 ~]# ls *.cfg
anaconda-ks.cfg initial-setup-ks.cfg
mkdir :创建目录。
mkdir 目录名可创建一个新目录,mkdir -p 目录路径可递归创建多级目录。
bash
#创建目录
[root@centos7 ~]# cd /tmp
[root@centos7 tmp]# mkdir ppt
[root@centos7 tmp]# cd ppt
[root@centos7 ppt]# pwd
/tmp/ppt
#使用-p,创建递归目录
[root@centos7 ppt]# mkdir -p d1/d2/d3
[root@centos7 ppt]# ls
d1
[root@centos7 ppt]# ls -R d1
d1:
d2
d1/d2:
d3
d1/d2/d3:
#同时创建三个目录
[root@centos7 ppt]# mkdir d01 d02 d03
[root@centos7 ppt]# ls
d01 d02 d03 d1
rmdir:删除空目录,非空目录无法删除
bash
[root@centos7 ppt]# mkdir d01 d02 d03
[root@centos7 ppt]# ls
d01 d02 d03 d1
[root@centos7 ppt]# rmdir d01 d02 d03 d1
rmdir: failed to remove 'd1': Directory not empty
[root@centos7 ppt]# ls
d1
du :查看文件或目录所占用的磁盘空间大小。
du -sh 目录名可查看指定目录的总大小(以易读格式显示),du -h 目录名则可详细显示目录下每个子文件和子目录的大小。
bash
[root@centos7 ppt]# du -sh /tmp
56K /tmp
四、文件操作命令
touch :创建空文件。
touch 文件名可生成一个指定名称的空文件,常用于创建日志文件或作为其他操作的基础文件。
bash
[root@centos7 ppt]# touch file1 file2
[root@centos7 ppt]# ll
total 0
drwxr-xr-x. 3 root root 16 Nov 4 02:45 d1
-rw-r--r--. 1 root root 0 Nov 4 02:52 file1
-rw-r--r--. 1 root root 0 Nov 4 02:52 file2
file:查看文件类型
bash
[root@centos7 ppt]# file d1
d1: directory #目录
[root@centos7 ppt]# file file1
file1: empty #空文件
[root@centos7 ppt]# file /etc/passwd
/etc/passwd: ASCII text #普通文本文件
[root@centos7 ppt]# file /bin/ls
/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=aaf05615b6c91d3cbb076af81aeff531c5d7dfd9, stripped #可执行文件
cp :复制文件或目录。
cp 源文件 目标文件复制文件,cp -r 源目录 目标目录复制目录。
bash
#复制file1文件
[root@centos7 ppt]# cp file1 file1.bak
[root@centos7 ppt]# ls
d1 file1 file1.bak file2
#使用相对路径复制file1和file1.bak 到d1里
[root@centos7 ppt]# cp file1 file1.bak d1
[root@centos7 ppt]# ls d1
d2 file1 file1.bak
#创建一个新目录d01,使用cp -r将d1目录级联复制到d01目录
[root@centos7 ppt]# mkdir d01
[root@centos7 ppt]# cp -r d1/ d01/
[root@centos7 ppt]# ls
d01 d1 file1 file1.bak file2
[root@centos7 ppt]# ls -R d01
d01:
d1
d01/d1:
d2 file1 file1.bak
d01/d1/d2:
d3
d01/d1/d2/d3:
#若目标文件已存在,会覆盖目标文件
[root@centos7 ppt]# cp file1 file2
cp: overwrite 'file2'? y
rm :删除文件或目录。
rm 文件名删除文件,rm -r 目录名递归删除目录及其内容。需谨慎使用,删除后的数据难以恢复。
bash
#删除文件file1.bak
[root@centos7 ppt]# rm file1.bak
rm: remove regular empty file 'file1.bak'? y
[root@centos7 ppt]# ls
d01 d1 file1 file2
#删除目录使用-r选项
[root@centos7 ppt]# rm -r d01
rm: descend into directory 'd01'? y
rm: descend into directory 'd01/d1'? y
rm: descend into directory 'd01/d1/d2'? y
rm: remove directory 'd01/d1/d2/d3'? y
rm: remove directory 'd01/d1/d2'? y
rm: remove regular empty file 'd01/d1/file1'? y
rm: remove regular empty file 'd01/d1/file1.bak'? y
rm: remove directory 'd01/d1'? y
rm: remove directory 'd01'? y
#使用-f选项强制删除
[root@centos7 ppt]# rm -f file2
[root@centos7 ppt]# ls
d1 file1
[root@centos7 ppt]# rm -rf d1
[root@centos7 ppt]# ls
file1
mv :移动或重命名文件或目录。
mv 源文件 目标路径将文件移动到指定位置,mv 原文件名 新文件名对文件进行重命名。
bash
[root@centos7 ppt]# mkdir dir1
[root@centos7 ppt]# touch file1
[root@centos7 ppt]# ll
total 0
drwxr-xr-x. 2 root root 6 Nov 4 03:48 dir1
-rw-r--r--. 1 root root 0 Nov 4 03:48 file1
#将文件file1移动到dir1目录下并改名为file1.bak
[root@centos7 ppt]# mv file1 dir1/file1.bak
[root@centos7 ppt]# ll
total 0
drwxr-xr-x. 2 root root 23 Nov 4 03:49 dir1
[root@centos7 ppt]# ll dir1/
total 0
-rw-r--r--. 1 root root 0 Nov 4 03:48 file1.bak
#将dir1重命名为dir2
[root@centos7 ppt]# mv dir1/ dir2/
[root@centos7 ppt]# ll
total 0
drwxr-xr-x. 2 root root 23 Nov 4 03:49 dir2
which:显示命令文件所在位置,外部命令的执行,依赖环境变量PATH对应的值,会去PATH设置的路径去寻找命令对应的文件,能够找到则命令可以执行;内部命令属于内置在shell中,不受PATH影响。
bash
[root@centos7 ~]# which cd
/usr/bin/cd
[root@centos7 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
find:查找文件。find 查找路径 -name 文件名 可在指定路径下查找符合名称条件的文件,还可结合其他选项如 -type(指定文件类型)、-mtime(按照修改时间查找)等进行更精确的查找。
bash
#使用通配符*匹配多个字符
[root@centos7 ppt]# touch file1 file2
[root@centos7 ppt]# mkdir dir1 dir2
[root@centos7 ppt]# find /tmp -name file1
/tmp/ppt/file1
[root@centos7 ppt]# find /tmp -name 'file*'
/tmp/ppt/file1
/tmp/ppt/file2
#使用-type选项
[root@centos7 ~]# find /tmp/ppt -type f
/tmp/ppt/file1
/tmp/ppt/file2
[root@centos7 ~]# find /tmp/ppt -type d
/tmp/ppt
/tmp/ppt/dir1
/tmp/ppt/dir2
五、文件内容操作命令
cat :查看文件内容。
cat 文件名可显示文件内容,对于小文件快速查看内容较为方便。
bash
[root@centos7 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
>:重定向符,>覆盖重定向,>>追加重定向
bash
[root@centos7 ppt]# rm -rf *
[root@centos7 ppt]# echo 'hello file1' > file1
[root@centos7 ppt]# echo 'hello file2' > file2
[root@centos7 ppt]# cat file1
hello file1
[root@centos7 ppt]# cat file1 file2 > file3
[root@centos7 ppt]# cat file3
hello file1
hello file2
[root@centos7 ppt]# echo 'hello file3' > file3
[root@centos7 ppt]# cat file3
hello file3
[root@centos7 ppt]# echo 'hello liunx' >> file3
[root@centos7 ppt]# cat file3
hello file3
hello liunx
less:分页查看文件内容。与 cat 相比,less 更适合查看大文件,使用 less 文件名 打开文件后,可通过上下箭头或空格键翻页,按 q 键退出查看。
more :与 less 类似,也是分页查看文件内容,但功能相对较少,翻页操作不如 less 灵活。
bash
[root@centos7 ~]# cp /etc/services /tmp/services.bak
#将内容全部显示
[root@centos7 ~]# cat /tmp/services.bak
#使用less命令分页查看文件内容,可通过上下箭头或空格键翻页,按 q 键退出查看。
[root@centos7 ~]# less /tmp/services.bak
#使用less命令分页查看文件内容,可通过回车或空格键翻页,b上一页,按 q 键退出查看。
[root@centos7 ~]# more /tmp/services.bak
head:查看文件的开头部分。head -n 文件名 默认显示文件的前 10 行内容,通过 -n 选项可指定显示的行数。
tail:查看文件的结尾部分。tail -n 文件名 默认显示文件的后 10 行内容,同样可通过 -n 选项调整显示行数。此外,tail -f 文件名 可实时查看文件的尾部新增内容,常用于监控日志文件的更新。
bash
#查看前三行内容
[root@centos7 ~]# head -3 /tmp/services.bak
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
#查看后三行内容
[root@centos7 ~]# tail -3 /tmp/services.bak
iqobject 48619/tcp # iqobject
iqobject 48619/udp # iqobject
matahari 49000/tcp # Matahari Broker
#-f选项可以动态追,新开一个session向/tmp/service.bak追加内容查看情况
[root@centos7 ~]# tail -f /tmp/services.bak
[root@centos7 ~]# cat /etc/passwd >> /tmp/services.bak
wc :统计文件中的行数、字数和字节数。
wc -l 文件名统计文件的行数,wc -w统计字数,wc -c统计字节数,帮助了解文件的大致规模和内容量。
bash
[root@centos7 ~]# wc /etc/passwd
43 87 2253 /etc/passwd
[root@centos7 ~]# wc -lwc /etc/passwd
43 87 2253 /etc/passwd
grep:在文件中查找符合条件的字符串。grep "查找内容" 文件名 可在指定文件中查找包含 "查找内容" 的行,配合选项如 -i(忽略大小写)、-n(显示行号)、-r(递归查找目录)等可实现更强大的查找功能。
bash
[root@centos7 ~]# cp /etc/passwd /tmp/passwd.bak
#查看以root开头的行
[root@centos7 ~]# grep '^root' /tmp/passwd.bak
root:x:0:0:root:/root:/bin/bash
#查看以bash结尾的行
[root@centos7 ~]# grep 'bash$' /tmp/passwd.bak
root:x:0:0:root:/root:/bin/bash
ob:x:1000:1000:ob:/home/ob:/bin/bash
#查看包含system的行,忽略大小写
[root@centos7 ~]# grep system /tmp/passwd.bak
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
[root@centos7 ~]# grep -i system /tmp/passwd.bak
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
#查看不包含nologin的行
[root@centos7 ~]# grep -v nologin /tmp/passwd.bak
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
ob:x:1000:1000:ob:/home/ob:/bin/bash
|:管道符,用来连接多个命令,把前一个命令的输出作为后一个命令的输入,在文件查找中使用管道符把前面命令的结果作为查找的文件
bash
[root@centos7 ~]# cat /etc/services | more
[root@centos7 ~]# cat /etc/passwd | grep root
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
六、归档及压缩命令
gzip:对文件进行压缩,将文件压缩为.gz 格式。
bzip2 :提供更高的压缩率,将文件压缩为.bz2 格式。
zip/unzip :支持对文件和目录进行压缩和解压,生成.zip 格式文件。
bash
#创建两个10M大小的文件
[root@centos7 ppt]# rm -rf *
[root@centos7 ppt]# dd if=/dev/zero of=file1 bs=1M count=10
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.00645048 s, 1.6 GB/s
[root@centos7 ppt]# dd if=/dev/zero of=file2 bs=1M count=10
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.0134052 s, 782 MB/s
[root@centos7 ppt]# ls -lh
total 20M
-rw-r--r--. 1 root root 10M Nov 4 04:46 file1
-rw-r--r--. 1 root root 10M Nov 4 04:47 file2
#分别使用gzip和bzip2进行压缩
[root@centos7 ppt]# gzip file1
[root@centos7 ppt]# bzip2 file2
[root@centos7 ppt]# ls -lh
total 16K
-rw-r--r--. 1 root root 10K Nov 4 04:46 file1.gz
-rw-r--r--. 1 root root 49 Nov 4 04:47 file2.bz2
#解压缩
[root@centos7 ppt]# gunzip file1.gz
[root@centos7 ppt]# bzip2 -d file2.bz2
[root@centos7 ppt]# ls -lh
total 20M
-rw-r--r--. 1 root root 10M Nov 4 04:46 file1
-rw-r--r--. 1 root root 10M Nov 4 04:47 file2
tar :归档工具,常与压缩功能结合使用。
tar -cvf :创建归档文件
tar -xvf :解压归档文件
tar -tvf :查看归档文件中的内容列表。
bash
#将/etc目录打包到/tmp/ppt目录下,生成etc1.tar文件
[root@centos7 ppt]# tar -cvf etc1.tar /etc/
#解包
[root@centos7 ppt]# tar -xvf etc1.tar
[root@centos7 ppt]# ls -lh
total 58M
drwxr-xr-x. 140 root root 8.0K Oct 30 15:48 etc
-rw-r--r--. 1 root root 38M Nov 4 04:51 etc1.tar
-rw-r--r--. 1 root root 10M Nov 4 04:46 file1
-rw-r--r--. 1 root root 10M Nov 4 04:47 file2
#解包到指定目录使用-C选项
[root@centos7 ppt]# mkdir dir1
[root@centos7 ppt]# tar -xvf etc1.tar -C dir1
[root@centos7 ppt]# cp -r /etc/ ./etc1
[root@centos7 ppt]# cp -r /etc/ ./etc2
[root@centos7 ppt]# ls -lh
total 21M
drwxr-xr-x. 3 root root 18 Nov 4 04:55 dir1
drwxr-xr-x. 140 root root 8.0K Oct 30 15:48 etc
drwxr-xr-x. 140 root root 8.0K Nov 4 04:53 etc1
-rw-r--r--. 1 root root 10K Nov 4 04:53 etc1.tar
drwxr-xr-x. 140 root root 8.0K Nov 4 04:55 etc2
-rw-r--r--. 1 root root 10M Nov 4 04:46 file1
-rw-r--r--. 1 root root 10M Nov 4 04:47 file2
#无法对目录进行压缩
[root@centos7 ppt]# gzip etc1/
gzip: etc1/ is a directory -- ignored
#可以先打包在压缩,也可以打包同时压缩使用-z选项
[root@centos7 ppt]# tar -cf etc1.tar etc1/
[root@centos7 ppt]# gzip etc1.tar
[root@centos7 ppt]# tar -zcf etc2.tar.gz etc2/
#解包解压缩
[root@centos7 ppt]# tar -zxf etc1.tar.gz -C dir1
七、vim文本编辑器
7.1、使用vim
bash
[root@centos7 ~]# vim /etc/passwd
7.2、禁用selinux
bash
#查看selinux状态
[root@centos7 ~]# getenforce
Enforcing
#临时更改
[root@centos7 ~]# setenforce 0
[root@centos7 ~]# getenforce
Permissive
#永久更改
[root@centos7 ~]# vim /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
#重启验证
[root@centos7 ~]# reboot
[root@centos7 ~]# getenforce
Permissive
