首页 > 系统 > Linux > 正文

Linux下一些常用的Shell脚本整理

2019-10-26 18:46:47
字体:
来源:转载
供稿:网友

如何计算当前目录下的文件数和目录数
   
   

 # ls -l * |grep "^-"|wc -l ---- to count files    # ls -l * |grep "^d"|wc -l ----- to count dir

   
    如何只列子目录?
   

  ls -F | grep /$ 或者 alias sub = "ls -F | grep /$"(linux)    ls -l | grep "^d" 或者 ls -lL | grep "^d" (Solaris)

   
    如何实现取出文件中特定的行内容
  
    如果你只想看文件的前5行,可以使用head命令,
  
    如: 

head -5 /etc/passwd

   
    如果你想查看文件的后10行,可以使用tail命令,
  
    如: 

tail -10 /etc/passwd

   
    你知道怎么查看文件中间一段吗?你可以使用sed命令
  
    如:

sed -n '5,10p' /etc/passwd 

这样你就可以只查看文件的第5行到第10行。
  
    如何查找含特定字符串的文件
  
    例如查找当前目录下含有"the string you want find…"字符串的文件:
   
    

$find . -type f -exec grep "the string you want find…" {} ; -print

   
    如何列出目录树
  
    下面的短小的shell程序可以列出目录树, 充分利用了sed强大的模式匹配能力。
  
    目录树形式如下:
   

  .    `--shellp    `--updates    `--wu-ftpd-2.4    | `--doc    | | `--examples    | `--src    | | `--config    | | `--makefiles    | `--support    | | `--makefiles    | | `--man    | `--util

   
    脚本如下:
   
  

 #!/bin/sh    # dtree: Usage: dtree [any directory]    dir=${1:-.}    (cd $dir; pwd)    find $dir -type d -print | sort -f | sed -e "s,^$1,," -e "/^$/d" -e "s,[^/]*/([^/]*)$,`----1," -e "s,[^/]*/,| ,g"

   
    如何实现取出文件中特定的列内容
  
    我们经常会遇到需要取出分字段的文件的某些特定字段,例如/etc/password就是通过":"分隔各个字段的。可以通过cut命令来实现。例如,我们希望将系统账号名保存到特定的文件,就可以:
   

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表