0

そのため、フォルダー内のファイルの変更日を比較したいと考えています。-nt または -ot と比較できることは知っていますが、ファイルを走査して比較する方法がわかりません。ファイルを前のファイルに割り当てる必要があることは知っていますが、そのコードはわかりません。

たとえば、a、b、c の 3 つのファイルを含むフォルダーがあるとします。for ループで、a (前のエントリ) と b (エントリ) を比較したい。a が b より新しい場合、b を削除します。等々。

「前のエントリ」を割り当てる方法を理解しようとしています。

よろしくお願いします!

echo "Which directory would you like to clean?"
read directory
echo "Are you sure you want to delete old back ups? Y for yes"
read decision
if [ $decision = "y" ]
then
for entry in "$directory"/*

do
#need to somehow assign the previous entry and current entry to a variable
if [ $entry -nt $previousEntry ]
rm -i $previousEntry
echo "Deleted $previousEntry"
fi
done
echo "Deleted all old files"

else
echo "Exiting"
exit 1
fi
4

2 に答える 2

0

理解した。2 つのネストされたforループ。ありがとうございます。

echo "Which directory would you like to clean?"
read directory
echo "Are you sure you want to delete old back ups? Y for yes"
read decision
if [ $decision = "y" ]
then

# Beginning of outer loop.
for entry in "$directory"/*
do
  # Beginning of inner loop.
  for previousEntry in "$directory"/*

  do
if [[ $entry -nt $previousEntry ]] #nt = newer than
then
echo "$entry is newer than $previousEntry"
echo "Deleting $previousEntry"
rm -i $previousEntry
fi 
  done
  # End of inner loop.

done

fi #end first if
于 2013-05-06T08:12:58.860 に答える