RubyPDF Blog Ubuntu,中文 tree,linux(Unix)下按层级显示目录结构的脚本

tree,linux(Unix)下按层级显示目录结构的脚本

因为想生成Grails创建的目录结构,就尝试ls命令,发现不能满足我的要求(能力不够),网上找到这么一个脚本Tree.sh,感觉不错,这里贡献下,顺便说下安装过程中碰到的一个小问题。
我在Ubuntu下直接下载tree.sh并按要求安装,然后运行tree,得到如下错误:

/bin/sh^M: bad interpreter: No such file or directory

一时没有了主意,还是google吧,结果很少,但不是零,打开页面一看,不知哪国语言,但稀里糊涂地感觉是文本文件的类型问题,想了想,还是自己创建个tree文件,然后把tree.sh里的内容copy过来好了,这招的确有效,不然还要找工具来转换,因为我还不知道什么工具能做这样的转换呢(Linux刚刚入门不到一个星期)。

#!/bin/sh

#######################################################
# UNIX TREE #
# File: ~/apps/tree/tree.sh #
# Version: 2.1 #
# Displays Structure of Directory Hierarchy #
# —————————————– #
# This tiny script uses “ls”, “grep”, and “sed” #
# in a single command to show the nesting of #
# sub-directories. #
# #
# Setup: #
# % cd ~/apps/tree #
# % chmod u+x tree.sh #
# % ln -s ~/apps/tree/tree.sh ~/bin/tree #
# #
# Usage: #
# % tree [directory] #
# #
# Examples: #
# % tree #
# % tree /etc/opt #
# % tree .. #
# #
# Public Domain Software — Free to Use as You Like #
# http://www.centerkey.com/tree #
#######################################################

echo
if [ “$1” != “” ] #if parameter exists, use as base folder
then cd $1
fi
pwd
ls -R | grep “:$” | \
sed -e ‘s/:$//’ -e ‘s/[^-][^\/]*\//–/g’ -e ‘s/^/ /’ -e ‘s/-/|/’
# 1st sed: remove colons
# 2nd sed: replace higher level folder names with dashes
# 3rd sed: indent graph three spaces
# 4th sed: replace first dash with a vertical bar
if [ `ls -F -1 | grep “/” | wc -l` = 0 ] # check if no folders
then echo ” -> no sub-directories”
fi
echo
exit

1 thought on “tree,linux(Unix)下按层级显示目录结构的脚本”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.