13、linux文件及目录管理

一、linux目录和文件管理

在这里插入图片描述
在这里插入图片描述
目录结构和每个目录的作用。

/

1、root:是管理员的家目录,只有管理员的家目录在根目录,其他普通用户的家目录在home下 ssh。

2、home:是普通用户的家目录。因为普通用户的权限不够,一般无法直接读写,所以特意创建了一个家目录,方便普通用户读写操作。

3、/boot:系统文件,相当于C盘的windows,不要去动。

4、nginx.conf cat vim

5、/dev:硬件设备。linux 一切皆设备,硬件设备。

7、/etc:存放系统默认程序以及一些第三方安装程序的配置文件。我们会进入目录修改一些配置文件,只改和我们相关的,不相干的我们不要动。

8、/bin:都是保存系统命令的文件目录  所有用户都可以执行的命令。

9、/sbin:                                                    只有管理员可以执行的命令。

10、/var: 存放系统的可变化文件,存放各种日志文件,主要是给我们看日志用的。/log

11、/opt:存放第三方应用程序或者是用户自定义的脚本文件等等。和系统没有任何关系的文件。

12、/lib:存放系统程序的动态连接共享库文件,类似于windows.dll文件。

13、/usr:存放系统用户的工具和第三方安装的程序。rpm yum 编译安装。

14、/media:媒体文件

15、/proc:存放映射系统信息的文件。

16、/mnt :挂载存储设备。

17、/tmp:存放系统的临时文件。

****我们一般操作的几个目录:etc root home opt var/log。****

二、文件管理

在这里插入图片描述

2.1、cat命令

cat:查看文件内容,全量查看文件内容。一般文件可以使用,如果文件内容太大,全量打开速度会很慢。

**-n**对所有行输出行的编号,行号。

**-b**空行不算在行号内。对空白行不编号。

**-s**把多个连续空行,替换成一个空行。

cat只能查,不能写。全量展示。

操作:

cat -n 456
     1	d
     2	ds
     3	dsd
     4	
     5	sdsds
     6	
     7	sdsds
     8	
     9	dsdssdsdf
cat -b 456
     1	d
     2	ds
     3	dsd

     4	sdsds
    
     5	sdsds
    
     6	dsdssdsdf
cat -s 456
d
ds
dsd

sdsds

sdsds

dsdssdsdf
cat -nbs 456
     1	d
     2	ds
     3	dsd

     4	sdsds
    
     5	sdsds
    
     6	dsdssdsdf

2.2、分页查看

**2.2.1、more:**分页查看 more /etc/passwd

阅读命令:b上一行,enter下一行,空格自动翻页到行尾,q 退出,或者阅读完毕自动退出。

**2.2.2、less:**分页查看less /etc/passwd

使用上下键翻页,page up page down

可以搜索 / :查询功能,N(上翻)和n(下翻)可以对查询内容进行上下翻。

退出q

2.3、tail行数查看

**2.3.1、tail命令:**从尾部查看文件的一定内容。行数限制。

动态查看。有变化会立即显示在屏幕上。

-n 可以指定从下往上数n行,默认10行

tail -n 20 /etc/passwd

-f 动态查看 默认跟踪最后10条

tail -10f 文件

第一次看的时候,只看最后的10行,如果内容发送变化,超过10行

全量展示。

tail -f 动态查看,这个是重点,必须要会

[root@localhost opt]# tail -f /var/log/messages
May 20 22:50:01 localhost systemd: Started Session 244 of user root.
May 20 23:00:01 localhost systemd: Started Session 245 of user root.
May 20 23:01:01 localhost systemd: Started Session 246 of user root.
May 20 23:10:01 localhost systemd: Started Session 247 of user root.
May 20 23:20:02 localhost systemd: Started Session 248 of user root.
May 20 23:28:16 localhost systemd-logind: New session 249 of user root.
May 20 23:28:16 localhost systemd: Started Session 249 of user root.
May 20 23:28:16 localhost dbus[8762]: [system] Activating service name='org.freedesktop.problems' (using servicehelper)
May 20 23:28:16 localhost dbus[8762]: [system] Successfully activated service 'org.freedesktop.problems'
May 20 23:30:01 localhost systemd: Started Session 250 of user root.

[root@localhost opt]# systemctl restart firewalld //**tset2:输入代码动态刷新

May 20 23:37:17 localhost systemd: [/usr/lib/systemd/system/firstboot-graphical.service:14] Support for option SysVStartPriority= has been removed and it is ignored
May 20 23:37:20 localhost systemd: [/usr/lib/systemd/system/firstboot-graphical.service:14] Support for option SysVStartPriority= has been removed and it is ignored
May 20 23:37:22 localhost systemd: [/usr/lib/systemd/system/firstboot-graphical.service:14] Support for option SysVStartPriority= has been removed and it is ignored
May 20 23:37:24 localhost systemd: [/usr/lib/systemd/system/firstboot-graphical.service:14] Support for option SysVStartPriority= has been removed and it is ignored
May 20 23:37:29 localhost systemd: [/usr/lib/systemd/system/firstboot-graphical.service:14] Support for option SysVStartPriority= has been removed and it is ignored
May 20 23:37:31 localhost systemd: [/usr/lib/systemd/system/firstboot-graphical.service:14] Support for option SysVStartPriority= has been removed and it is ignored
May 20 23:37:51 localhost systemd: Stopping firewalld - dynamic firewall daemon...
May 20 23:37:52 localhost kernel: Ebtables v2.0 unregistered
May 20 23:37:52 localhost libvirtd: 2024-05-20 15:37:52.408+0000: 9191: error : virFirewallApplyRuleFirewallD:790 : The name org.fedoraproject.FirewallD1 was not provided by any .service files
May 20 23:37:52 localhost systemd: Stopped firewalld - dynamic firewall daemon.
May 20 23:37:52 localhost systemd: Starting firewalld - dynamic firewall daemon...
May 20 23:37:52 localhost systemd: Started firewalld - dynamic firewall daemon.
May 20 23:37:53 localhost kernel: ip_tables: (C) 2000-2006 Netfilter Core Team
May 20 23:37:53 localhost kernel: ip6_tables: (C) 2000-2006 Netfilter Core Team
May 20 23:37:53 localhost kernel: Ebtables v2.0 registered
May 20 23:37:53 localhost kernel: nf_conntrack version 0.5.0 (16384 buckets, 65536 max)
May 20 23:37:54 localhost firewalld[108818]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -w --table mangle --delete POSTROUTING --out-interface virbr0 --protocol udp --destination-port 68 --jump CHECKSUM --checksum-fill' failed: iptables: No chain/target/match by that name.
May 20 23:37:54 localhost firewalld[108818]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -w --table nat --delete POSTROUTING --source 192.168.122.0/24 --destination 224.0.0.0/24 --jump RETURN' failed: iptables: Bad rule (does a matching rule exist in that chain?).
May 20 23:37:54 localhost firewalld[108818]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -w --table nat --delete POSTROUTING --source 192.168.122.0/24 --destination 255.255.255.255/32 --jump RETURN' failed: iptables: Bad rule (does a matching rule exist in that chain?).
May 20 23:37:54 localhost firewalld[108818]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -w --table nat --delete POSTROUTING --source 192.168.122.0/24 -p tcp ! --destination 192.168.122.0/24 --jump MASQUERADE --to-ports 1024-65535' failed: iptables: No chain/target/match by that name.
May 20 23:37:54 localhost firewalld[108818]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -w --table nat --delete POSTROUTING --source 192.168.122.0/24 -p udp ! --destination 192.168.122.0/24 --jump MASQUERADE --to-ports 1024-65535' failed: iptables: No chain/target/match by that name.
May 20 23:37:54 localhost firewalld[108818]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -w --table nat --delete POSTROUTING --source 192.168.122.0/24 ! --destination 192.168.122.0/24 --jump MASQUERADE' failed: iptables: No chain/target/match by that name.
May 20 23:37:54 localhost firewalld[108818]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -w --table filter --delete FORWARD --destination 192.168.122.0/24 --out-interface virbr0 --match conntrack --ctstate ESTABLISHED,RELATED --jump ACCEPT' failed: iptables: Bad rule (does a matching rule exist in that chain?).
May 20 23:37:54 localhost firewalld[108818]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -w --table filter --delete FORWARD --source 192.168.122.0/24 --in-interface virbr0 --jump ACCEPT' failed: iptables: Bad rule (does a matching rule exist in that chain?).
May 20 23:37:54 localhost firewalld[108818]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -w --table filter --delete FORWARD --in-interface virbr0 --out-interface virbr0 --jump ACCEPT' failed: iptables: Bad rule (does a matching rule exist in that chain?).
May 20 23:37:54 localhost firewalld[108818]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -w --table filter --delete FORWARD --out-interface virbr0 --jump REJECT' failed: iptables: No chain/target/match by that name.
May 20 23:37:54 localhost firewalld[108818]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -w --table filter --delete FORWARD --in-interface virbr0 --jump REJECT' failed: iptables: No chain/target/match by that name.
May 20 23:37:54 localhost firewalld[108818]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -w --table filter --delete INPUT --in-interface virbr0 --protocol udp --destination-port 53 --jump ACCEPT' failed: iptables: Bad rule (does a matching rule exist in that chain?).
May 20 23:37:54 localhost firewalld[108818]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -w --table filter --delete INPUT --in-interface virbr0 --protocol tcp --destination-port 53 --jump ACCEPT' failed: iptables: Bad rule (does a matching rule exist in that chain?).
May 20 23:37:54 localhost firewalld[108818]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -w --table filter --delete OUTPUT --out-interface virbr0 --protocol udp --destination-port 68 --jump ACCEPT' failed: iptables: Bad rule (does a matching rule exist in that chain?).
May 20 23:37:54 localhost firewalld[108818]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -w --table filter --delete INPUT --in-interface virbr0 --protocol udp --destination-port 67 --jump ACCEPT' failed: iptables: Bad rule (does a matching rule exist in that chain?).
May 20 23:37:54 localhost firewalld[108818]: WARNING: COMMAND_FAILED: '/usr/sbin/iptables -w2 -w --table filter --delete INPUT --in-interface virbr0 --protocol tcp --destination-port 67 --jump ACCEPT' failed: iptables: Bad rule (does a matching rule exist in that chain?).

关闭防火墙[root@localhost opt]# systemctl stop firewalld

May 20 23:40:01 localhost systemd: Started Session 251 of user root.
May 20 23:41:24 localhost systemd: Stopping firewalld - dynamic firewall daemon...
May 20 23:41:25 localhost kernel: Ebtables v2.0 unregistered
May 20 23:41:25 localhost libvirtd: 2024-05-20 15:41:25.619+0000: 9191: error : virFirewallApplyRuleFirewallD:790 : The name org.fedoraproject.FirewallD1 was not provided by any .service files
May 20 23:41:25 localhost systemd: Stopped firewalld - dynamic firewall daemon.

2.4、wc:word count 文件统计

统计文件的单词数量以及其他信息。

wc -l /etc/passwd
45 /etc/passwd
wc -w /etc/passwd
91 /etc/passwd
 wc -c /etc/passwd
2333 /etc/passwd
wc -lwc /etc/passwd
  45   91 2333 /etc/passwd
```	-l 统计文件当中的行数。

	-w 统计文件内容中单词的个数。
	
	-c 统计文件内容的字节数,统计文件内容的大小。

head:从头看往后看

head -n 10 文件(使用-n选项,一定要跟数据,否则报错)

2.5、grep:查找文件内容,过滤文本内容。grep是linux的文本三剑客之一。

2.5.1、grep命令

-n:显示匹配内容,还可以显示匹配内容的行号。

-e:实现多个查找条件的匹配。相当于or。

-v:取反,就是显示不匹配内容的行。

-c:显示匹配到所在内容的总行数,不是匹配字符的次数。
grep -v sbin /etc/passwd
root:x:0:0:root:/root:/bin/bash
li:x:1000:1000:li:/home/li:/bin/bash
dn:x:1001:1001::/home/dn:/bin/bash
grep -e root -e dn /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
dn:x:1001:1001::/home/dn:/bin/bash
grep -n root /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
grep -c root -e -c dn /etc/passwd
grep: root: 没有那个文件或目录
grep: dn: 没有那个文件或目录
/etc/passwd:0
grep -c dn /etc/passwd
1
以上都是查看文件内容,或者是对文件内容进行条件过滤。
grep -v sbin /etc/passwd
root:x:0:0:root:/root:/bin/bash
li:x:1000:1000:li:/home/li:/bin/bash
dn:x:1001:1001::/home/dn:/bin/bash
grep -e root -e dn /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
dn:x:1001:1001::/home/dn:/bin/bash
grep -n root /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
grep -c root -e -c dn /etc/passwd
grep: root: 没有那个文件或目录
grep: dn: 没有那个文件或目录
/etc/passwd:0
grep -c dn /etc/passwd
1
grep -n -e、sed -e -n没有固定位置

2.5.2、| 管道符

在一个命令行只能有一个命令,通过管道符|,可以把多个命令连接起来。处理文本内容的命令,都可以使用管道符使用。

特点:把前一个命令的处理结果,作为|管道符后一个命令的参数。

管道符含义:cat /etc/passwd | grep -n root=grep -n root /etc/passwd** 

命令打印出来的结果,也是文本内容。

三、打包和解包,以及vim的用法。

3.1压缩包格式及特点

.zip  .rar

.gz  .bz2

bz2:压缩的文件比较小,压缩效率高,但是速度比较慢。

gz:压缩速度比较快,压缩的效率比较低。

3.1.1tar:就是压缩和解压缩。

-z 调用gzip gz的方式进行压缩或解压

-j 调用bzip bz2的方式进行压缩或者解压

-c 创建.tar格式的包文件 打包 压缩

-x  解开。tar格式的包文件,解包,解压缩。

-v 输出详细的信息

-f 使用归档文件(这是必加的选项,不论是打包还是解包)

-C 解压时,指定解压文件释放的路径,绝对路径。

在这里插入图片描述

touch 123 456
tar -cf xy102.tar.gz 123 456
ll
总用量 12
-rw-r--r--. 1 root root     0 520 18:23 123
-rw-r--r--. 1 root root     0 520 18:23 456
-rw-r--r--. 1 root root 10240 520 18:25 xy102.tar.gz
tar -xf xy102.tar.gz -C /opt/xy103/
tar: /opt/xy103:无法 open: 没有那个文件或目录
tar: Error is not recoverable: exiting now
mkdir xy103
tar -xf xy102.tar.gz -C /opt/xy103/
cd xy103
ll
总用量 0
-rw-r--r--. 1 root root 0 520 18:23 123
-rw-r--r--. 1 root root 0 520 18:23 456

最简格式

 tar -cf xy102.tar.gz 123 456            **123 456文件压缩至xy102.tar.gz中,另外后缀也可以是bz2**

 tar -xf xy102.tar.gz -C /opt/xy103/   **bz2或者gz文件解压当前目录下xy102至/opt/xy103**

tar 只能针对 .gz .bz2文件

**zip**

unzip:解压命令----unzip xxxxxxx.zip -d /opt/test1/将xxxxxxxx.zip解压到/opt/test/

unzip 第2章_源码编译构建LAMP.zip -d /opt/test1/

**rar**

unrar:解压命令unzip x ******.zip /opt/test1/将**.zip解压到/opt/test1/

unrar x test1.rar /opt/test1

四、vim 文本编辑器

vim编辑器

vi 老版本

vim vi的升级版

使用vim打开文本,第一个界面,命令模式,又叫视图模式,只能看,不能改。

4.1、编辑模式:i进入。

**a**在当前光标位置之后插入内容;

**A**光标移动到所在行的行尾,开始插入内容;

**I**在当前行的行首开始插入内容。

**o**在当前行的下一行开始插入内容;

**O**在当前行的上一行开始插入内容。			

4.2、底行模式:shift+:进入底行模式。set nu 编号,显示行号。

set nonu 取消行号。

**:w**** 保存   

**:wq** 保存退出  

**:wq!**强制保存退出(一定能保存,一定能退出)

**:q** 不保存退出

 **:q!**强制不保存退出(一定不保存,一定退的出)

在这里插入图片描述

命令模式:行间快速跳转        **:set nu**(显示行编号)

**1G 1gg**直接跳转到指定行。例如6gg/6G

**G**快速跳转到最后一行。

**x** 删除光标处的单个字符

**dd** 删除光标行

**8dd** 从光标行删除8行(包含光标行)

**d^** 删除该行光标处前面所有(不包含光标)

**d$** 删除该行光标处后面所有(包含光标)

**3yy** 从光标处复制3行

**p** 将复制3行从此处向下粘贴

**P** 将复制3行从此处向上粘贴

**u** 撤销到上一步

 **:s /old/new**   光标处单个替换,一定把光标移动到指定内容上,单个替换,不是整行。

 **:s /old/new/g** 整行替换

 **:#,# 在#到#行,s /old/new/g** 范围内替换

**:%,整个文件s /old/new/g** 范围内替换


vi 文件名
vim 文件名
!vim	//返回上一次编辑的文件

命令模式切换至输入模式:
a:在当前光标位置之后插入内容
i:在当前光标位置之前插入内容
o:在光标所在行之下插入一个新行内容
O:在光标所在行之上插入一个新行内容
A:在所在行的行尾插入内容
I:在所在行的行首插入内容

命令模式:
操作类型		操作键						功能
翻页移动		Page Down键或 Ctrl+F		向下翻动一整页内容
			Page Up键或 Ctrl+B			向上翻动一整页内容
行内快速跳转	Home键或^键、数字 0键			跳转到本行的行首
			End 键或$键					跳转到本行的行尾
行间快速跳转	1G 或 gg					转到文件内容的第 1 行
			G							跳转到文件的最后一行
			#G							跳转到文件中的第#行(其中“#”号用具体数字替换)
			M 					        跳转至当前页的中间位置
            H                           当前页面的第一行
			L				            当前页面的最后一行
底行模式:
显示行号		:set nu						在编辑器中显示行号
			:set nonu					取消行号显示
删除			x或Delete键					删除光标处的单个字符
			dd							删除当前光标所在行(有剪切功能)
			#dd							删除从光标处开始的#行内容  -----#表示几行
			d^							删除当前光标之前到行首的所有字符
			d$							删除当前光标处到行尾的所有字符
			dw							删除光标处的整个单词
替换字符		R 或 Shift+r				替换当前光标处字符
复制			yy							复制当前行整行的内容到剪贴板
			#yy							复制从光标处开始的#行内容  ----#表示几行
粘贴			p							粘贴到光标所在行之下
			P							粘贴到光标所在行之上
查找			/word						从当前光标处开始向后进行查找字符串“word”
			?word						从当前光标处开始向前进行查找
			n							定位下一个匹配的被查找字符串
			N							定位上一个匹配的被查找字符串
撤销			u							按一次取消最近的一次操作;重复u键,恢复多步操作
			
保存退出		:w							保存修改的内容
			:w 新文件名					另存为其它文件
			:q
			:q!							放弃对文件内容的修改并退出
			ZZ或:wq、:x					保存当前的文件内容并退出vi编辑器
打开新文件		:e 其它文件名				打开新的文件进行编辑
读入文件内容	:r 其它文件名					在当前文件中读入其他文件内容
文件内容替换	:s /old/new					将当前行中查找到的第一个字符串“old” 串替换为“new”
			:s /old/new/g				将当前行中查找到的所有字符串“old” 替换为“new”
			:#,# s/old/new/g 			在行号“#,#”范围内替换所有的字符串“old”为“new”
			:% s/old/new/g 				在整个文件范围内替换所有的字符串“old”为“new”
			
			:1,5 m 10					将1-5行的内容剪切粘贴到第10行下面
			:1,5 co 10					将1-5行的内容复制粘贴到第10行下面
  • 8
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值