What exactly do you mean by the age of a directory? Time since the creation of its inode? I suggest you use find
to identify the directories you want to move. E.g. like this:
#!/bin/bash
res=0
cd /disk1/data/
find -type d -ctime +1 -print0 -prune | \
while IFS= read -r -d '' i; do
if [[ ${i} == */* ]]; then # need to ensure subdir exists
if ! mkdir -p "/disk2/data/${i%/*}; then # error creating dir?
res=1
continue
fi
fi
rm -rf "/disk2/data/${i}" # make sure target does not exist
mv "${i}" "/disk2/data/${i}" # move stuff
done
exit ${res}
If other people were allowed to write disk2
, then this code might be vulnerable to some kinds of symlink attacks if someone creates the right symlinks fast enough. Not sure.
I suggest you write the script to some file and execute that from the cronjob. You create a conjob with crontab -e
(make sure you've set your EDITOR
environment variable appropriately), following the format described in man crontab
.