如何計算當前目錄下的文件數和目錄數
   
   
# 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命令來實現。例如,我們希望將系統賬號名保存到特定的文件,就可以:
               
新聞熱點
疑難解答