I have two directories with files with the same name. Basically I want to replace dir1/file.txt with dir2/file.txt if the creation date of dir1/file.txt is more recent. But i think I'm missing something in the if condition.
#!/bin/bash
for i in /dir1/*; do
nameFIRR=$(basename "$i")
dateINPUT="$(date -r $i)"
dateOUTPUT="$(date -r /dir2/$nameFIRR)"
if [ $dateINPUT -ge $dateOUTPUT ];
then
cp -u $i /dir2/$nameFIRR
fi
done
if [ $nameFIRR -nt /dir2/$nameFIRR ]cp's-uoption you don't need all this!cp -u dir1/* dir2...date -rand alsotest -ntandcp -uuse the last modification time of the file(s), which may or may not be the same as creation time. Unix utilities rarely support creation time because original Unix filesystems didn't store it, and even today only some do.