本文介绍了Ubuntu的/ Linux的庆典:遍历目录及子目录进行文件操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我开始与我的需要;该计划给出一个目录,它将然后检查目录(作品)的所有文件和做的东西到文件(等待,直到它可以找到这部分的所有文件)。那么它会寻找子目录,然后重新运行它对于每个子目录的自我。

我与这个样子的测试目录:

 桌面/ test_files /(起始目录)
台式机/ test_files /文件夹1 /
台式机/ test_files /文件夹1 GT;文件夹2 /
台式机/ test_files /文件夹1 GT;文件夹2 /<文件,20个左右的&GT​​;
台式机/ test_files /文件夹3 /
台式机/ test_files /文件夹3 /<更多的文件,20个左右的&GT​​;

文件夹和文件确实包含在名称中使用空格

的输出是:

  $ ./x007_shorter.sh桌面/ test_files /桌面/ test_files /文件夹1/
桌面/ test_files /文件夹1 /文件夹2/
LS:不能访问* /:没有这样的文件或目录
桌面/ test_files /文件夹1 /文件夹2 /文件夹中3/
./x007_shorter.sh:4号线:CD:./folder 3 /:没有这样的文件或目录
LS:不能访问* /:没有这样的文件或目录

下面是程序:

 #!/斌/庆典
功能findir {
    NEWDIR = $ 1
    EVAL CD $ NEWDIR
    ARRAY =($(LS-D * /))
    声明-a dirY中
    数= 0
    a = 0时
    而[$一个-lt $ {#ARRAY [@]}];做
        diry的[$计数] =$ {ARRAY [$一]}
        无空间=真
        而[真]做
            如果[[$ {diry的[$计数]} == * / *];然后
                如果[$ =无空间假]。然后
                diry的[$计数] =$ {diry的[$计数]:0:(($ {#diry的[$计数]} - 1))} \\/
                科幻
                打破
                无空间=真
            科幻
            让一= $ A + 1
            如果[$ =无空间假]。然后
                diry的[$计数] =$ {diry的[$计数]} $ {ARRAY [$一]}
            其他
                diry的[$计数] =\\$ {diry的[$计数]} $ {ARRAY [$一]}
            科幻
            无空间= FALSE
        DONE
        让数= $计数+ 1
        让一= $ A + 1
    DONE
    对于在'序列1 $ {#diry的[@]}`;做
        EVAL光盘。$ NEWDIR
#清单$ {diry的[($ A-1)]}
        其中,=`pwd`
#EVAL CD $ NEWDIR
        #findir$ {diry的[($ A-1)]}
        #findir$其中$ {diry的[($ A-1)]:1}
        #Right选项赢了,回声$ {diry的[($ A-1)]} $ VS哪里/ $ {diry的[($ A-1)]}
        回声$哪里/ $ {diry的[($ A-1)]}
        findir./${diry[($a-1)]}
    DONE
}
功能列表{
    input_file_directory = $ 1
    EVAL CD $ input_file_directory
    ARRAY =($(找到。-maxdepth 1型的F -print))
    声明-a文件
    数= 0
    a = 0时
    而[$一个-lt $ {#ARRAY [@]}];做
        文件[$计数] =$ {ARRAY [$一]}
        而[真]做
            如果[[$ {ARRAY [(($一个+ 1))]} == ./*]];然后
                打破
            科幻
            如果[[$ {ARRAY [(($ A + 1))]}==]];然后
                打破
            科幻
            让一= $ A + 1
            文件[$计数] =$ {文件[$计数]} $ {ARRAY [$一]}
        DONE
        让数= $计数+ 1
        让一= $ A + 1
    DONE
    其中,=`pwd`
    对于在'序列1 $ {#文件[@]}`;做
        回声$其中$ {文件[($ A-1)]:1}
        #going在每个文件的工作,只是呼应文件,直到列出所有文件
    DONE
}明确
DAR =
如果[[$ 1 =]];然后
    阅读-p请输入一个目录,我扫描NEWDIR
    DAR = $ NEWDIR
    名单$ NEWDIR
    findir $ NEWDIR
其他
    DAR = $ 1
    表$ 1
    findir $ 1
科幻


解决方案

克里斯·J的答案是做的事情,如果你可以把每个文件的东西,在一个单独的命令(/脚本)preferred方式。如果你想在一个脚本中的一切,我最喜欢的咒语是这样的:

 而IFS =读-r -d $'\\ 000'文件<及3;做
    仅在Python标准$文件
做3'; ≤(-x找到$目录-mindepth 1型的F -print0)

请参阅并的的解释和一些其他的选择。请注意,在bash这只是工程(即脚本的必须的开头#!/斌/ bash)的。此外,处理指定目录中的内容按字母顺序,而不是文件 - 之前的子目录中。

如果你真的想步执行文件手动(即获得通过遍历顺序更多的控制),这里就是我想要做的:

 #!/斌/庆典process_dir(){
    当地-a子目录=()
    回声扫描目录:$ 1    #扫描目录,处理文件和收集子目录
    在$ 1文件/ *;做
        如果[[-f$文件]];然后
            回声处理文件:$文件
            #其实这里处理文件...
        ELIF [-d$文件]];然后
            子目录+ =($文件)
            #如果你不关心子文件夹之前处理所有的文件,只是做:
            #process_dir$文件
        科幻
    DONE    #现在经过子目录
    在$ {子目录[@]}D;做
        process_dir$ D
    DONE
}明确
如果[[-z$ 1]];然后
    阅读-p请输入一个目录,我扫描DIR
其他
    DIR =$ 1
科幻
process_dir$目录

let me start off with what I need; the program is given a directory, it will then examine all the files in the directory (works) and do stuff to the files (waiting till it can find all the files for this part). then it will look for subdirectories and re-run its self for each subdirectory.

the directory I'm testing with looks like this:

desktop/test_files/ (starting directory)
desktop/test_files/folder 1/
desktop/test_files/folder 1>folder 2/
desktop/test_files/folder 1>folder 2/<files, 20 or so>
desktop/test_files/folder 3/
desktop/test_files/folder 3/<more files, 20 or so>

folders and files do contain spaces in the names

the output is:

$ ./x007_shorter.sh Desktop/test_files/

Desktop/test_files/"folder 1"/
Desktop/test_files/folder 1/"folder 2"/
ls: cannot access */: No such file or directory
Desktop/test_files/folder 1/folder 2/"folder 3"/
./x007_shorter.sh: line 4: cd: ./folder 3/: No such file or directory
ls: cannot access */: No such file or directory

here is the program:

#!/bin/bash
function findir {
    newDir=$1
    eval cd $newDir
    ARRAY=( $(ls -d */) )
    declare -a diry
    count=0
    a=0
    while [ $a -lt ${#ARRAY[@]} ]; do
        diry[$count]="${ARRAY[$a]}"
        noSpace=true
        while [ true ]; do
            if [[ ${diry[$count]} == */* ]] ; then
                if [ $noSpace = false ]; then
                diry[$count]="${diry[$count]:0:((${#diry[$count]}-1))}\"/"
                fi
                break
                noSpace=true
            fi
            let "a=$a+1"
            if [ $noSpace = false ]; then
                diry[$count]="${diry[$count]} ${ARRAY[$a]}"
            else
                diry[$count]="\"${diry[$count]} ${ARRAY[$a]}"
            fi
            noSpace=false
        done
        let "count=$count+1"
        let "a=$a+1"
    done
    for a in `seq 1 ${#diry[@]}`; do
        eval cd .$newDir
#        list "${diry[($a-1)]}"
        where=`pwd`
#        eval cd $newDir
        #findir "${diry[($a-1)]}"
        #findir "$where${diry[($a-1)]:1}"
        #Right option won,  echo "${diry[($a-1)]} Vs $where/${diry[($a-1)]}"
        echo "$where/${diry[($a-1)]}"
        findir "./${diry[($a-1)]}"
    done
}
function list {
    input_file_directory=$1
    eval cd $input_file_directory
    ARRAY=( $(find . -maxdepth 1 -type f -print) )
    declare -a files
    count=0
    a=0
    while [ $a -lt ${#ARRAY[@]} ]; do
        files[$count]="${ARRAY[$a]}"
        while [ true ]; do
            if [[ ${ARRAY[(($a+1))]} == ./* ]] ; then
                break
            fi
            if [[ "${ARRAY[(($a+1))]}" == "" ]] ; then
                break
            fi
            let "a=$a+1"
            files[$count]="${files[$count]} ${ARRAY[$a]}"
        done
        let "count=$count+1"
        let "a=$a+1"
    done
    where=`pwd`
    for a in `seq 1 ${#files[@]}`; do
        echo "$where${files[($a-1)]:1}"
        #going to work on each file, just echoing file till lists all files
    done
}

clear
dar=""
if [[ $1 = "" ]]; then
    read -p "Please enter a directory for me to scan" newdir
    dar=$newdir
    list $newdir
    findir $newdir
else
    dar=$1
    list $1
    findir $1
fi
解决方案

Chris J's answer is the preferred way to do things if you can put the per-file stuff in a separate command(/script). If you want everything in a single script, my favorite incantation is something like this:

while IFS="" read -r -d $'\000' file <&3; do
    dostuffwith "$file"
done 3< <(find -x  "$dir" -mindepth 1 -type f -print0)

See BashFAQ #20 and #89 for explanations and some other options. Note that this only works in bash (i.e. the script must start with #!/bin/bash). Also, it processes the contents of a given directory in alphabetic order, rather than files-before-subdirectories.

If you really want to step through the files "by hand" (i.e. to get more control over the traversal order), here's how I'd do it:

#!/bin/bash

process_dir() {
    local -a subdirs=()
    echo "Scanning directory: $1"

    # Scan the directory, processing files and collecting subdirs
    for file in "$1"/*; do
        if [[ -f "$file" ]]; then
            echo "Processing file: $file"
            # actually deal with the file here...
        elif [[ -d "$file" ]]; then
            subdirs+=("$file")
            # If you don't care about processing all files before subfolders, just do:
            # process_dir "$file"
        fi
    done

    # Now go through the subdirs
    for d in "${subdirs[@]}"; do
        process_dir "$d"
    done
}

clear
if [[ -z "$1" ]]; then
    read -p "Please enter a directory for me to scan " dir
else
    dir="$1"
fi
process_dir "$dir"

这篇关于Ubuntu的/ Linux的庆典:遍历目录及子目录进行文件操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 22:37