linux shell 遍历文件夹 并将结果保存 到变量

跪求 用Shell 脚本 实现 显示test目录的各文件和目录
目录 folder1 下面 有三个文件a.txt b.txt c.txt 有一个子目录 subfolder 和两个文件 d.txt e.txt

要求遍历folder1的全部目录输出结果
folder1 Directory File a.txt, File b.txt,File c.txt
subfolder Directory File d.txt, File e.txt

#!/bin/bash
(( $# < 1 )) && echo "param is zero!" && exit 1
[ ! -d $1 ] && echo "$1 not path" && exit 1
dir=$1
dir_p="$dir Directory :"
cd $dir
dir=`pwd`
for i in `ls $dir`
do
    if [ -d $i ]; then
        /tmp/sh/dir_file $i            #我的脚本文件在/tmp/sh中,需要改一下这里
    else
        dir_p="$dir_p File $i"
    fi
done
cd ..
echo $dir_p


实验结果:

[root@localhost sh]# ./dir_file /tmp/python/

python_2 Directory : File 1.log File 2.log

python_3 Directory : File 3.log

/tmp/python/ Directory : File p File t.py File y.py


这样应该可以吧,试试看

温馨提示:内容为网友见解,仅供参考
第1个回答  推荐于2017-09-06
travel_file()
{
    cd $1
    
    for file in `ls`
    do
        if [ -d $file ]
        then
            echo "" >> $filepath/ret.txt
            echo -e $file Directory "\c" >> $filepath/ret.txt
        fi
        if [ -f $file ]
        then
            echo -e File $file "\c" >> $filepath/ret.txt
            
        fi
        
        
        if [ -d $file ]
        then
            
            travel_file $file
        fi
        
    done
}
if [ $# -lt 1 ]
then
    echo "输入文件目录"
    exit -1
fi
 
if [ ! -d $1 ]
then
    echo "$1 不是目录"
    exit -1
fi
filepath=`pwd`
if [ -f $filepath/ret.txt ]
then
    rm $filepath/ret.txt
fi
travel_file $1

文件名称 file.sh 执行命令 file.sh  test  
生成文件格式
folder1 Directory File a.txt File b.txt File c.txt 
subfolder Directory File d.txt File e.txt

本回答被提问者采纳
第2个回答  推荐于2017-09-14

Shell脚本遍历目录并批量修改文件并保存,有两种实现代码;

编写脚本文件实现:使用函数循环调用

#!/bin/bash
#
#
SPATH="/root/chengji/WebRoot"
DPATH="/web"
# 函数开始部分
CYCLING(){ 
  filelist=`ls -1 $SPATH` 
for filename in $filelist ; do
if [ -f $filename ] ; then  
        echo Filename:$filename 
        /usr/bin/iconv -f GBK -t UTF-8  $SPATH/$filename -o  $DPATH/$filename 
        #cp -pv $SPATH/$filename  $DPATH/$filename 该句为前期便利效果测试
        sed  -i  -e  's/gb2312/UTF-8/g'  -e 's/GB2312/UTF-8/g'  $DPATH/$filename 
    elif [ -d $filename ] ; then 
        DPATH=$DPATH/$filename 
        mkdir -pv $DPATH 
        cd $filename 
        SPATH=`pwd` 
    # Next for recurse 如果遇到目录进行自我调用。。。实现深层遍历
        CYCLING 
    # Next Usag: basename dirname
        DPATH=`dirname $DPATH` 
        SPATH=`dirname $SPATH` 
        cd $SPATH 
else
        echo "File $SPATH/$filename is not a common file.Please check."
    fi 
  done 

 # 命令开始部分
cd $SPATH 
CYCLING 
echo "All Done."

不使用函数循环调用:

#/bin/bash 
#Auth: Mo 
#Desc: 
#
SPATH="/root/chengji"
DIR=WebRoot 
DPATH="/web"
 find ${DIR}   -type d  -exec mkdir -pv ${DPATH}/{}  \;    
 find ${DIR}  -type f -exec  iconv -f GBK -t UTF-8  {} -o  ${DPATH/{}  \;  
echo "The file Next Listed is not a common file or directory ,please check."
find  ${DIR}  ! -type f  -a  ! -type d -ecec  ls -l {} \;  
find  $DPATH -type f -exec sed  -i  -e  's/gb2312/UTF-8/g'  -e 's/GB2312/UTF-8/g'  {} \; 
echo ' '
echo "All Done."

第3个回答  2013-11-06
这样?
脚本:
#!/bin/bash
a=`ls folder1/*.txt |awk -F'/' '{print$NF}'`
echo 'folder1 Directory File '$a

b=`ls folder1/subfolder/*.txt |awk -F'/' '{print$NF}'`
echo 'subfolder Directory File '$b

结果:
sh test.sh
folder1 Directory File a.txt b.txt c.txt
subfolder Directory File d.txt e.txt
第4个回答  2013-11-06

1.  tree command


2. 

fun_call_back()
{
    for i in `ls`;
    do
        if [ -d $i ];then
            echo "folder $i"
            cd $i



            cd ../
        fi
        echo $i
    done
}

追问

实验了一下,是不是递归调用函数方法呀?可是最核心的那段算法呢?

追答cd $i
fun_call_back

中间这段让度娘给吃了?

追问

可是输出格式不对呀,我自己实验了尝试字符串累加等,都没有成功按格式输出

追答

其实重点是这样的

echo -n

让echo不显示回车,然后

done后面需要显示回车

另外函数入口要输入tab缩进,这个就当留给你的乐趣了

相似回答