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

2025-04-24 02:21:52
推荐回答(5个)
回答1:

#!/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


这样应该可以吧,试试看

回答2:

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

回答3:

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."

回答4:

这样?
脚本:
#!/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

回答5:

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
}