Linux目录和文件高级操作详述(2)

wc用于统计文件中的单词数量、(Word Count)、行数、字节数等。基本语法格式如下:

Linux目录和文件高级操作详述

wc常用选项如下:

-l:统计行数;

-w:统计单词个数;

-c:统计字节数;

应用举例:

[root@centos01 ~]# wc -l /etc/passwd <!--统计文件行数--> 41 /etc/passwd [root@centos01 ~]# wc -w /etc/passwd <!--统计文件中单词个数--> 81 /etc/passwd [root@centos01 ~]# wc -c /etc/passwd <!--统计文件中字节数--> 2104 /etc/passwd [root@centos01 ~]# wc /etc/passwd <!--不加选项统计顺序依次是43行,85个单词,2223个字节--> 43 85 2223 /etc/passwd [root@centos01 ~]# find /etc -name "*.conf" | wc -l <!--统计/etc下有多少个以*.conf为后缀的文件数量--> 437 6、grep——检索和过滤文件内容

grep命令用于在文件中查找并显示包含指定字符串的行。基本语法格式如下:

grep命令常用选项如下:

-i:查找时忽略大小写;

-v:反转查找,输出与条件不相符的行;

grep查找条件设置:

要查找的字符串以双引号括起来;

“^......”:表示查找以......开头的字符串;

“......$”:表示查找以......结尾的字符串;

“^$”:表示查找空行;

应用举例:

[root@centos01 ~]# grep -i "SSHD" /etc/passwd <!--查询sshd用户忽略大小写显示出来--> sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin [root@centos01 ~]# grep "ftp" /etc/passwd <!--查询ftp用户显示出来--> ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin [root@centos01 ~]# grep -v "^#" /etc/yum.conf | grep -v "^$" <!--过滤/etc/yum.conf中的注释行和空行--> [main] cachedir=/var/cache/yum/$basearch/$releasever keepcache=0 debuglevel=2 logfile=/var/log/yum.log exactarch=1 obsoletes=1 gpgcheck=1 plugins=1 installonly_limit=5 bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum distroverpkg=centos-release 三、压缩和解压缩文件 1、gzip、gunzip——压缩和解压缩

Linux目录和文件高级操作详述

应用举例:

<!--压缩和解压缩方法一(“-9”选项代表高压缩)--> [root@centos01 ~]# ls anaconda-ks.cfg CentOS-7.4-x86_64-1708.iso initial-setup-ks.cfg [root@centos01 ~]# gzip -9 initial-setup-ks.cfg [root@centos01 ~]# ls anaconda-ks.cfg CentOS-7.4-x86_64-1708.iso initial-setup-ks.cfg.gz [root@centos01 ~]# gzip -d initial-setup-ks.cfg.gz [root@centos01 ~]# ls anaconda-ks.cfg CentOS-7.4-x86_64-1708.iso initial-setup-ks.cfg <!--压缩和解压缩方法二(不加选项进行压缩,解压缩使用gunzip命令)--> [root@centos01 ~]# ls anaconda-ks.cfg CentOS-7.4-x86_64-1708.iso initial-setup-ks.cfg [root@centos01 ~]# gzip initial-setup-ks.cfg [root@centos01 ~]# ls anaconda-ks.cfg CentOS-7.4-x86_64-1708.iso initial-setup-ks.cfg.gz [root@centos01 ~]# gunzip initial-setup-ks.cfg.gz [root@centos01 ~]# ls anaconda-ks.cfg CentOS-7.4-x86_64-1708.iso initial-setup-ks.cfg 2、bzip2、bunzip2——压缩和解压缩

Linux目录和文件高级操作详述

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/11553.html