递归查找文件具有特定扩展名 [英] Recursively look for files with a specific extension
问题描述
我试图找到在目录中的特定扩展名,并将所有文件及其子目录与我的bash(最新的Ubuntu LTS发行版)。
这是什么写在一个脚本文件:
#!/斌/庆典目录=/家/翻盖/桌面
后缀=中browsefolders()
因为我在$ 1/ *;
做
回声导演:$目录
回声文件名:$ I
#回声$ {I#*。}
延长=`回声$ I|切-d'。 -f2`
回声Erweiterung $扩展
如果[-f$ I];然后 如果[$扩展== $后缀]然后
回声$ I $与结尾中 其他
回声$我不以$结尾
科幻
ELIF [-d$ I];然后
browsefolders$ I
科幻
DONE
}
browsefolders$目录
不幸的是,当我开始在终端这个脚本,它说:
[:29:在:运营商意外
(用 $扩展
而不是'在'
)
这是怎么回事,哪来的错误?
但这个大括号
找到$目录型的F -name*。在
比整个事情(更安全 - 涉及的文件名和目录名空格)短了一点。
您的脚本可能失败的不具有条目的。
在他们的名字,使得 $扩展
空的。
I'm trying to find all files with a specific extension in a directory and its subdirectories with my bash (Latest Ubuntu LTS Release).
This is what's written in a script file:
#!/bin/bash
directory="/home/flip/Desktop"
suffix="in"
browsefolders ()
for i in "$1"/*;
do
echo "dir :$directory"
echo "filename: $i"
# echo ${i#*.}
extension=`echo "$i" | cut -d'.' -f2`
echo "Erweiterung $extension"
if [ -f "$i" ]; then
if [ $extension == $suffix ]; then
echo "$i ends with $in"
else
echo "$i does NOT end with $in"
fi
elif [ -d "$i" ]; then
browsefolders "$i"
fi
done
}
browsefolders "$directory"
Unfortunately, when I start this script in terminal, it says:
[: 29: in: unexpected operator
(with $extension
instead of 'in'
)
What's going on here, where's the error? But this curly brace
find $directory -type f -name "*.in"
is a bit shorter than that whole thing (and safer - deals with whitespace in filenames and directory names).
Your script is probably failing for entries that don't have a .
in their name, making $extension
empty.
这篇关于递归查找文件具有特定扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!