Linux-1.4文件操作命令(grep,cat,tail,head,less,find,chmod,tail,less)

Linux基础命令(grep,cat,tail,head,less,find,chmod,tail,less)

grep(常用)

  •  grep 指定“文件”搜索文件内容
    • grep hello 1.txt 在文件中查找字串
    • grep -n hello 1.txt 显示字串的行数
    • grep -i hello 1.txt 不区分大小写
    • grep -in hello 1.txt 显示字串的行数&不区分大小写
    • grep -v hello 1.txt 反向查询不包含字串的

      

  • grep 在目录下搜索内容包含hello的文件
    • grep -n hello 目录 -r

      

  • 正则查找
    • h* 查找包含h的
    • ^h 查找行头是h的
    • h$ 查找行尾是h的

      

cat

  • cat 查看文件内容
    • cat 1.txt 把文件内容全部显示在终端中 
    • cat 1.txt 2.txt 把n个文件内容依次全部显示在终端中 
    • cat 1.txt 2.txt > 3.txt 显示后内容合并存储到新的文件中

tail

  •  tail 显示指定文件末尾内容(常用查看日志文件)
    • tail a.log 默认显示文件末尾10行(等同于tail -10 a.log,等同于tail -n -10 a.log)
    • tail -n +5 a.log 从文件的第6条显示所有内容
    • tail -f a.log 循环查看文件内容,会刷新出文件新增的内容。使用于文档监视

head

  • head 显示指定文件头部内容
    • head a.log 默认显示文件头10行(等同于head -10 a.log,等同于head -n -10 a.log)
    • head -n +5 a.log 显示文件的最后5条

less 

  • less 分页显示文件信息
    • less a.log(↑↓箭头查看文件内容,more不能回放)
    • 按q退出
    • less -N a.log 显示了行号
    • less -N +10 a.log 从第十行开始显示

find

扫描二维码关注公众号,回复: 4762504 查看本文章
  •  find 查找文件
    • find /home -name a.txt 在目录下查找指定文件
    • find /home -name '*txt' 在目录下查找以txt结尾的文件(一定要用英文单引号!)

chmod

  •  chmod 字母修改文件权限
    • chmod u+x a.txt
      • r,w,x,- 权限(read,write,excute,-表示无权限)
      • u,g,o,a(u 文件所有者;g 同组的人;o 其他人;a 所有人)
      • +,-,=(权限的增加,减少,赋值)

       

  •  chmod 数字修改文件权限
    • chmod 761 a.txt
    • 三个数字依次表示用户类型:ugo
    • 数字的值表示权限r=4,w=2,x=1,-=0
      • 若要rwx属性则4+2+1=7;
      • 若要rw-属性则4+2=6;
      • 若要r-x属性则4+1=7。

猜你喜欢

转载自www.cnblogs.com/bf-blackfish/p/10213298.html