首页 > 解决方案 > 当文件 xz 存在时,删除所有 xy 形式的文件

问题描述

我想删除所有名为foo .o 的文件,仅当存在相应的foo .asm 文件时,对于所有foo

我可以用这样的for循环来做到这一点:

for asmfile in *.asm; do
    prefix=${asmfile%".asm"}
    echo "Processing $asmfile ($prefix)"
    rm -f "${prefix}.o"
done

...但是 bash 中是否有一些不那么尴尬或更惯用的东西?

标签: linuxbash

解决方案


您可以这样做来获取前缀:

 PREFIX=`basename $ASMFILE .asm`

推荐阅读