ディレクトリのコピーではなく、ディレクトリ内のすべてのファイルを削除する

ディレクトリのコピーではなく、ディレクトリ内のすべてのファイルを削除する

次のフォルダ構造があります

foo/images
foo/infos

infosには、foo / imagesフォルダの画像を記述するxmlファイルのコレクションが含まれています。命名規則は次のとおりです。イメージは image1.png、記述子は image1.xml です。私が解決しようとしている問題は次のとおりです。関連記述子を持たないすべての画像を削除します。

私はこれのためにしばらく大きな成功を収めたemacsを使用してきました。ただし、emacsがインストールされていないシステムでこれを行う必要があり、bashが唯一のオプションです。

ベストアンサー1

#!/bin/bash

infosdir="foo/infos"
imagesdir="foo/images"

# use a different IFS to allow spaces in filenames
IFS=$(echo -en "\n\b")

# for all images in the images-directory
for pngfullname in ${imagesdir}/*.png; do
 # get the name of the image without the path and without the file-extension
 basename="$(basename ${pngfullname} .png)"
 # if an appropriate xml-file in the infos-directory is missing
 if [ ! -f "${infosdir}/${basename}.xml" ] ; then
   # delete the image
   rm "${pngfullname}"
 fi
done

おすすめ記事