gpt4 book ai didi

linux - Ubuntu/Linux 庆典 : traverse directory and subdirectories to work with files

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:49:52 24 4
gpt4 key购买 nike

让我从我需要的开始;该程序被赋予一个目录,然后它将检查目录中的所有文件(工作)并对文件执行操作(等待它可以找到这部分的所有文件)。然后它将查找子目录并为每个子目录重新运行它自己。

我正在测试的目录如下所示:

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>

文件夹和文件的名称中确实包含空格

输出是:

$ ./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

程序如下:

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

最佳答案

您有什么理由不能为此使用查找?将您想要的每个文件操作粘贴到它自己的脚本中(我在下面将其称为 dostufftomyfile.sh),然后执行:

find $dir -type f -print0 | xargs -0 dostufftomyfile.sh

将 $dir 替换为您将从中搜索的顶级目录...

编辑添加...当您编写 shell 脚本时,请确保将 $@ 放在双引号中...例如,您希望您的 dostufftomyfile.sh 脚本具有以下结构:

#!/bin/sh
for f in "$@"
do
echo "Processing file: $f"
# Do something to file $f
done

如果你不引用 $@ 那么文件名中的空格将被忽略(我怀疑你不会想要的):-)

关于linux - Ubuntu/Linux 庆典 : traverse directory and subdirectories to work with files,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3954458/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com