#!/bin/bash # This is version 2 made at 2016/8/27 # 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") realpath1=$(realpath "$refdir/$file") if [ ! -e "$refdir/$dirname1" ];then echo "$refdir/$dirname1 which should be a directory does not exist" elif [ ! -d "$refdir/$dirname1" ];then echo "$refdir/$dirname1 is not a directory, but a file" elif [ -e "$realpath1" ];then if [ "$(realpath "$file")" = "$realpath1" ];then echo "$file is the destination of the symbolic link $refdir/$file = $realpath1" elif [ "$file" -nt "$realpath1" ];then echo "$refdir/$file which is older than $file" else rm $file fi elif [ "$refdir/$file" = "$realpath1" ];then echo "$refdir/$file which should be a file does not exist" else echo "$refdir/$file = $realpath1 which should be a file does not exist" fi done