Bash 找到子文件夹中的所有 zip 文件,将文件及其路径绑定到循环中并执行一些任务
Bash find all zip files within subfolders, bind files and their path into loop and perform some tasks
我需要在 Bash 上编写脚本,它将找到子文件夹中的所有 zip 文件,将文件及其路径绑定到某个文件中,然后遍历此列表并对所有 zip 文件执行一些任务(例如解压缩,检查 zip 中的文件,然后删除解压缩的文件)。
一些想法:
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo $DIR ##current dir
ls $DIR\*.zip
然后将结果绑定到文件(例如ziplist.txt)
然后按字符串循环读取此文件:
if [[ some result ]] ; then
while IFS= read -r ; do
done <$DIR/ziplist.txt
如何以最佳方式做到这一点?抱歉,我对 bash 的经验有限。
您可以通过多种方式使用查找来执行此操作。
"Your way" 将创建一个临时文件并循环执行此操作:
find /your/path -name '*.zip' > /tmp/zips
如果你不一定需要收集文件,只是想在找到它们时对它们执行任务,你可以使用 find 的 exec:
find /your/path -name '*.zip' -exec /path/to/your/worker/script.sh {} \;
它将对它找到的每个 zip 文件执行你的 script.sh,并将完整的 zip 文件路径作为参数。
希望这对您有所帮助。
这应该可以解决问题:
for filename in $(find . -name '*.zip'); do
# Your operations here
done
如果您想继续使用一段时间,您可以这样做:
while IFS= read -r ; do
done < <(find . -name '*.zip')
我需要在 Bash 上编写脚本,它将找到子文件夹中的所有 zip 文件,将文件及其路径绑定到某个文件中,然后遍历此列表并对所有 zip 文件执行一些任务(例如解压缩,检查 zip 中的文件,然后删除解压缩的文件)。
一些想法:
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo $DIR ##current dir
ls $DIR\*.zip
然后将结果绑定到文件(例如ziplist.txt) 然后按字符串循环读取此文件:
if [[ some result ]] ; then
while IFS= read -r ; do
done <$DIR/ziplist.txt
如何以最佳方式做到这一点?抱歉,我对 bash 的经验有限。
您可以通过多种方式使用查找来执行此操作。
"Your way" 将创建一个临时文件并循环执行此操作:
find /your/path -name '*.zip' > /tmp/zips
如果你不一定需要收集文件,只是想在找到它们时对它们执行任务,你可以使用 find 的 exec:
find /your/path -name '*.zip' -exec /path/to/your/worker/script.sh {} \;
它将对它找到的每个 zip 文件执行你的 script.sh,并将完整的 zip 文件路径作为参数。
希望这对您有所帮助。
这应该可以解决问题:
for filename in $(find . -name '*.zip'); do
# Your operations here
done
如果您想继续使用一段时间,您可以这样做:
while IFS= read -r ; do
done < <(find . -name '*.zip')