亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 3833 | 回復(fù): 7
打印 上一主題 下一主題

[文件目錄] Shell版tree和de-tree [復(fù)制鏈接]

論壇徽章:
2
白羊座
日期:2013-11-18 19:52:42辰龍
日期:2014-09-07 07:46:06
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2013-10-12 15:51 |只看該作者 |倒序?yàn)g覽
本帖最后由 damcool 于 2013-10-12 17:09 編輯

為了把導(dǎo)出的目錄文件搞得漂亮一點(diǎn),又沒辦法安裝tree,只能用土辦法。真作孽!各位高手看看還有什么可以優(yōu)化的。

Tree,把文件目錄導(dǎo)成樹狀結(jié)構(gòu),并作美化
  1. find . -print | awk 'NR>1{printf "%s",$0;if (system("[ -d \"" $0 "\" ]") == 0) printf "/";printf "\n"}'|LC_ALL=C sort|awk 'BEGIN{system("pwd")}{n=split($0,a,"/");for (i=2;i<n;i++) if (i<n-1 || a[n]!="") {printf "| "}; if (a[n]=="") {print "|___[D]"a[n-1]} else {print "|___[F]"a[n]}}'|tac|awk '{n=split($0,a,"");for(i=1;i<=n;i++) {if (a[i]=="|" && b[i]!="|" && a[i+1]!="_") a[i]=" "; b[i]=a[i];printf "%s",b[i]}; print ""}'|tac
復(fù)制代碼
De-Tree,將導(dǎo)出的樹狀結(jié)構(gòu)還原成find . -print 輸出,想想真TMD覺得自己有點(diǎn)賤,喜歡自孽!
  1. cat /tmp/tree|awk 'NR==1{base=$0;folders="";print $0}NR>1{while (gsub(/\|  /,"| |"));sub(/___\[[D|F]\]/,"");n=split($0,a,"|");split(folders,b,"/");folders="";for(i=2;i<n;i++)folders=folders"/"b[i];folders=folders"/"a[n]; print base""folders}'
復(fù)制代碼

論壇徽章:
15
2015年辭舊歲徽章
日期:2015-03-03 16:54:15雙魚座
日期:2015-01-15 17:29:44午馬
日期:2015-01-06 17:06:51子鼠
日期:2014-11-24 10:11:13寅虎
日期:2014-08-18 07:10:55酉雞
日期:2014-04-02 12:24:51雙子座
日期:2014-04-02 12:19:44天秤座
日期:2014-03-17 11:43:36亥豬
日期:2014-03-13 08:13:51未羊
日期:2014-03-11 12:42:03白羊座
日期:2013-11-20 10:15:18CU大;照
日期:2013-04-17 11:48:45
2 [報(bào)告]
發(fā)表于 2013-10-14 19:12 |只看該作者
蠻好。

這里有個(gè)現(xiàn)成的 http://www.centerkey.com/tree/。
  1. #!/bin/sh
  2. #######################################################
  3. #  UNIX TREE                                          #
  4. #  Version: 2.3                                       #
  5. #  File: ~/apps/tree/tree.sh                          #
  6. #                                                     #
  7. #  Displays Structure of Directory Hierarchy          #
  8. #  -------------------------------------------------  #
  9. #  This tiny script uses "ls", "grep", and "sed"      #
  10. #  in a single command to show the nesting of         #
  11. #  sub-directories.  The setup command for PATH       #
  12. #  works with the Bash shell (the Mac OS X default).  #
  13. #                                                     #
  14. #  Setup:                                             #
  15. #     $ cd ~/apps/tree                                #
  16. #     $ chmod u+x tree.sh                             #
  17. #     $ ln -s ~/apps/tree/tree.sh ~/bin/tree          #
  18. #     $ echo "PATH=~/bin:\${PATH}" >> ~/.profile      #
  19. #                                                     #
  20. #  Usage:                                             #
  21. #     $ tree [directory]                              #
  22. #                                                     #
  23. #  Examples:                                          #
  24. #     $ tree                                          #
  25. #     $ tree /etc/opt                                 #
  26. #     $ tree ..                                       #
  27. #                                                     #
  28. #  Public Domain Software -- Free to Use as You Like  #
  29. #  http://www.centerkey.com/tree  -  By Dem Pilafian  #
  30. #######################################################

  31. echo
  32. if [ "$1" != "" ]  #if parameter exists, use as base folder
  33.    then cd "$1"
  34.    fi
  35. pwd
  36. ls -R | grep ":$" |   \
  37.    sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'
  38. # 1st sed: remove colons
  39. # 2nd sed: replace higher level folder names with dashes
  40. # 3rd sed: indent graph three spaces
  41. # 4th sed: replace first dash with a vertical bar
  42. if [ `ls -F -1 | grep "/" | wc -l` = 0 ]   # check if no folders
  43.    then echo "   -> no sub-directories"
  44.    fi
  45. echo
  46. exit
復(fù)制代碼

論壇徽章:
15
2015年辭舊歲徽章
日期:2015-03-03 16:54:15雙魚座
日期:2015-01-15 17:29:44午馬
日期:2015-01-06 17:06:51子鼠
日期:2014-11-24 10:11:13寅虎
日期:2014-08-18 07:10:55酉雞
日期:2014-04-02 12:24:51雙子座
日期:2014-04-02 12:19:44天秤座
日期:2014-03-17 11:43:36亥豬
日期:2014-03-13 08:13:51未羊
日期:2014-03-11 12:42:03白羊座
日期:2013-11-20 10:15:18CU大;照
日期:2013-04-17 11:48:45
3 [報(bào)告]
發(fā)表于 2013-10-14 19:16 |只看該作者
看了一下,就是一行語句
  1. ls -R | grep ":$" |sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'
復(fù)制代碼

論壇徽章:
15
2015年辭舊歲徽章
日期:2015-03-03 16:54:15雙魚座
日期:2015-01-15 17:29:44午馬
日期:2015-01-06 17:06:51子鼠
日期:2014-11-24 10:11:13寅虎
日期:2014-08-18 07:10:55酉雞
日期:2014-04-02 12:24:51雙子座
日期:2014-04-02 12:19:44天秤座
日期:2014-03-17 11:43:36亥豬
日期:2014-03-13 08:13:51未羊
日期:2014-03-11 12:42:03白羊座
日期:2013-11-20 10:15:18CU大;照
日期:2013-04-17 11:48:45
4 [報(bào)告]
發(fā)表于 2013-10-14 19:40 |只看該作者
本帖最后由 rdcwayx 于 2013-10-14 21:57 編輯

http://en.wikipedia.org/wiki/Tree_(Unix)
  1. find . -type d
  2. # or
  3. ls -R | grep ': | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'
  4. # or
  5. ls -R | grep ': | sed -e 's/:$//' -e 's/[^\/]*\//|  /g' -e 's/|  \([^|]\)/`--\1/g'
  6. # or
  7. find . -print | sort | sed 's;[^/]*/;|___;g;s;___|; |;g'
復(fù)制代碼
命令 tree 的 源代碼, 自己可以編譯的。
ftp://mama.indstate.edu/linux/tree/
tree-1.6.0.tar (160 KB, 下載次數(shù): 13)

論壇徽章:
2
白羊座
日期:2013-11-18 19:52:42辰龍
日期:2014-09-07 07:46:06
5 [報(bào)告]
發(fā)表于 2013-10-14 20:06 |只看該作者
rdcwayx 發(fā)表于 2013-10-14 19:40
http://en.wikipedia.org/wiki/Tree_(Unix)命令 tree 的 源代碼, 自己可以編譯的。
ftp://mama.indstate. ...


很不錯(cuò)的資料,運(yùn)行起來出結(jié)果也很快。不過不能區(qū)分文件或目錄,而且對樹狀結(jié)構(gòu)的處理也很粗糙。

論壇徽章:
15
2015年辭舊歲徽章
日期:2015-03-03 16:54:15雙魚座
日期:2015-01-15 17:29:44午馬
日期:2015-01-06 17:06:51子鼠
日期:2014-11-24 10:11:13寅虎
日期:2014-08-18 07:10:55酉雞
日期:2014-04-02 12:24:51雙子座
日期:2014-04-02 12:19:44天秤座
日期:2014-03-17 11:43:36亥豬
日期:2014-03-13 08:13:51未羊
日期:2014-03-11 12:42:03白羊座
日期:2013-11-20 10:15:18CU大;照
日期:2013-04-17 11:48:45
6 [報(bào)告]
發(fā)表于 2013-10-15 08:52 |只看該作者
畢竟方便而且速度快很多啊, 自己設(shè)個(gè)alias 就可以直接用了。
  1. alias tree="ls -R | grep ":$" |sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/' "
復(fù)制代碼
如果要分辨目錄和文件,可以用這個(gè):
  1. find . -exec ls -Fd {} \; | sort |sed 's;\/$;[d];; s;[^/]*/;|___;g;s;___|; |;g'
復(fù)制代碼

論壇徽章:
2
白羊座
日期:2013-11-18 19:52:42辰龍
日期:2014-09-07 07:46:06
7 [報(bào)告]
發(fā)表于 2013-10-15 09:34 |只看該作者
本帖最后由 damcool 于 2013-10-24 17:10 編輯

來個(gè)完整的tree.sh吧
  1. #!/bin/bash

  2. ################################################################################
  3. #  Function Name:    HELP_USAGE
  4. #  Description:      Function to display the usage of the script
  5. #  Parameters:       None
  6. #  Return:           Help messages
  7. #  Called By:        Script Main Loop->Script Parameters' Handler
  8. #  History:          2013-Oct-15    Initial Edition              RobinHoo
  9. ################################################################################
  10. function help_usage(){
  11. cat <<EOF
  12. Usage: $PROGNAME [OPTION]... [DIRECTORY_NAME]
  13. Display directories and/or files in tree view instead of binary tree command.

  14.   -d, --dir      Display only the director tree
  15.   -r, --reverse  De-Tree of the directory tree view to find . -print layout
  16.   -h, --help     Show current help message of the script usages
  17.   
  18.   
  19. Example:
  20.   $PROGNAME              Display the tree view from current folder
  21.   $PROGNAME --dir        Display the directory tree view from current folder
  22.   $PROGNAME -d /usr      Show the directory tree view for /usr
  23.   $PROGNAME /etc         Show the tree view of /etc with files listed
  24.   
  25. Please Report Script Bugs to $AUTHOR_MAIL


  26. EOF
  27. exit 1
  28. }

  29. ################################################################################
  30. #  Function Name:    DUMP_FOLDER
  31. #  Description:      Function to display the tree view of given folder
  32. #  Parameters:       Folder Name,Prefix Tree
  33. #  Return:           None
  34. #  Called By:        Script Main Loop->Folder Tree View Drawing
  35. #  History:          2013-Oct-15    Initial Edition              RobinHoo
  36. ################################################################################
  37. function dump_folder(){
  38.     local dir_name="$1"
  39.     local dir_tree="$2"
  40.     local dir_item=""
  41.     local dir_nbr=0
  42.     local dir_idx=2
  43.     local dir_ifs=$IFS
  44.     IFS=\n'
  45.     if [ $FOLDER -eq 0 ]; then
  46.         dir_nbr=$(find "$dir_name" -maxdepth 1 -print|wc -l)
  47.         for dir_item in $(find "$dir_name" -maxdepth 1 -print|awk 'NR>1{n=split($0,a,"/");if (system("[ -d \"" $0 "\" ]") == 0) {printf "[D]"} else printf "[F]"; printf "%s\n",a[n]}'|sort); do
  48.             echo "$dir_tree|___$dir_item"
  49.             [ "$dir_name" == "/" ] && dir_name=""
  50.             if [ "${dir_item:0:3}" == "[D]" ] ; then
  51.                 dir_item="${dir_item:3}"
  52.                 [ $dir_idx -eq $dir_nbr ] && dump_folder "$dir_name/$dir_item" "$dir_tree  " || dump_folder "$dir_name/$dir_item" "$dir_tree| "
  53.             fi
  54.             dir_idx=$(($dir_idx+1))
  55.         done;
  56.     else
  57.         dir_nbr=$(find "$dir_name" -maxdepth 1 -type d -print|wc -l)
  58.         for dir_item in $(find "$dir_name" -maxdepth 1 -type d -print|awk 'NR>1{n=split($0,a,"/");printf "[D]%s\n",a[n]}'|sort); do
  59.             echo "$dir_tree|___$dir_item"
  60.             [ "$dir_name" == "/" ] && dir_name=""
  61.             dir_item="${dir_item:3}"
  62.             [ $dir_idx -eq $dir_nbr ] && dump_folder "$dir_name/$dir_item" "$dir_tree  " || dump_folder "$dir_name/$dir_item" "$dir_tree| "
  63.             dir_idx=$(($dir_idx+1))
  64.         done;   
  65.     fi
  66.     IFS=$dir_ifs
  67. }



  68. ################################################################################
  69. #  Function Name:    Script Main Loop
  70. #  History:          2012-Jun-06    Initial Edition              RobinHoo
  71. ################################################################################
  72. TREE_DIR="$(pwd)"
  73. BASE_DIR=$(cd "$(dirname "$0")" && pwd)
  74. PROGNAME=$(basename "$0")
  75. AUTHOR_MAIL="robin.hoo(at)outlook.com"
  76. FOLDER=0
  77. HELP=0
  78. REVERSE=0
  79. while [ $# -gt 0 ]
  80. do
  81.     case "$1" in
  82.     (-d)            FOLDER=1;;
  83.     (-r)            REVERSE=1;TREE_DIR="/dev/stdin";;
  84.     (-h)            HELP=1;shift;break;;
  85.     (--dir)         FOLDER=1;;
  86.     (--reverse)     REVERSE=1;TREE_DIR="/dev/stdin";;
  87.     (--help)        HELP=1;shift;break;;
  88.     (-*)            echo "$PROGNAME: error - unrecognized option or parameter $1" 1>&2; HELP=1;break;;
  89.     (*)             TREE_DIR="$1";shift;break;;
  90.     esac
  91.     shift
  92. done
  93. [ $# -gt 0 ] && HELP=1
  94. [ $REVERSE -eq 0 ] && [ ! -d "$TREE_DIR" ] && HELP=1
  95. [ $REVERSE -eq 1 ] && [ "$TREE_DIR" != "/dev/stdin" ] && [ ! -f "$TREE_DIR" ] && HELP=1
  96. [ $HELP -eq 1 ] && help_usage

  97. [ $REVERSE -eq 0 ] && echo "$TREE_DIR" && dump_folder "$TREE_DIR" ""
  98. [ $REVERSE -eq 1 ] && cat <"$TREE_DIR"|awk 'NR==1{if ($0!="/") base=$0;folders="";print base"/"}NR>1{gsub(/^   /,"  |");while (gsub(/\|  /,"| |"));if (gsub(/___\[D\]/,"___[D]")) $0=$0"/";sub(/___\[[D|F]\]/,"");n=split($0,a,"|");split(folders,b,"/");folders="";for(i=2;i<n;i++)folders=folders"/"b[i];folders=folders"/"a[n]; print base""folders}'
復(fù)制代碼

論壇徽章:
2
白羊座
日期:2013-11-18 19:52:42辰龍
日期:2014-09-07 07:46:06
8 [報(bào)告]
發(fā)表于 2013-10-15 10:14 |只看該作者
rdcwayx 發(fā)表于 2013-10-15 08:52
畢竟方便而且速度快很多啊, 自己設(shè)個(gè)alias 就可以直接用了。如果要分辨目錄和文件,可以用這個(gè):


嗯,我最早也是用你列出的辦法,不過,本人有點(diǎn)吹毛求疵,最后忍不住自己寫了一個(gè)。
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP