Linux基础知识--文件内容操作命令

文件内容操作命令

  cat命令:

  用途:用途显示文件的全部内容,也可以同时显示多个文件的内容。

  语法:cat 文件名

  more命令和less命令

  用途:全屏方式分页显示内容

  head、tail命令

  用途与格式:

    head是查看文件开头的一部分(默认10行)。格式为head -n 文件名

    tail是查看文件末尾的一部分(默认10行),格式为:tail -n 文件名    tail -f 文件名

  

  wc命令:

    用途:统计文件中单词的数量(Word count)等信息

    格式:wc [选项]... 目标文件...

    -l 统计行数

    -w 统计单词个数

    -c 统计字节数

[root@localhost home]# cat file
i am bigbigtong
[root@localhost home]# wc file
 1  3 16 file
[root@localhost home]# wc -l file
1 file
[root@localhost home]# wc -w file
3 file
[root@localhost home]# wc -c file
16 file

  可以看到file文件中有1行,3个单词,占16个字节

  grep命令:

    用途:在文件中查找并显示包含某字符串的行

    格式:grep [选项]... 查找条件 目标文件

      -i 查找时忽略大小写

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

    查找条件设置:

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

      “^……”表示以……开头,“……$”表示以……结尾

      “^$”表示空行

      

[root@localhost home]# cp /etc/audisp/audispd.conf /home
[root@localhost home]# ls
audispd.conf  file  file1  file2  test1  test2
[root@localhost home]# cat audispd.conf
#
# This file controls the configuration of the audit event
# dispatcher daemon, audispd.
#

q_depth = 64
overflow_action = SYSLOG
name_format = HOSTNAME
#name = mydomain

[root@localhost home]# grep "name" audispd.conf
name_format = HOSTNAME
#name = mydomain

  audispd.cof文件中查找包含"name"的行,结果就是这两行

[root@localhost home]# grep -v "name" audispd.conf
#
# This file controls the configuration of the audit event
# dispatcher daemon, audispd.
#

q_depth = 64
overflow_action = SYSLOG

  查找不包含“name”的行

  

[root@localhost home]# grep -i "this" audispd.conf
# This file controls the configuration of the audit event

  忽略大小写

  

[root@localhost home]# grep "^#" audispd.conf
#
# This file controls the configuration of the audit event
# dispatcher daemon, audispd.
#
#name = mydomain
[root@localhost home]# grep "main$" audispd.conf
#name = mydomain
[root@localhost home]# grep "^$" audispd.conf

  以#开头,以main结尾以及空行的信息

  

[root@localhost home]# grep -v "^$" audispd.conf
#
# This file controls the configuration of the audit event
# dispatcher daemon, audispd.
#
q_depth = 64
overflow_action = SYSLOG
name_format = HOSTNAME
#name = mydomain

  去除空行

  tar命令:

  用途:制作归档文件、释放归档文件

  格式:

    制作归档文件:tar [选项]... 归档文件名 源文件或目录

    释放归档文件:tar [选项]... 归档文件名 [-C 目标目录]

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

    -x 解开.tar格式的包文件

    -v 输出详细信息

    -f 表示使用归档文件

    -p 打包时保留源文件或目录的权限

    -t 列表查看包内文件

    -C 解包时指定释放的目标目录

    -z 调用gzip程序进行压缩或解压

    -j 调用bzip2程序进行压缩或解压

    

  创建归档文件时一般使用.tar文件名作为结尾

[root@localhost home]# ls
audispd.conf  file  file1  file2  test1  test2
[root@localhost home]# tar -cvf file1.tar file1
file1
[root@localhost home]# ls
audispd.conf  file  file1  file1.tar  file2  test1  test2
[root@localhost home]# ls -l file1
-rw-r--r-- 1 root root 98 Jan 27 04:11 file1
[root@localhost home]# ls -l file1.tar
-rw-r--r-- 1 root root 10240 Jan 27 06:08 file1.tar

  可以看到打包之后的文件比源文件还大,这是因为打包并没有进行压缩,而且打包还带有一些打包信息

  如果你是使用gzip程序进行压缩,后缀名就是.gz

[root@localhost home]# tar -zcvf file1.tar.gz file1
file1
[root@localhost home]# ls -l file1.tar.gz
-rw-r--r-- 1 root root 148 Jan 27 06:13 file1.tar.gz

  会发现一个有趣的现象,压缩之后的文件比源文件还大 

[root@localhost home]# du -h file1
4.0K    file1
[root@localhost home]# du -h file1.tar.gz
4.0K    file1.tar.gz

  使用du命令查看两个文件的磁盘空间,均为4k,这是因为Linux给文件划分磁盘空间的时候,最小单位为4k。

  在压缩的时候,会写入一些打包信息,如果你源文件太小,那么压缩之后的文件就会比源文件更大。

  新建一个文件,往里面添加多一点的内容

  

[root@localhost home]# tar -zcvf file3.tar.gz file3
file3
[root@localhost home]# ls -l
total 52
-rw-r----- 1 root root   173 Jan 27 04:26 audispd.conf
-rw-r--r-- 1 root root    16 Jan 27 04:18 file
-rw-r--r-- 1 root root    98 Jan 27 04:11 file1
-rw-r--r-- 1 root root 10240 Jan 27 06:08 file1.tar
-rw-r--r-- 1 root root   148 Jan 27 06:13 file1.tar.gz
-rw-r--r-- 1 root root   117 Jan 27 04:11 file2
-rw-r--r-- 1 root root   160 Jan 27 06:17 file2.tar.gz
-rw-r--r-- 1 root root  1415 Jan 27 06:31 file3
-rw-r--r-- 1 root root   213 Jan 27 06:32 file3.tar.gz
drwxr-xr-x 3 root root  4096 Jan 27 00:27 test1
drwxr-xr-x 2 root root  4096 Jan 27 00:23 test2

  可以看到这次压缩file3,压缩后的文件比源文件小很多了

  把源文件都删除,然后进行解压缩

[root@localhost home]# tar -zxvf file3.tar.gz
file3
[root@localhost home]# ls
audispd.conf  file1.tar.gz  file3         test1
file1.tar     file2.tar.gz  file3.tar.gz  test2

  可以看到file3被解压出来了,不写目标目录,默认解压到当前目录

  还可以解压到指定的目录,-C 指定的目录名

[root@localhost home]# mkdir test
[root@localhost home]# tar -zxvf file3.tar.gz -C test
file3
[root@localhost home]# ls test
file3

  可以看到压缩文件被解压到test目录下了

  

  如果你用-j调用bzip2程序压缩文件,那么后缀名就是bz2

  

[root@localhost home]# ls -l test
total 4
-rw-r--r-- 1 root root 1415 Jan 27 06:31 file3
[root@localhost home]# tar -jcvf test.tar.bz2 test
test/
test/file3
[root@localhost home]# ls
audispd.conf  file1.tar.gz  file3         test   test2
file1.tar     file2.tar.gz  file3.tar.gz  test1  test.tar.bz2
[root@localhost home]# ls -l test.tar.bz2
-rw-r--r-- 1 root root 278 Jan 27 06:42 test.tar.bz2
[root@localhost home]# tar -jxvf test.tar.bz2 -C test1
test/
test/file3
[root@localhost home]# ls -l test1
total 4
lrwxrwxrwx 1 root root   11 Jan 27 00:16 file1 -> /home/file1
drwxr-xr-x 2 root root 4096 Jan 27 06:38 test

  可以看到压缩文件解压到了指定的目录下

  vim命令:

  用途:编辑文件

  格式: vim 文件名

  i 进入编辑模式

  wq 保存并退出,如果vim 文件名(文件不存在),wq保存之后,该文件也就生成并且编辑保存了

  q! 强制退出,不保存,如果vim 文件名(文件不存在),q!之后,该文件也不会生成

  小结:

  tar 命令制作或释放归档文件,-cvf 是打包但并没有压缩,-zcvf 以gzip程序压缩文件,-jcvf以bzip2程序压缩文件,-x是解压缩,-C 跟指定目录是解压到指定目录

  vim创建新文件时,只有保存并退出,文件才会被创建编辑

  cat、wc、head、tail、grep

  

  

猜你喜欢

转载自www.cnblogs.com/bigbigtong/p/10325230.html
今日推荐