linux案例3

文件内容操作命令 —— cat、less
cat命令
用途:显示出文件的全部内容
格式:cat 目标文件
less命令
用途:全屏方式分页显示文件内容
交互操作方法:
按Enter键向下逐行滚动
按空格键向下翻一屏、按b键向上翻一屏
按q键退出

文件内容操作命令—— grep
grep命令
用途:在文件中查找并显示包含指定字符串的行
格式:grep [选项]… 查找条件 目标文件
常用命令选项
-i:查找时忽略大小写
-v:反转查找,输出与查找条件不相符的行
查找条件设置
要查找的字符串以双引号括起来
“^……”表示以……开头,“……KaTeX parse error: Expected group after '^' at position 11: ”表示以……结尾 “^̲”表示空行
案例:
[root@localhost ~]#
[root@localhost ~]# ls 显示当前目录内容
anaconda-ks.cfg lshome.txt 模板 图片 下载 桌面
initial-setup-ks.cfg 公共 视频 文档 音乐
[root@localhost ~]# cat anaconda-ks.cfg 查看anaconda-ks.cfg文件内容
[root@localhost ~]# less anaconda-ks.cfg 分页查看anaconda-ks.cfg文件内容(按Enter键向下逐行滚动 按空格键向下翻一屏、按b键向上翻一屏。 按q键退出)
[root@localhost ~]# grep “root” anaconda-ks.cfg 查看anaconda-ks.cfg文件中包含root字符串的行
[root@localhost ~]# grep -i “root” anaconda-ks.cfg 查看anaconda-ks.cfg文件中包含root字符串的行(忽略大小写,加-i)
[root@localhost ~]# grep -v “root” anaconda-ks.cfg 查看anaconda-ks.cfg文件中包含不root字符串的行
[root@localhost ~]# grep “^#” anaconda-ks.cfg 查看anaconda-ks.cfg文件中以#开头的行
[root@localhost ~]# grep “tionKaTeX parse error: Expected 'EOF', got '#' at position 71: …ot@localhost ~]#̲ grep "^” anaconda-ks.cfg 查看anaconda-ks.cfg文件中的空行
[root@localhost ~]# grep -v “^$” anaconda-ks.cfg 查看anaconda-ks.cfg文件中除空行以外行
[root@localhost ~]# grep -v “^#” anaconda-ks.cfg 查看anaconda-ks.cfg文件中以#开头行以外的行
[root@localhost ~]#

归档及压缩命令 —— tar
tar命令
用途:制作归档文件、释放归档文件
格式:tar [选项]… 归档文件名 源文件或目录
tar [选项]… 归档文件名 [-C 目标目录]
常用命令
打包:
tar -czvf [存放路径]归档文件名.tar.gz 源文件或目录 (c代表压缩,z代表gzip压缩格式,v代表在压缩时显示信息,f代表跟随参数信息)
或 tar -cjvf [存放路径]归档文件名.tar.bz2 源文件或目录 (j代表bz2压缩格式)
或 tar cJvf [存放路径]归档文件名.tar.xz 源文件或目录 (J代表xz压缩格式)
解包:
tar -xzvf [存放路径]归档文件名.tar.gz [-C 解压目录] (x代表解压缩,z代表gzip解压缩格式,v代表在解压缩时显示信息,f代表跟随参数信息)-C可忽略不写,默认解压缩到当前目录
或 tar -xjvf [存放路径]归档文件名.tar.bz2 [-C 解压目录]
或 tar xJvf [存放路径]归档文件名.tar.xz [-C 解压目录]
案例:
[root@localhost home]#
[root@localhost home]# tar -czvf student.tar.gz student 压缩student为gzip格式,名为student.tar.gz
student/
student/.mozilla/
student/.mozilla/extensions/
student/.mozilla/plugins/
student/.bash_logout
student/.bash_profile
student/.bashrc
[root@localhost home]# ls 查看当前目录文件内容
student student.tar.gz 查看到一个student.tar.gz压缩文件
[root@localhost home]# tar -xzvf student.tar.gz -C /opt/abc 解压缩student.tar.gz至/opt/abc/
student/
student/.mozilla/
student/.mozilla/extensions/
student/.mozilla/plugins/
student/.bash_logout
student/.bash_profile
student/.bashrc
[root@localhost home]# ls /opt/abc 查看/opt/abc/文件内容
a1 aa a.txt student 查看到一个student文件目录
[root@localhost home]# tar -cjvf students.tar.bz2 student 压缩student为bz2格式, students.tar.bz2
student/
student/.mozilla/
student/.mozilla/extensions/
student/.mozilla/plugins/
student/.bash_logout
student/.bash_profile
student/.bashrc
[root@localhost home]# ls 查看当前目录文件内容
student students.tar.bz2 student.tar.gz 查看到一个students.tar.bz2压缩文件
[root@localhost home]# tar -xjvf students.tar.bz2 -C /opt/ 解压缩students.tar.bz2至/opt/
student/
student/.mozilla/
student/.mozilla/extensions/
student/.mozilla/plugins/
student/.bash_logout
student/.bash_profile
student/.bashrc
[root@localhost home]# ls /opt/ 查看/opt /文件内容
abc home openoffice4 rh student 查看到一个student文件目录
[root@localhost home]#

发布了113 篇原创文章 · 获赞 3 · 访问量 4602

猜你喜欢

转载自blog.csdn.net/ghf183184/article/details/105017590