该文仅针对自身对Linux基础知识不足的地方进行补充扩展,便于巩固。
1、七大文件类型
- 普通文件
d 目录文件
l 链接文件(软链接)
p 管道文件
s 套接字文件(ip:端口号):服务端可以启动一个程序来监听客户端的请求,而客户端就可以通过socket来进行
数据通信。
b 块设备文件
c 字符设备文件
#注意:linux文件的扩展名不具有实际意义,唯一的作用是方便用户
2、创建文件/目录
命令 | 含义 |
touch | 创建文件/修改时间戳。 |
mkdir | 创建目录,-v 显示过程;-p 递归创建。 |
ln | 创建硬连接,-s 创建软连接。 |
硬链接和源文件共用一个节点号。
3、编辑文件
(1)tee
# 从标准输入读取数据,同时将数据写入标准输出和一个或多个文件。
[root@openEuler-4 mnt]# tee 1.txt
hello
hello
hahah
hahah
^C
[root@openEuler-4 mnt]# cat 1.txt
hello
hahah
# 编写文件内容
[root@openEuler-2 ~]# tee /usr/lib/systemd/system/docker.socket <<EOF
> [Unit]
> Description=Docker Socket for the API
>
> [Socket]
> ListenStream=/var/run/docker.sock
> SocketMode=0660
> SocketUser=root
> SocketGroup=docker
>
> [Install]
> WantedBy=sockets.target
> EOF
# 监控日志文件
[root@openEuler-2 ~]# tail -f /var/log/syslog | tee -a monitoring.log
# 常用参数 -a :以追加的形式写入文件。
(2)vim编辑器
注意:插入模式和末行模式之间不能切换,必须先esc进入命令模式。
命令模式:
快捷键 | 含义 |
yy | 复制当前行,nyy 多行复制。 |
p | 粘贴到下一行,P 粘贴至上一行。 |
dd | 剪切当前行,ndd 多行剪切。 |
x | 当前光标向右删除。 |
s | 删除当前光标并进入插入模式。 |
r | 替换当前字符。 |
cc | 剪切当前行并进入插入模式,ncc 多行剪切并进入插入模式。 |
u | 撤销上一次操作。 |
h、j、k、l | 分别代表光标左、下、上、右移动。 |
a、i、o | 分别移动光标至前、后、下一行进入编辑模式。 |
末行模式:
关键字 | 含义 |
/xx | 正向搜索字符串xx,n匹配下一个字段;N匹配上一个字段。 |
?xx | 反向搜索字符串xx,n匹配下一个字段;N匹配上一个字段。 |
:wq | w保存,q退出; ! 强制退出;x 在文件修改时才保存,更安全。 |
:w filename | 另存为 filename 文件。 |
:set nu | 显示行号,set nonu取消显示。 |
:r /dir1/dir2/filename | 表示读取指定文件的内容到当前文件. |
:n | 跳转到n行,:$跳转到最后一行。 |
:%s /old/new/g | 全局将 old 替换成 new 。 |
4、查看文件
(1)less、more、head、tail
命令 | 含义 |
less | 交互式查看大文件。↑/↓ 上/下翻页;/ ? 正/反向搜索。作用:不会加载整个文件,适合查看大文件。 |
more | 逐页查看,Space 下一页;Enter 下一行。 |
head | 查看文件头十行。-n 指定行数。 |
tail | 查看文件后十行。-n 指定行数‘;-f 持续查看实时刷新。 |
(2)cut、sort、uniq、tr
命令 | 含义 |
cut |
切割文本。-d 指定分隔符,-f 指定提取的列,-c指定提取的字符位置; |
sort | 对文本进行排序。-n 按数值排序;-r 逆序排序;-u 去重;-k 指定排序的列。 |
uniq | 去重。-c 统计重复次数;-d 仅显示重复行;-u 仅不显示重复行;-i 忽略大小写 |
tr | 字符替换。-d 删除指定字符;-s 压缩连续重复字符; |
组合使用示例:
# 提取日志的IP列,统计访问量TOP 10
cut -d' ' -f1 access.log | sort | uniq -c | sort -nr | head -10
# 清理CSV文件:删除空格并转大写
cat data.csv | tr -d ' ' | tr 'a-z' 'A-Z' > cleaned.csv
5、文件的压缩与解压缩
(1)zip、unzip
#zip和unzip (保留源文件)
# zip压缩到指定位置
[root@server100 ~]# zip /root/yasuo.zip /yasuo/dir1/*
# -m 添加文件至压缩包
# -d 从压缩包内删除文件
# unzip解压到当前目录
[root@server100 ~]# unzip /root/yasuo.zip
# 常用参数:
# -d 解压至指定目录
# -v 列出压缩包里的内容
(2)gzip和bunzip、bzip2和bunzip2、xz和unzx
#注意以上三对压缩工具不能对目录进行压缩,只能对普通文件进行压缩
[root@haha ~]# gzip /yasuo
gzip: /yasuo is a directory -- ignored
#1、默认不保留源文件 压缩到当前工作目录
[root@haha yasuo]# gzip hostname
[root@haha yasuo]# bzip2 hostname02
[root@haha yasuo]# xz passwd
#2、不保留源文件 解压缩到当前工作目录
[root@haha yasuo]# gunzip hostname.gz
[root@haha yasuo]# bunzip2 hostname02.bz2
[root@haha yasuo]# unxz passwd.xz
#3、保留源文件 压缩到指定工作目录
[root@haha yasuo]# gzip sd.txt -c > /jieya/sd.txt.gz
[root@haha yasuo]# bzip2 ssh_config -c > /jieya/ssh_config.bz2
[root@haha yasuo]# xz ssh_config -c > /jieya/ssh_config1.xz
#4、保留源文件 解压缩到指定工作目录
[root@haha yasuo]# gunzip /jieya/sd.txt.gz -c > /jieya/sd
[root@haha yasuo]# bunzip2 /jieya/ssh_config.bz2 -c > /yasuo/ssh_config
[root@haha yasuo]# unxz /jieya/ssh_config1.xz -c > /yasuo/ssh_config1
(3)tar
打包格式: tar 打包过后的路径 对谁打包
解包格式: tar 对谁解包 -C 解包后的路径(解包到哪)
-z 调用 gzip 程序,以 gzip 格式压缩或解压缩文件(.tar.gz)
-j 调用 bzip2 程序,以 bzip2 格式压缩或解压缩文件(.tar.bz2)
-J 使用 xz 压缩或解压缩文件(.tar.xz),xz 的压缩率通常比 bzip2 更高
-a 根据压缩包后缀自动匹配,自动匹配压缩算法进行压缩
-C 和 -x 选项一起使用,表示释放包时指定释放的目标路径
# 压缩
tar -cvf
# 解压缩
tar -xvf
6、查找文件
# 1、查看指定文件类型
find / -type b
# 2、查看指定权限的文件
find /etc/ -perm 755
# 3、查看文件所属用户
find /home -user user1
# 4、查看文件所属组
find /home -group redhat
# 5、查看文件大小(20M内)
find /etc/ -size -20M
# 6、查看文件日期(三天前)
find /var/log -atime +3
# 7、查看文件名称
find /var/log -name "*.log"
find /home -maxdepth 3 # 在 /home 目录及其前两层子目录中查找文件
find /home -maxdepth 2 # 在 /home 目录及其前一层子目录中查找文件
find /root/ -name haha* -delete # 删除 /root/ 目录下所有 名字是haha开头的文件
练习:
(1)将当前目录下 .conf 结尾的文件进行备份
方法一:for循环
# 检查当前目录
[root@openEuler-1 mnt]# ls
cgconfig.conf dracut.conf host.conf krb5.conf libuser.conf man_db.conf resolv.conf sudo.conf vconsole.conf
cgsnapshot_blacklist.conf e2scrub.conf idmapd.conf ld.so.conf locale.conf mke2fs.conf rsyslog.conf sysctl.conf xattr.conf
chrony.conf fuse.conf kdump.conf libaudit.conf logrotate.conf nsswitch.conf sestatus.conf tcsd.conf yum.conf
# for循环依次拷贝文件
[root@openEuler-1 mnt]# for file in `ls`;do
> cp $file $file.bak
> done
# 再次检查当前目录
[root@openEuler-1 mnt]# ls
cgconfig.conf e2scrub.conf kdump.conf libuser.conf mke2fs.conf sestatus.conf vconsole.conf
cgconfig.conf.bak e2scrub.conf.bak kdump.conf.bak libuser.conf.bak mke2fs.conf.bak sestatus.conf.bak vconsole.conf.bak
cgsnapshot_blacklist.conf fuse.conf krb5.conf locale.conf nsswitch.conf sudo.conf xattr.conf
cgsnapshot_blacklist.conf.bak fuse.conf.bak krb5.conf.bak locale.conf.bak nsswitch.conf.bak sudo.conf.bak xattr.conf.bak
chrony.conf host.conf ld.so.conf logrotate.conf resolv.conf sysctl.conf yum.conf
chrony.conf.bak host.conf.bak ld.so.conf.bak logrotate.conf.bak resolv.conf.bak sysctl.conf.bak yum.conf.bak
dracut.conf idmapd.conf libaudit.conf man_db.conf rsyslog.conf tcsd.conf
dracut.conf.bak idmapd.conf.bak libaudit.conf.bak man_db.conf.bak rsyslog.conf.bak tcsd.conf.bak
方法二:awk
# 删除上面的备份文件
[root@openEuler-1 mnt]# rm -f *.bak
[root@openEuler-1 mnt]# ls
cgconfig.conf dracut.conf host.conf krb5.conf libuser.conf man_db.conf resolv.conf sudo.conf vconsole.conf
cgsnapshot_blacklist.conf e2scrub.conf idmapd.conf ld.so.conf locale.conf mke2fs.conf rsyslog.conf sysctl.conf xattr.conf
chrony.conf fuse.conf kdump.conf libaudit.conf logrotate.conf nsswitch.conf sestatus.conf tcsd.conf yum.conf
# awk内调用system()命令,将$1变量传进去
[root@openEuler-1 mnt]# ls | awk '{system("cp "$1" "$1".bak")}'
# 查看结果
[root@openEuler-1 mnt]# ls
cgconfig.conf e2scrub.conf kdump.conf libuser.conf mke2fs.conf sestatus.conf vconsole.conf
cgconfig.conf.bak e2scrub.conf.bak kdump.conf.bak libuser.conf.bak mke2fs.conf.bak sestatus.conf.bak vconsole.conf.bak
cgsnapshot_blacklist.conf fuse.conf krb5.conf locale.conf nsswitch.conf sudo.conf xattr.conf
cgsnapshot_blacklist.conf.bak fuse.conf.bak krb5.conf.bak locale.conf.bak nsswitch.conf.bak sudo.conf.bak xattr.conf.bak
chrony.conf host.conf ld.so.conf logrotate.conf resolv.conf sysctl.conf yum.conf
chrony.conf.bak host.conf.bak ld.so.conf.bak logrotate.conf.bak resolv.conf.bak sysctl.conf.bak yum.conf.bak
dracut.conf idmapd.conf libaudit.conf man_db.conf rsyslog.conf tcsd.conf
dracut.conf.bak idmapd.conf.bak libaudit.conf.bak man_db.conf.bak rsyslog.conf.bak tcsd.conf.bak
相较于方法一,awk处理文件性能更优。