首先,在/tmp/test
文件路径中我有以下目录
amb
bmb
cmb
通过运行find
命令,我在这三个目录中获得了以下文件列表:
amb/eng/canon.amb
bmb/eng/case.bmb
cmb/eng/hint.cmb
list1
我正在尝试使用for
循环并根据文件类型从中获取每个文件,*.amb
或者应该执行特定的 IF *.bmb
:*.cmb
cd /tmp/test
find */ -type f -exec ls {} \; > /tmp/test/list1
for file in `cat /tmp/test/list1`
do
if [ -f *.amb ]
then
sed "s/amb/amx/g" /tmp/test/list1 > /tmp/test/list2
ls /tmp/test/list2 >> /tmp/test/finallist
fi
if [ -f *.bmb ]
then
sed "s/bmb/bmx/g" /tmp/test/list1 > /tmp/test/list2
ls /tmp/test/list2 >> /tmp/test/finallist
fi
if [ -f *.cmb ]
then
sed "s/cmb/cmx/g" /tmp/test/list1 > /tmp/test/list2
ls /tmp/test/list2 >> /tmp/test/finallist
fi
done
echo "*********************"
echo -e "\nFinal list of files after replacing from tmp area"
felist=`cat /tmp/test/finallist`
echo -e "\nfefiles_list=`echo $felist`"
所以我的最终输出应该是:
amx/eng/canon.amx
bmx/eng/case.bmx
cmx/eng/hint.cmx
我认为您正在尝试根据文件后缀应用不同的操作:
顺便,
find */ -type f -exec ls {} \; > /tmp/test/list1
最好写成find */ -type f -print > /tmp/test/list1
, 并且因为你已经用linux标记了,而不是进一步减少到find */ -type f > /tmp/test/list1
. 但这会破坏奇怪的(但合法的)文件名。$( … )
。但即使这样也会破坏包含空格和其他特殊字符的文件名。使用 else if,也就是 bash 中的 elif,例如: