Linux文件管理命令
1. touch :创建普通文件
1.1 语法结构
touch 【参数选项】 【文件名】
touch [文件] --->>相对路径创建文件
touch [/目录/文件] --->>绝对路径创建文件
touch [文件1] [文件2] [文件3] --->>连续创建多个文件
1.2 参数选项
参数选项 |
注释说明 |
参数选项 |
注释说明 |
-m |
设置文件的修改时间记录 |
-d |
设置时间与日期 |
1.3 案例实践
#案例1:在当前目录创建yyds.txt
[root@yyds ~]# touch yyds.txt
[root@yyds ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg yyds.txt
#案例2:/tmp目录下创建test.txt
#方法一:使用绝对路径创建
[root@yyds ~]# touch /tmp/test.txt
[root@yyds ~]# ls /tmp/
yyds01.txt
test.txt
#方法二:使用相对路径创建
[root@yyds ~]# cd /tmp/
[root@yyds tmp]# touch test.txt
[root@yyds tmp]# ls
test.txt
#案例3:在当前的yyds目录中创建1.txt
[root@yyds ~]# touch yyds/1.txt
[root@yyds ~]# ls yyds
1.txt
#案例4:1次在当前的目录创建多个文件 1.txt、2.txt、3.txt
#方法一:
[root@yyds ~]# touch 1.txt 2.txt 3.txt
[root@yyds ~]# ls
1.txt 3.txt 2.txt
initial-setup-ks.cfg yyds.txt anaconda-ks.cfg yyds
#方法二:
[root@yyds ~]# touch {1..3}.txt
[root@yyds ~]# ls
1.txt 2.txt 3.txt anaconda-ks.cfg cfg.txt initial-setup-ks.cfg www
[root@yyds ~]# rm -rf {1..3}.txt
[root@yyds ~]# ls
anaconda-ks.cfg cfg.txt initial-setup-ks.cfg www
#案例5.在不同的目录创建多个文件/tmp/1.txt /opt/2.txt当前创建a.txt
[root@yyds ~]# touch /tmp/1.txt /opt/2.txt a.txt
#查看方法一:
[root@yyds ~]# ls -l /tmp/1.txt /opt/2.txt a.txt
-rw-r--r-- 1 root root 0 7月 1 16:21 a.txt
-rw-r--r-- 1 root root 0 7月 1 16:21 /opt/2.txt
-rw-r--r-- 1 root root 0 7月 1 16:21 /tmp/1.txt
#查看方法二:
[root@yyds ~]# ls /tmp/
1.txt test.txt
[root@yyds ~]# ls /opt/
2.txt kylin-sm-package patch_workspace
[root@yyds ~]# ls
1.txt 3.txt a.txt yyds
2.txt anaconda-ks.cfg initial-setup-ks.cfg yyds.txt
2. cat :查看文件内容
2.1 语法结构
cat 【参数选项】 【文件】
2.2 参数选项
参数选项 |
注释说明 |
参数选项 |
注释说明 |
-n |
显示行数(空行也编号)❤️ |
-b |
显示行数(空行不编号) |
-A |
显示文件内容结尾符❤️ |
-E |
显示文件内容结尾符 |
2.3 案例实践
#案例1:查看/etc/hosts的内容
[root@yyds ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
#案例2:查看网卡配置文件
[root@yyds ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=6a0c5e65-5aa1-41ce-abd7-8a3e0cb58401
DEVICE=ens33
ONBOOT=yes
IPADDR=10.0.0.200
PREFIX=24
GATEWAY=10.0.0.2
DNS1=223.5.5.5
IPV6_PRIVACY=no
3. tail:查看文件尾部内容
3.1 语法结构
tail [参数] [文件]
3.2 参数选项
参数选项 |
注释说明 |
参数选项 |
注释说明 |
-f |
持续显示文件尾部最新内容❤️ |
-n |
显示文件尾部的n行 |
-F |
实时监控文件的变化,如果文件没有会等待文件的出现 |
3.3 案例实践
#案例1.显示/etc/passwd的最后10行内容
[root@Linux ~]# tail -10 /etc/passwd
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
cockpit-ws:x:993:989:User for cockpit-ws:/nonexisting:/sbin/nologin
chrony:x:992:988::/var/lib/chrony:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
dhcpd:x:177:177:DHCP server:/:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
pesign:x:991:986:Group for the pesign signing daemon:/var/run/pesign:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
xiaozhou:x:1000:1000::/home/xiaozhou:/bin/bash
[root@Linux ~]#
#案例2.持续显示/var/log/secure
[root@Linux ~]# tail -f /var/log/secure
Jul 5 00:51:51 Linux polkitd[808]: Loading rules from directory /etc/polkit-1/rules.d
Jul 5 00:51:51 Linux polkitd[808]: Loading rules from directory /usr/share/polkit-1/rules.d
Jul 5 00:51:51 Linux polkitd[808]: Finished loading, compiling and executing 5 rules
Jul 5 00:51:51 Linux polkitd[808]: Acquired the name org.freedesktop.PolicyKit1 on the system bus
Jul 5 00:53:08 Linux systemd[1555]: pam_unix(systemd-user:session): session opened for user root(uid=0) by (uid=0)
Jul 5 00:53:08 Linux sshd[1545]: pam_unix(sshd:session): session opened for user root(uid=0) by (uid=0)
Jul 5 01:15:22 Linux sshd[1751]: pam_unix(sshd:session): session opened for user root(uid=0) by (uid=0)
Jul 5 02:48:56 Linux sshd[2137]: pam_unix(sshd:session): session opened for user root(uid=0) by (uid=0)
Jul 5 04:59:53 Linux sshd[1545]: pam_unix(sshd:session): session closed for user root
Jul 5 05:04:15 Linux sshd[1751]: pam_unix(sshd:session): session closed for user root
补充:
tailf:实时监控文件的变化,等同于tail -f,麒麟和Ubuntu系统默认取消了这个命令,可以安装
4. head :查看文件开头的内容
4.1 语法结构
head [参数] [文件]
4.2 参数选项
参数选项 |
注释说明 |
参数选项 |
注释说明 |
-n |
显示文件头部的n行 |
4.3 案例实践
#案例1.查看/etc/passwd 的前5行内容
[root@Linux ~]# head -5 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
#案例2.查看/etc/passwd的10~15行
[root@Linux ~]# head -15 /etc/passwd |tail -6
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:65534:65534:Kernel Overflow User:/:/sbin/nologin
systemd-coredump:x:999:997:systemd Core Dumper:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
管道:"|" 其他命令的输出,作为后续命令的输入
5. grep :模糊过滤文件中的字符串
5.1 语法结构
grep '字符串' [文件]
5.2 参数选项
参数选项 |
注释说明 |
参数选项 |
注释说明 |
-w |
精准匹配整词 |
-n |
显示匹配内容的行号 |
-v |
取反❤️ |
-r |
递归过滤文件内容❤️ |
-E |
扩展正则❤️ |
-o |
匹配过程 |
-i |
忽略大小写 |
-A |
过滤内容,往下显示n行 |
-B |
过滤内容,往上显示n行 |
-C |
过滤内容,上下各显示n行 |
5.3 案例实践
#案例1.过滤/var/log/secure中的fialure
[root@Linux ~]# grep 'fialure' /var/log/secure
#案例2.过滤ip a中包含10的行
[root@Linux ~]# ip a |grep '10\.'
inet 10.0.0.200/24 brd 10.0.0.255 scope global noprefixroute ens33
6. wc:统计文件的字节数、单词数、行数
6.1 语法结构
wc [参数] [文件]
6.2 参数选项
参数选项 |
注释说明 |
参数选项 |
注释说明 |
-c |
统计字节数 |
-l |
统计行数 |
-w |
统计单词数 |
-L |
统计最长行的字符数 |
6.3 案例实践
[root@Linux ~]# wc -l /etc/passwd
35 /etc/passwd
[root@Linux ~]# wc -w /etc/passwd
74 /etc/passwd
[root@Linux ~]# wc -c /etc/passwd
1856 /etc/passwd
[root@Linux ~]# wc /etc/passwd
35 74 1856 /etc/passwd
7. echo:输出字符串到屏幕或文件(支持序列)
7.1 语法结构
echo [参数] [字符串或$环境变量]
echo 字符串 #输出字符串到屏幕
echo 字符串 >文件 #输出字符串到文件
其他命令 >文件 #输出到文件中
7.2 案例实践
#案例1.echo 输出字符串qinghua
[root@Linux ~]# echo qinghua
qinghua
#案例2.echo 输出多个字符串qinghua test xiaozhou
[root@Linux ~]# echo qinghua test xiaozhou
qinghua test xiaozhou
#案例3.echo 输出数字
[root@Linux ~]# echo 1 2 3
1 2 3
#案例4.输出字符加数字
[root@Linux ~]# echo yyds_10.0.0.200
yyds_10.0.0.200
#案例5.输出特殊符号
[root@Linux ~]# echo % $ /
% $ /
#案例6.输出序列 数字序列
[root@Linux ~]# echo {1..5}
1 2 3 4 5
#案例7.输出序列 字母序列
[root@Linux ~]# echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z
#案例8.通过序列创建目录
[root@Linux ~]# mkdir {1..3}
[root@Linux ~]# ll
total 0
drwxr-xr-x 2 root root 6 Jul 5 08:09 1
drwxr-xr-x 2 root root 6 Jul 5 08:09 2
drwxr-xr-x 2 root root 6 Jul 5 08:09 3
#案例9.复制当前的1 2 3目录到/tmp目录
[root@Linux ~]# cp -r {1..3} /tmp
[root@Linux ~]# ll /tmp
total 0
drwxr-xr-x 2 root root 40 Jul 5 08:10 1
drwxr-xr-x 2 root root 40 Jul 5 08:10 2
drwxr-xr-x 2 root root 40 Jul 5 08:10 3
#案例10.批量创建文件
[root@Linux ~]# touch {a..c}.txt
[root@Linux ~]# ll
-rw-r--r-- 1 root root 0 Jul 5 08:11 a.txt
-rw-r--r-- 1 root root 0 Jul 5 08:11 b.txt
-rw-r--r-- 1 root root 0 Jul 5 08:11 c.txt
#案例11.字符串 数字和序列的组合
[root@Linux ~]# echo yyds{1..3}
yyds1 yyds2 yyds3
[root@Linux ~]# touch yyds{1..3}
[root@Linux ~]# ll
-rw-r--r-- 1 root root 0 Jul 5 08:12 yyds1
-rw-r--r-- 1 root root 0 Jul 5 08:12 yyds2
-rw-r--r-- 1 root root 0 Jul 5 08:12 yyds3
#案例12.使用序列删除文件
[root@Linux ~]# rm -f yyds{1..3}
[root@Linux ~]# ll
#案例13.序列前后拼接
[root@Linux ~]# touch yyds{1..3}.txt
[root@Linux ~]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 5 08:16 yyds1.txt
-rw-r--r-- 1 root root 0 Jul 5 08:16 yyds2.txt
-rw-r--r-- 1 root root 0 Jul 5 08:16 yyds3.txt
#案例14.拼接不连续的字符串
[root@Linux ~]# touch {a,c,e}.txt
[root@Linux ~]# ll
total 0
-rw-r--r-- 1 root root 0 Jul 5 08:15 a.txt
-rw-r--r-- 1 root root 0 Jul 5 08:15 c.txt
-rw-r--r-- 1 root root 0 Jul 5 08:15 e.txt
#案例15.拼接目录
[root@Linux ~]# mkdir -p yyds/{a,c}
[root@Linux ~]# tree
.
└── yyds
├── a
└── c
8. less:分页显示文件内容
8.1 语法结构
less 【参数】 【文件】
8.2 参数选项
参数选项 |
注释说明 |
参数选项 |
注释说明 |
-N |
显示行号❤️ |
i |
搜索时忽略大小写 |
-m |
显示阅读进度百分比 |
-y |
设置向前滚动的最大行数 |
快捷键:
快捷键 |
含义 |
快捷键 |
含义 |
f / 空格 |
往下翻页 |
b |
往上翻页 |
1G |
快速到文本的首行 |
g |
快速到文本的首行 |
G |
快速到文本的最后一行 |
10g |
快速到文本的第10行 |
/ |
搜索内容 |
q |
退出 |
9. more:分页显示文本文件内容
9.1 语法结构
more 【参数】 【文件】
9.2 参数选项
参数选项 |
注释说明 |
参数选项 |
注释说明 |
-数字 |
设置每屏显示最大数字 |
+数字 |
设置从指定行开始显示内容 |
10. sort:对文件内容进行排序
10.1 语法结构
sort 【参数】 【文件名】
10.2 参数选项
参数选项 |
注释说明 |
参数选项 |
注释说明 |
-n |
依据数值从小到大排序❤️ |
-r |
以相反顺序排序❤️ |
-k |
指定排序的列❤️ |
-t |
指定排序时所用的栏位分隔符❤️ |
10.3 案例实践一
#环境准备
[root@linux ~]# cat a.txt
123
222
1
34
456
5
6
10
9
7
8
12
100
24
#案例1:对数字进行排序
[root@linux ~]# sort a.txt ----->>默认按照第一列数字排序
1
10
100
12
123
222
24
34
456
5
6
7
8
9
#案例2:按数值正序进行排序
[root@linux ~]# sort -n a.txt
1
5
6
7
8
9
10
12
24
34
100
123
222
456
#案例3:按数值逆序进行排序
[root@linux ~]# sort -rn a.txt
456
222
123
100
34
24
12
10
9
8
7
6
5
1
10.4 实践案例二
#环境准备
[root@linux ~]# cat b.txt
age
b23
chd
jkl
r
e
z
haj
kla
dg
#案例1.对b.txt进行排序
[root@linux ~]# sort b.txt ----->>默认按照第一列数字排序
age
b23
chd
dg
e
haj
jkl
kla
r
z
10.5 案例实践三
#环境准备
[root@linux ~]# cat c.txt
aaa 1
aaa 5
aaa 8
aaa 9
aaa 10
aaa 34
aaa 123
aaa 222
aaa 456
aaa 321
aaa 678
aaa 35
aaa 878
#案例1.对文本第二列进行排序
[root@linux ~]# sort -nk2 c.txt
aaa 1
aaa 5
aaa 8
aaa 9
aaa 10
aaa 34
aaa 35
aaa 123
aaa 222
aaa 321
aaa 456
aaa 678
aaa 878
11. uniq:去除文件中重复内容的行
11.1 语法结构
uniq 【参数】【文件名】
11.2 参数选项
参数选项 |
注释说明 |
参数选项 |
注释说明 |
-c |
显示每行在文本中出现的次数❤️ |
-d |
设置每个重复记录只出现一次 |
11.3 案例实践
#环境准备:
[root@linux ~]# cat d.txt
test
yyds
mysql
shell
docker
shell
mysql
test
test
#案例1.统计单词出现的个数
[root@linux ~]# cat d.txt | sort | uniq -c
1 docker
2 mysql
1 yyds
2 shell
3 test
#案例2.统计ip地址文本中IP地址出现次数最多的前10行
[root@linux ~]# cat ip.txt | sort | uniq -c |sort -rn | head -10
189 157.66.48.147
76 219.82.2.110
53 123.112.20.52
53 117.132.188.205
44 143.198.0.139
36 180.98.131.167
36 113.215.189.214
36 113.215.188.106
21 113.215.188.107
19 113.215.188.108
12. xargs:将前一个命令的输出作为参数发送给其他命令
12.1 语法结构
xargs 【参数】 【文件名】
12.2 参数选项
参数选项 |
注释说明 |
参数选项 |
注释说明 |
-n |
指定标准输出的列数❤️ |
-d |
指定分隔符❤️ |
-i |
指定标准输出的值❤️ |
-E |
指定结束符 |
-t |
先打印命令然后执行操作 |
12.3 案例实践
#案例1.指定输出两列
[root@linux:~]# cat aa.txt
1a 2b 3c 4d 5e 6f
[root@linux:~]# cat aa.txt |xargs -n 2
1a 2b
3c 4d
5e 6f
#案例2.指定分隔符为,并成3列输出
[root@linux:~]# cat aa.txt
1a,2b,3c,4d,5e,6f
[root@linux:~]# cat aa.txt |xargs -d ","
1a 2b 3c 4d 5e 6f
[root@linux:~]# cat aa.txt |xargs -d "," -n 3
1a 2b 3c
4d 5e 6f
#案例2.将find结果交给cp命令
[root@linux:~]# find ./ -name "b.txt" |xargs -i cp {} /tmp
[root@linux:~]# ll /tmp/
total 0
-rw-r--r-- 1 root root 0 Jul 10 00:29 b.txt
13. tr:字符转化工具
13.1 语法结构
commands |tr [字符串1] [字符串2]
tr [字符串1] [字符串2] <filename
tr [参数] [字符串] <filename
13.2 参数选项
参数选项 |
注释说明 |
参数选项 |
注释说明 |
-d |
删除字符串中出现的所有字符 |
-s |
删除所有重复出现的字符序列只保留第一个 |
13.3 案例实践
案例1.查看/etc/passwd中每个单词出现的次数
[root@linux:~]# cat /etc/passwd |tr "[0-9:/x]" " "|xargs -n1|sort|uniq -c|sort -rn
14. stat:显示文件状态信息
14.1 语法结构
stat [参数] [文件名]
14.2 参数选项
参数选项 |
注释说明 |
参数选项 |
注释说明 |
-f |
显示文件系统信息 |
-L |
支持符号链接 |
14.3 案例实践
[root@nfs:~]#stat test.txt
File: ‘test.txt’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 101039759 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2024-08-03 20:49:10.916832137 +0800
Modify: 2024-08-03 20:49:10.916832137 +0800
Change: 2024-08-03 20:49:10.916832137 +0800
Birth: -
[root@nfs:~]#stat -f test.txt
File: "test.txt"
ID: fd0000000000 Namelen: 255 Type: xfs
Block size: 4096 Fundamental block size: 4096
Blocks: Total: 10028253 Free: 8886630 Available: 8886630
Inodes: Total: 20066304 Free: 19902269
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~