Posted On August 12, 2021

Linux: Bash Shell Script To Move/Archive Old Files

kimconnect 0 comments
blog.KimConnect.com >> Codes , Linux >> 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 *

Related Post

PowerShell RandomPassword Function

function randomPassword{    # return a reasonably randomized password that starts with "KimConnect:" plus 25…

How to Install Virtualbox in Ubuntu

Simply run these two commands: sudo add-apt-repository multiverse && sudo apt-get update sudo apt -y…

Recover Windows CD Key

Set WshShell = CreateObject("WScript.Shell")MsgBox ConvertToKey(WshShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DigitalProductId"))Function ConvertToKey(Key)Const KeyOffset = 52i = 28Chars = "BCDFGHJKMPQRTVWXY2346789"DoCur =…