基本命令的认识
服务器:
1 安装 默认安装成功
2 配置
vim /etc/exports
格式:
sharedir client(option)
案例:
/ken *(ro)
/tools *(ro)
/homework *(rw)
client :
* : 表示能ping通服务器的所有客户端
192.168.0.* : 表示只有192.168.0网络所有用户可以访问
192.168.0.110 : 表示只有110这个用户可以访问
3 重启服务
[root@localhost test]# service nfs
用法:nfs {start|stop|status|restart|reload|condrestart}
service nfs restart : 表示重启服务
客户端:
1 检测网络 ping
2 查看服务器共享目录 showmout
3 挂载 mount => /mnt
4 拷贝 cp
5 解挂 umount
案例命令
1371 cd / 切换到根目录
1372 pwd
1373 ls
1374 mkdir hello123 创建一个目录
1375 ls
1376 vim /etc/exports nfs配置文件
i => /hello123 *(ro) => ESC键 => :wq => enter
1377 service nfs restart 重启服务
客户端
1379 ping 127.0.0.1 -c 3 检测网络
1380 showmount -e 127.0.0.1 查看共享
1381 mount 127.0.0.1:/hello123 /mnt 挂载
1382 df -h 查看挂载是否成功
1383 ls /mnt 查看内容
==============================================================
ls : 显示目录相关文件或者目录
ls [option] … [filename | dirname] …
option:
-a : 表示显示所有文件信息(隐藏和非隐藏)
-A : 表示除了不显示.和..,其他都显示
-l : 表示长格式显示
[root@localhost test]# ls -l
总计 12
-rwxr-xr-x 1 root root 4729 07-24 14:52 hello
-rw-r--r-- 1 root root 78 07-24 14:52 hello.c
空格分割成7列
第一列:表示文件类型和权限
文件类型:第一列的第一个字符表示
- : 表示普通文件
d : 表示目录文件
c : 表示字符设备类文件 device driver
b : 表示块设备类文件
l : 表示链接文件
s : 表示套接子文件 net
p : 表示管道文件 sys
文件权限
r : 表示只读 4
w : 表示只写 2
x : 表示可执行 1
- : 表示无权限
三位为一组,共三组
第一组 : 表示属主的权限
第二组 : 表示属组的权限
第三组 : 表示其他用户权限
设置文件权限
属主: u
属组: g
其他用户 : o
所有用户 :a
chmod option filename
添加权限:+
删除权限:-
赋值权限:=
案例一:字符指定方法
[root@localhost test]# chmod a=rwx hello.c
[root@localhost test]# ll
总计 12
-rwxr-xr-x 1 root root 4729 07-24 14:52 hello
-rwxrwxrwx 1 root root 102 07-24 17:30 hello.c
prw-r--r-- 1 root root 0 07-24 17:22 ken
[root@localhost test]# chmod u=rwx,g=rx,o=r hello.c
[root@localhost test]# ll
总计 12
-rwxr-xr-x 1 root root 4729 07-24 14:52 hello
-rwxr-xr-- 1 root root 102 07-24 17:30 hello.c
prw-r--r-- 1 root root 0 07-24 17:22 ken
案例二: 数字指定方法
[root@localhost test]# chmod 07 hello.c
[root@localhost test]# ll
总计 12
-rwxr-xr-x 1 root root 4729 07-24 14:52 hello
-------rwx 1 root root 102 07-24 17:30 hello.c
prw-r--r-- 1 root root 0 07-24 17:22 ken
[root@localhost test]# chmod 0700 hello.c
[root@localhost test]# ll
总计 12
-rwxr-xr-x 1 root root 4729 07-24 14:52 hello
-rwx------ 1 root root 102 07-24 17:30 hello.c
prw-r--r-- 1 root root 0 07-24 17:22 ken
设置文件属主和属组
chown
chown tom hello.c 指定文件属主
chown :jim hello.c 指定文件属组
chown lili:mary hello 指定属主和属组
第二列:表示文件硬连接数
计算机识别文件唯一标识:文件索引号 inode节点号
-i : 显示文件索引号
硬链接:
1 创建硬链接
ln srcfilename destfilename
2 相当于给文件做了一个备份
3 文件inode节点号是一致
4 不可以操作目录
5 不可以跨分区
软连接:
1 创建软连接
ln -s srcfilename destfilename
2 inode号不一致
3 相当于给文件做快捷方式
4 可以操作目录
5 不可以跨分析
第三列:表示文件属主(文件拥有者)
第四列:表示文件属组(文件拥有组)
第五列:表示文件大小(字节)
byte => K => M => G => T => p....
1k = 1024B
1M = 1024K
bit 位
1B = 8bit
-h : 表示易读方式显示文件大小
第六列:表示修改文件内容时间
stat : 查看文件属性(状态)
[root@localhost test]# stat a.txt
File: “a.txt”
Size: 24 Blocks: 8 IO Block: 4096
一般文件
Device: 802h/2050d Inode: 1829638 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: (
0/ root)
Access: 2020-07-25 10:06:16.000000000 +0800
Modify: 2020-07-25 10:06:16.000000000 +0800
Change: 2020-07-25 10:06:16.000000000 +0800
atime:访问时间 cat tac head tail more less
mtime:修改文件内容,同时会触发ctime时间
ctime:修改文件属性(chmod chown)
第七列:表示文件名
同一个目录中起相同的文件名或者目录名
=================================================
cd : 切换目录
format :
cd + dirname
案例
cd : 表示切换到当前用户家目录
or
cd ~
管理员家目录: /root
普通用户家目录:/home/usrname
~ : 表示当前用户家目录
目录路径:
绝对路径:就是从根开始的路径
相对路径:就是当前工作目录开始的路径
案例:
[root@localhost test]# pwd
/test
[root@localhost test]# cd /etc/sysconfig/network-scripts/ 绝对路径
[root@localhost network-scripts]# ls
ifcfg-eth0 ifdown-ppp ifup-ipsec ifup-sl
ifcfg-lo ifdown-routes ifup-ipv6 ifup-tunnel
ifdown ifdown-sit ifup-ipx ifup-wireless
ifdown-bnep ifdown-sl ifup-isdn init.ipv6-global
ifdown-eth ifdown-tunnel ifup-plip net.hotplug
ifdown-ippp ifup ifup-plusb network-functions
ifdown-ipsec ifup-aliases ifup-post network-functions-ipv6
ifdown-ipv6 ifup-bnep ifup-ppp
ifdown-isdn ifup-eth ifup-routes
ifdown-post ifup-ippp ifup-sit
[root@localhost network-scripts]# cd - 表示切换到上次目录所在路径
/test
[root@localhost test]# cd ../etc/sysconfig/network-scripts/ 相对路径
[root@localhost network-scripts]# pwd
/etc/sysconfig/network-scripts
注意:
. : 表示当前目录
.. : 表示上级目录
.filename : 表示隐藏文件
.dirname : 表示隐藏目录
案例:
[root@localhost network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@localhost network-scripts]# cd ....
bash: cd: ....: 没有那个文件或目录
[root@localhost network-scripts]# cd ../..
[root@localhost etc]# pwd
/etc
pwd : 表示查看当前工作目录的一个绝对路径
创建文件 touch
[root@localhost test]# touch a 表示创建一个文件
[root@localhost test]# touch a1 a2 a3 a4
[root@localhost test]# touch a{1,2}b{1,2,3}
[root@localhost test]# touch a{1,2} b{1,2,3}
[root@localhost test]# touch ‘a{1,2}b{1,2,3}’
删除文件 rm
option:
-f : 表示强制删除
-r : 表示删除目录
注意:
cd / && rm * -rf !!!!!!!!!
rm /* -rf !!!!!!!!!!!!!!!!
* : 表示匹配所有文件
?:表示任意匹配一个字符
创建目录 mkdir
mkdir aa …
案例
[root@localhost test]# mkdir bb/cc/dd/ee/ff
mkdir: 无法创建目录 “bb/cc/dd/ee/ff”: 没有那个文件或目录
上级目录不存在,以递归的方式创建目录
[root@localhost test]# mkdir -p bb/cc/dd/ee/ff
[root@localhost test]# tree
.
|-- aa
| `-- bb
`-- bb
`-- cc
`-- dd
`-- ee
`-- ff
以树状显示目录结构
tree dirname
删除目录 rmdir
删除一个非空的目录
创建文件和目录的时候权限不一样
创建是文件:0644
创建是目录:0755
默认权限:umask
[root@localhost test]# umask
0022
一般权限最高权限 0777
文件 0666 - 0022 => 0644
目录 0777 - 0022 => 0755
修改umask:
临时修改:
[root@localhost test]# umask 02
永久修改:
vim /etc/bashrc
结尾添加
umask 044
拷贝文件或者目录
cp
format : cp src dest option
option:
-r : 表示拷贝目录
案例
[root@localhost test]# which cp
alias cp='cp -i'
/bin/cp
无需交互式拷贝方式
[root@localhost test]# /bin/cp a.txt b.txt
[root@localhost test]# /bin/cp a.txt b.txt
[root@localhost test]# /bin/cp a.txt b.txt
[root@localhost test]# /bin/cp a.txt b.txt
[root@localhost test]# \cp a.txt b.txt
[root@localhost test]# \cp a.txt b.txt
[root@localhost test]# \cp a.txt b.txt
需要用户确认是否拷贝
[root@localhost test]# cp a.txt b.txt
cp:是否覆盖“b.txt”? y
[root@localhost test]# cp a.txt b.txt -f
cp:是否覆盖“b.txt”? y
[root@localhost test]#
案例:
[root@localhost test]# cp *.c src/ 表示拷贝所有.c到src目录中
[root@localhost test]# ls
1.c a aa a.txt ccc dddd hello.c paswd.txt src tt.c while.txt
[root@localhost test]# tree src/
src/
|-- 1.c
|-- hello.c
`-- tt.c
* 表示匹配任意多个字符
[root@localhost test]# cp ???? dest/
表示拷贝含有四个字符文件名拷贝到dest目录中
? 表示匹配任意一个字符
案例:
[root@localhost test]# cp dest src -r 拷贝目录
[root@localhost test]# tree src/
src/
|-- 1.c
|-- dest
| |-- dddd
| `-- tt.c
|-- hello.c
`-- tt.c
1 directory, 5 files
==========================================
移动或者改名
mv
案例 改名
[root@localhost test]# touch a
[root@localhost test]# ls
a
[root@localhost test]# mv a b
[root@localhost test]# ll
总计 0
-rw-rw-r-- 1 root root 0 07-25 11:40 b
案例 移动
[root@localhost test]# mv b /tmp
[root@localhost test]# ls
[root@localhost test]# ls /tmp/b
/tmp/b
案例 移动并改名
[root@localhost test]# mv a /tmp/aaa
[root@localhost test]# ls /tmp/aaa
/tmp/aaa
查看文件内容
cat option filename
option
-n : 表示显示行号
反着查看
tac
分屏查看:向下滚动
more => enter
less => 方向键
从前查看
head : 默认前10行
[root@localhost test]# head -n 5 passwd 指定查看文件前5行
从后查看
tail
[root@localhost test]# tail -n 1 passwd 查看后面1行
========================================================
文本操作基本命令
grep : 查找匹配信息
grep root a 表示在文件中查找匹配字符串的行并显示出来
grep -v root a 表示显示不含root关键字的行
grep -w root a 表示查找匹配root单词
grep -n root a 表示查找到关键行的行号显示
/etc/passwd : 用户帐号信息文件
root:x:0:0:this is root username:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
以:为分割符
第一列:表示用户名
第二列:表示密码
第三列:表示用户uid
第四列:表示组gid
第五列:表示描述行信息
第六列:表示用户家目录
第七列:表示用户使用shell
cut : 表示切割
cut -d: -f 1 passwd
-d : 表示切割符
-f : 表示取字段
cut -d: -f 1,7 passwd 表示取出第一列和第七列
cut -c 1-10 passwd 表示取出前面10个字符
sort : 表示排序
sort passwd 默认升序
sort -r passwd 降序排列
uniq : 表示去掉重复行
uniq a : 表示去掉连续重复的行
uniq -c a : 表示统计重复行
uniq -d a :表示显示重复行
uniq -u a :表示打印不重复行
wc : 统计
-l : 表示统计行
-w : 表示统计有多少个单词
-c : 表示统计有多少个字符
tr : 替换
[root@localhost test]# echo hello | tr 'a-z' 'A-Z'
HELLO
把小写替换成大写
==================================================
重定向:
设备名 sys c
标准输入 键盘 鼠标 0 stdin
标准输出 显示器 打印机 1 stdout
错误输出 显示器 打印机 2 stderr
输出重定向:
正确输出重定向 >
案例
ls / > a.txt
错误输出重定向 2>
案例
[root@localhost test]# ls /lkhdja > a.txt
ls: /lkhdja: 没有那个文件或目录
[root@localhost test]# cat a.txt
[root@localhost test]# ls /lkhdja 2> a.txt
[root@localhost test]# cat a.txt
ls: /lkhdja: 没有那个文件或目录
正确和错误放在同一个文件中 &>
案例
[root@localhost test]# ls / /lkjlds &> a.txt
[root@localhost test]# cat a.txt
ls: /lkjlds: 没有那个文件或目录
/:
bin
boot
dev
etc
追加方式输出重定向 >>
案例:
[root@localhost test]# echo hello >> a.txt
[root@localhost test]# echo hello >> a.txt
[root@localhost test]# echo hello >> a.txt
[root@localhost test]# cat a.txt
hello
hello
hello
hello
黑洞文件
/dev/null
输入重定向:
<
案例:
[root@localhost test]# passwd 表示从键盘获取
Changing password for user root.
New UNIX password:
BAD PASSWORD: it is WAY too short
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
[root@localhost test]# vim a.txt
[root@localhost test]# passwd < a.txt 表示文件获取
Changing password for user root.
New UNIX password: BAD PASSWORD: it is too simplistic/systematic
Retype new UNIX password: passwd: all authentication tokens
updated successfully.
<< : 表示函数结束标记
案例
[root@localhost test]# cat << EEE
> this is test progarm
> hello
> jim
> EEE
this is test progarm
hello
jim
管道: |
前一个命令输出作为后一个命令输入
案例:
[root@localhost test]# cat passwd | grep root
root❌0:0:this is root username:/root:/bin/bash
operator❌11:0:operator:/root:/sbin/nologin
[root@localhost test]# cat passwd | grep root | sort
operator❌11:0:operator:/root:/sbin/nologin
root❌0:0:this is root username:/root:/bin/bash
练习:
统计/etc/passwd中有多少种shell
cat grep cut sort uniq wc |
cat passwd | cut -d: -f 7 | sort | uniq | grep -v ^$ | wc -l
^ : 表示文件开始
$ : 表示文件结束
================================================
vim : 编辑器 程序开发器
vim -v : 查看vim版本
三种模式:
一般模式:vim直接打开文件模式就是一般模式
编辑模式:在左下角有"插入"字样
末行模式:命令行模式 在左下角有":"字样模式
模式之间切换
一般模式 => 编辑模式 :i I a A o O s S
编辑模式 => 末行模式 :ESC键 => :wq
一般模式下操作
删除:
dd : 表示删除一行
ndd : 表示删除n行
dw : 表示删除一个单词 光标必须位于首字母
ndw : 表示删除多个单词
daw : 表示删除一个单词 光标在单词任何位置
d^ : 表示从光标位置删除到行首
d$ : 表示从光标位置删除到行尾
u : 表示撤销上次操作
dgg : 表示删除到文件开头,包括光标所在行
dG : 表示删除到文件结尾,包括光标所在行
复制:
yy : 表示复制一行
p : 表示粘贴
nyy : 表示复制多行
可视化操作:
可视化字符:v => y => p
可视化行:shift + v => y => p
可视化块:ctrl + v => y => p
移动
gg : 表示文件开头
ngg : 表示定位到n行
G : 表示文件结尾
h : 左移
j : 下移
k : 上移
l : 右移
H : 表示屏幕顶端
L : 表示屏幕底部
M : 表示屏幕中间
50% : 表示文件中键
^ : 表示行首(有效字符)
$ : 表示行尾
0 : 表示行首(第一列)
J : 合并行
粘贴:
d => p
编辑模式:
i:表示在光标前面插入
I:表示在光标所在行的行首插入
a:表示在光标后面的插入
A:表示在光标所在行的行尾插入
s:表示删除光标所在行的字符插入
S:表示删除光标所在行的整行的插入
o:表示光标所在行的下一行的插入
O:表示光标所在行的上一行的插入
末行模式:
:w 表示保存
:q 表示退出
:wq 保存并退出
:wq! 强制保存并退出
:w filename 表示另存为
:r filename 表示读文件内容到当前文件中
:r !cmd 表示可以执行命令
临时有效
:set nu 显示行号
:set nonu 取消行号
:set ic 表示不区分大小写
永久有效
vim ~/.vimrc
添加set nu
移动
左移:shift + <
右移:shift + >
分屏显示
垂直分割:vim -O filename1 filename2 ... filenamen
vsplit world.c
水平分割:vim -o filename1 filename2 ... filenamen
split aa.txt
切换:
ctrl + w 2次
退出所有:
:wqa
=======================================
查找:
:/key 找到后会高亮显示
n : 查找下一个
N : 查找上一个
:?key
n : 查找上一个
N : 查找下一个
替换:
format :
[rang]s/oldkey/newkey/option
案例
1,4s/root/admin
表示替换第一行到第四行中查找到关键字,且只替换没一行的第一个关键字
:1,$s/root/admin/g 表示替换全文
:%s/root/admin/g 表示替换全文
:%s/root/admin/gi 表示不区分大小写替换
option:
g : 表示替换所有
i : 表示不区分大小写