LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 689|回复: 3

[请教]目录显示的问题

[复制链接]
发表于 2006-3-10 13:08:33 | 显示全部楼层 |阅读模式
环境:RH AS 4.0 BASH SHELL
问题:什么命令可以输出当前目录下的文件和其子目录下的文件和文件夹(列表形式)。
   ls可以吗?
   如果是其他的命令话,参数比较多的话,可以给个详细说明吗?
目的:可以输出成一个文本文件,查看其目录下的所有文件和文件夹。
发表于 2006-3-10 13:45:40 | 显示全部楼层
记得以前RedHat老带着一个叫tree的命令,现在不知道还有没有.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-10 15:03:37 | 显示全部楼层
我用一个命令试了下,还可以,就是没有tree好
ls -lR|tee save
不知道有没有更好的!~
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-10 15:11:41 | 显示全部楼层
才看了一个,要写脚本,不知道怎么用,全写下来,大家看看,怎么用:
一个输效果基本和tree命令的输出完全一样(除了目录未的工作不同),文件带色彩输出,文件、目录数统计,贴上来给大家做个参考:


#!/bin/sh
#
LSPROG="/bin/ls"

# COLOR DEFINE
# ============
#
DIR="\033[01;34m"
EXE="\033[01;32m"
DEV="\033[01;33m"
LNK="\033[01;36m"
ZIP="\033[01;31m"
SOCK="\033[01;35m"
NULL="\033[00m"

ROOT=${1:-.}
TRUE=0
FALSE=1
LAYERS=0
FILECOUNT=0
DIRCOUNT=0

# print_dash
# ==========
# Print the structure lines
#
print_dash()
{
        local i=0
        local num=$1
        
        while [ $i -lt $num ]; do
                echo -n "|"
                for j in 1 2 3; do
                        echo -n " "
                done

                i=`expr $i + 1`
        done

        echo -n "|-- "
}

# ispkg
# =====
# Test if the file is a package like:
# .gz .tar .tgz .tar.gz .zip .rar .rpm
# and etc.
#
ispkg()
{
        local f=$1
        local i        

        # Package extension list, you can add your coustom
        # extensions in it.
        #
        local pkg__ext=".gz .tar .tgz .tar.gz .zip .rar .rpm"

        # if the file's suffix contain any package extension
        # then cut it.

        for i in $pkg__ext; do
                f=${f%$i}
        done

        if [ "$f" != "$1" ]; then
                return $TRUE
        else
                return $FALSE
        fi
}

# mktree
# ======
# The main function, that print the
# dictionary structure in dos's tree
# command style. It's runs in nesting.
#
mktree()
{
        local f
        for f in `$LSPROG -1 $1 2> /dev/null`; do
                f=${f%/}
                f=${f##*/}
        
                # If dictionary then print it and enter
                # the nesting block.
                if [ -d $1/$f ]; then
                        print_dash $LAYERS
                        echo -e "${DIR}$f${NULL}"
                        DIRCOUNT=`expr $DIRCOUNT + 1`

                        LAYERS=`expr $LAYERS + 1`
                        mktree $1/$f
                else
                        print_dash $LAYERS
                        # file is a symbol link
                        if [ -L $1/$f ]; then
                                echo -e "${LNK}$f${NULL}"
                        # file is executable
                        elif [ -x $1/$f ]; then
                                echo -e "${EXE}$f${NULL}"
                        # file is a device
                        elif [ -c $1/$f -o -b $1/$f ]; then
                                echo -e "${DEV}$f${NULL}"
                        # file is a socket
                        elif [ -S $1/$f ]; then
                                echo -e "${SOCK}$f${NULL}"
                        # file is a package
                        elif `ispkg $f`; then
                                echo -e "${ZIP}$f${NULL}"
                        else        
                                echo -e "$f"
                        fi
                        
                        FILECOUNT=`expr $FILECOUNT + 1`
                fi
        done

        LAYERS=`expr $LAYERS - 1`
}

echo $ROOT
mktree $ROOT
echo "\`"
echo "$DIRCOUNT directories, $FILECOUNT files"


希望会的给个回复,谢谢了!~
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表