Linux: Bash Shell Script To Move/Archive Old Files

# create a script to move files (notice the regex escape for special chars)
sudo cat << EOF > /usr/local/sbin/movefiles
#!/bin/bash
sourceDirectory="\$1"
moveTo="\$2"
olderThan="\$3"
find \$sourceDirectory -name "*"  -type f -mtime +\$olderThan -print0 | while IFS= read -r -d '' file; do mv \$file \$moveTo; done
EOF

# Mark the file with executable permissions
sudo chmod +x /usr/local/sbin/movefiles

# Create a new entry in cron
sudo crontab -e
 
# Insert these new lines
0 2 * * * /usr/local/sbin/movefiles /home/path/tosource/ /path/todestination/ 30 > /dev/null

Leave a Reply

Your email address will not be published. Required fields are marked *