#!/bin/bash # This script tries to delete old or same files in the current directory (and in all the subdirectories in it), comparing to a directory (and in all the subdirectories in it) which is input. # This script first tries to find all the files in the current directory (and in all the subdirectories in it). # The directory containing each found file, which is called "file A" in this document, is called "directory B" in this document. # The input directory, which should be compared, is called "directory C" in this document. # If there is a corresponding file, which is called "file D", in a corresponding directory, which is called "directory E" in this document, in the directory C, and if the file A is not newer than the file D, the file A is deleted. # If there is the file D in the directory E in the directory C, and if the file A is newer than the file D, it is reported. # If there is not the directory E in the directory C, it is reported. # If there is a file whose name is same as the directory E, it is reported. # If there is not the file D, it is reported. # Below is the same explanation for this script written in Japanese. # このスクリプトを実行すると、カレントディレクトリとそのサブディレクトリ全ての中に存在するファイル(それぞれを以下、Aと記す。Aを含むディレクトリを以下、Bと記す。)について、 # refdir変数に代入されたディレクトリ(以下、C) # の中にAと同様のファイル(D)が存在するかを調べる。 # Dがあって、Dの方がAよりも新しければ、Aを削除する。 # Dがあって、Dの方がAよりも古ければ、それを報告する。 # Cの中のDが存在するはずのディレクトリをEとする。 # EがCに無ければ、それを報告する。 # Eと同名のファイルがCにあれば、それを報告する。 # Dが無ければ、それを報告する。 echo "Enter the directory path which should be compared to the current directory" read refdir find . -type f|while read file do dirname1=$(dirname $file) if [ ! -e $refdir/$dirname1 ];then echo "$refdir/$dirname1 which should be a directory does not exist" # mkdir -p $refdir/$dirname1 elif [ ! -d $refdir/$dirname1 ];then echo "$refdir/$dirname1 is not a directory, but a file" fi if [ -e $refdir/$file ];then if [ $file -nt $refdir/$file ];then echo "$refdir/$file which is older than $file" # cp -auvi $file $refdir/${dirname1}/ else rm $file fi else echo "$refdir/$file which should be a file does not exist" fi done