Linux命令 3——文件处理命令

Linux文件相关命令

  • touch命令:创建文件
  • find命令:查找文件
  • cat命令:输出文件内容    经常用作管道符号的输入
  • nl命令:将输出的文件内容自动的加上行号  
  • grep命令 :在文件中查找符合条件的字符串
  • sed:主要用来编辑一个或多个文件简化对文件的反复操作
  • Linux tr命令  用于转换或删除文件中的字符
  • awk命令:awk 是一种文本文件的处理语言,逐行获取处理。处理:就包括很多了,查找,替换,编辑等都叫处理。
  • wc命令: Word Count wc命令功能 也是用来处理文件的。统计指定文件的Byte数、字数、行数并将统计结果显示输出 

1.Linux touch命令: 参考菜鸟教程 Linux touch命令。用于修改文件或者目录的时间属性,包括存取时间和更改时间。如果文件不存在,就创建一个新文件。

创建文件 语法:touch file

例子

#创建一个空白文件
[root@VM_0_8_centos mm]# touch file1
[root@VM_0_8_centos mm]# ll
total 0
-rw-r--r-- 1 root root 0 Mar 19 18:27 file1

2.Linux find命令:根据条件 在给定目录中查找符合条件的文件。参考菜鸟教程 Linux find命令

语法:find path expression       返回每个符合条件的文件全路径

  • 在命令行上第一个 - ( ) , ! 之前的部份为 path之后的是 expression path 是空字串则使用目前路径
  • type 指定查找文件类型  例如 -type f: 一般文件   -type c:c   文件类型是c的文件
  • -maxdepth :指定遍历搜索的最大深度
  • -mindepth : 指定开始遍历搜索的最小深度
  •  maxdepth和mindepth 两个参数均以当前目录 . 作为起始深度1 。使用-maxdepth和-mindepth基于目录深度的搜索时,该参数应该作为find的第一种参数出现,否则会进行一些不必要的检查导致影响find的效率
  • -mtime -n +n   按照文件的更改时间来查找文件
  • -mmin +n  文件更改时间距离现在 n分钟之前, 比如查找5分钟前 更改的文件 -mmin +5
  • -mmin -n   文件更改时间距离现在 n分钟之内, 比如查找 5分钟内 更改的文件 -mmin -5
  • -mtime -n   文件更改时间距离现在 n天之内,   比如查找 3天内 更改的文件 -mtime -5
  • -regex : 根据正则表达式匹配文件名查找
  • -iregex : 根据正则表达式匹配文件名查找,不区分大写

例子:按文件类型 和 文件修改时间查找文件。

#在 ss目录下 查找最近30分钟修改的 类型为一般文件 的文件
[root@VM_0_8_centos ss]# find /ss -type f -mmin -30

例子:指定 遍历搜索的最大或最小深度,当前目录作为其实深度1。

#-maxdepth 0
[root@VM_0_8_centos /]# find ss -maxdepth 0
ss
#-maxdepth 1  
[root@VM_0_8_centos /]# find ss -maxdepth 1
ss
ss/file3
ss/mm
#-maxdepth 2  
[root@VM_0_8_centos /]# find ss -maxdepth 2
ss
ss/file3
ss/mm
ss/mm/mm_file1
#没有maxdepth 参数
[root@VM_0_8_centos /]# find ss 
ss
ss/file3
ss/mm
ss/mm/mm_file1

3.Linux cat命令:concatenate and print files in reverse cat命令是一个文本输出命令 常用于把文件内容输出到显示器上。参考菜鸟教程 Linux cat命令

cat命令和echo命令区别:cat是输出文件内容  echo是输出字符串。

语法:cat   [option]    [file]

  • option的可选项 有 -b -n等。
  • -b :列出行号,仅针对非空白行做行号显示,空白行不标行号!
  • -n :列印出行号,连同空白行也会有行号,与 -b 的选项不同

用法:查看文件内容、 创建一个或多个文件、  连接文件 、 重定向输出到终端或文件。具体介绍如下:

  • 一次显示整个文件:cat filename   
  • 从键盘创建一个文件:cat > filename  。。。。只输入cat命令的话,它只是接收标准输入的内容并在标准输出中显示,所以在输入一行并按回车后会在接下来的一行显示相同的内容。。。。像这种创建文件的时候 从键盘接收输入 按回车 结束输入 并把键盘输入 保存到filename文件
  • 将几个文件合并为一个文件:cat file1 file2 > file  
  • 和/dev/null 合用 清空文件内容  :  cat /dev/null > file

例子

#1.cat file  用来输出文件内容到
[root@VM_0_8_centos mm]# cat file1
echo test
hahahah
 
#2.cat > file3 创建新文件
[root@VM_0_8_centos mm]# ll
total 8
-rw-r--r-- 1 root root 18 Mar 19 18:34 file1
-rw-r--r-- 1 root root  1 Mar 20 10:45 file2
[root@VM_0_8_centos mm]# cat > file3 
^C
[root@VM_0_8_centos mm]# ll
total 8
-rw-r--r-- 1 root root 18 Mar 19 18:34 file1
-rw-r--r-- 1 root root  1 Mar 20 10:45 file2
-rw-r--r-- 1 root root  0 Mar 20 10:46 file3
 
#3.cat file1 file2 > file3 把file1和file2的内容合并到file3
[root@VM_0_8_centos mm]# cat file1 file2 > file3
[root@VM_0_8_centos mm]# cat file3
echo test
hahahah
this is file2
 
#4.清空file1文件内容
[root@VM_0_8_centos mm]# cat /dev/null > file1
 
#2.cat > file3 创建新文件 并从键盘键入输入 ctrL+c 结束输入
[root@VM_0_8_centos mm]# cat > file7
hello world!
this is file7
^C
[root@VM_0_8_centos mm]# cat file7
hello world!
this is file7

4.Linux nl命令 显示行号  参考html每天一个linux命令(11):nl命令

#这两行命令执行结果一样,都是把zcat文件 带行号显示在屏幕上,并且不算空行
[root@VM_0_8_centos bin]# cat zcat -b
[root@VM_0_8_centos bin]# nl zcat

 5.Linux grep命令 :在文件中查找符合条件的字符串,并把包含该字符串的一行打印出来。若不指定任何文件名称,或是所给予的文件名为 -,则 grep 指令会从标准输入设备读取数据 。它是有返回值的,所以做if判断条件的时候,常把grep 命令返回值重定向到/dev/null。参考 菜鸟教程 Linux grep 命令

语法:grep [option] file 或者 管道命令语法  cat file | grep [option]

参数

  • 参数 -q 或 --quiet或--silent : 不显示任何信息
  • -F 或 --fixed-regexp : 将样式视为固定字符串的列表

例子 在文件中查找符合条件的字符串 并打印出来      cat file | grep ""

#查看file1文件内容
[root@VM_0_8_centos mm]# cat file1
echo test
hahahah
#使用cat 和 grep命令 在file1文件中查找 "test" 字符串 返回结果是包含"test"字符串的这一行
[root@VM_0_8_centos mm]# grep "test" file1
echo test
#
[root@VM_0_8_centos ~]# grep -Fq "test" file1

例子    用echo | grep 判断字符串包含 判断字符串包含   echo "" | grep "  如果返回结果不是空字符串 就是包含

#echo 和 grep 合用 判断字符串包含 
[root@VM_0_8_centos mm]# echo "this is ah" | grep "ah" 
this is ah
result=$(echo "abcdefgh" | grep "def")
if [[ "$result" != "" ]]
then
    echo "包含"
else
    echo "不包含"
fi

例子    用echo | grep 判断字符串包含 并把grep结果重定向到/dev/null。把echo | grep 结果重定向 并不会影响判断结果 。以下shell脚本会 输出包含 , 因为if 判断的是echo "this is test" | grep "is" 返回结果  , >/dev/null操作没有返回结果。

if echo "this is test" | grep "is" > /dev/null
then
  echo "包含"
else
  echo "不包含"
fi


 6.Linux sed 命令 主要用来编辑一个或多个文件简化对文件的反复操作等。sed在处理文本时是逐行读取文件内容,读到匹配的行就根据指令操作,不匹配就跳过。参考菜鸟教程 Linux sed 命令 和 shell脚本--sed的用法

语法:sed [选项]  [指令]   [文件] 或者 sed   [选项]  [-f  包含sed指令的文件]  [文件] 

选项

  • -i 参数 表示直接对内容进行修改,不加-i 时,默认只是预览,不会对文件内容做实际修改

例子:sed实现替换文件内容  sed 's/要被取代的字符串/新的字符串/g' /g表示全部替换,否则只会替换第一行。

#查看file3的内容 
[root@VM_0_8_centos mm]# cat file3
echo test
hahahah
this is file2
ha wow
#全局替换 把'ha'字符串替换成'heng '
[root@VM_0_8_centos mm]# sed -i 's/ha/heng /g' file3
#再次查看file3的内容
[root@VM_0_8_centos mm]# cat file3
echo test
heng heng heng h
this is file2
heng  wow
#如果替换字符串是变量的拼接写法
[root@VM_0_8_centos ~]# sed -i 's/3/'$(date +%Y-%m-%d+%H:%M:%S)'/g' file4

7.Linux tr命令  用于转换或删除文件中的字符。从标准输入设备读取数据,经过字符串转译后,将结果输出到标准输出设备。参考 菜鸟教程 Linux tr命令

语法:tr [第一字符集]  [第二字符集]:把管道命令的输入中 第一字符集替换成第二字符集 并输出结果。

例子:用作管道命令 来替换标准输入中的指定字符 并输出

#获取字符串中的文件名作为管道命令的输入  替换文件名中的大写字母 并输入文件名
[root@VM_0_8_centos ~]# basename "mm/ss/hahah/20200323A" | tr [A-Z] [a-z]
20200323a
#把testfile文件中的小写字母 全部 替换成大写字母
[root@VM_0_8_centos ~]# cat testfile |tr a-z A-Z 

tr 命令和sed命令的对比 参考tr和sed对比

  • tr 只能用作管道命令 sed 可以用作管道命令,也可以直接处理文件
  • 替换功能时   tr作用于整段 实现如下:last | tr ‘[a-z]’ ‘[A-Z]’,而sed 是自己可以指定行  last | sed ‘1,$s/[a-z]/[A-Z]/g’

8. awk命令:awk 是一种文本文件的处理语言。其命名取自三位创始人 Alfred Aho,Peter Weinberger, 和 Brian Kernighan 的 Family Name 的首字符 参考菜鸟教程 Linux awk 命令awk命令获取文本的某一行某一列 、   linux awk命令详解

根据条件 获取文件中符合条件的行,并对这些行进行处理。

语法--命令行方式:awk  [-F field-seperator]  'commands'  input-file(s)

  • 其中[-F field-seperator] 域分隔符是可选的  就是把待处理文件的每一行以域分隔符分列,每一列为$1 $2 $3……$n
  • ‘commands’ 是真正要执行的awk命令  commands = pattern +action :pattern 表示 AWK 在数据中查找的内容,而 action 是在找到匹配内容时所执行的一系列命令
  •  input-file(s) 是待处理的文件。

 例子:读取文件中符合条件的行'$1<6 && $2>6',并且重定向到另一个文件

#1.待处理文件file3内容如下
[root@VM_0_8_centos ~]# cat file3
3 9 this is 3 9
3 20 this is 3 20
3 10 this is 3 10
6 10 this is 6 10
6 30 this is 6 30
#2.把file3的每一行 用域分隔符' '分开, 匹配$1<6并且$2>6的 行, 把这一行append到file4文件中,append时 每一列用',' 作为域分隔符
[root@VM_0_8_centos ~]# cat file3 | awk -F' ' '$1<6 && $2>6 {print $1 "," $2 "," $3 ","$4","$5","$6}' >>file4 
#3.查看file4文件内容
[root@VM_0_8_centos ~]# cat file4
3,9,this,is,3,9
3,20,this,is,3,20
3,10,this,is,3,10

例子: 读取文件中符合条件的行 'NR>2' 即行号>2   用awk的内置变量NR读取文件的特定行  NR:已经读出的记录数,就是行号,从1开始         linux || awk(1)

#从file3文件的 第4行开始读 
[root@VM_0_8_centos ~]# cat file3 | awk 'NR>2'

9.wc命令: Word Count wc命令功能 也是用来处理文件的。统计指定文件的Byte数、字数、行数并将统计结果显示输出 。参考菜鸟教程 Linux wc命令

语法 :wc [-clw] [--help] [--version] [文件]

  • -c或--bytes或--chars 只显示Bytes数。
  • -l或--lines 只显示行数。
  • -w或--words 只显示字数。

例子:wc 除了返回统计信息 还返回文件名。如果是只返回文件行数 可以用管道命令代替正常命令cat file | wc -l 代替 wc -l file

# file3文件的统计信息  
[root@VM_0_8_centos ss]# wc file3
 1  3 14 file3
[root@VM_0_8_centos ss]# wc -l file3
1 file3
[root@VM_0_8_centos ss]# wc -c file3
14 file3
[root@VM_0_8_centos ss]# wc -w file3
3 file3
#用作管道命令 只返回统计结果,不返回文件名
[root@VM_0_8_centos ss]# cat file3 | wc -l
1
发布了208 篇原创文章 · 获赞 84 · 访问量 44万+

猜你喜欢

转载自blog.csdn.net/dreamstar613/article/details/105065202