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

How to Install Visual Studio Code on Linux Mint 20.04

Linux Mint versions earlier than 20.04 would allow a user to install snapd, prior to…

Resolve Windows 2003 Logon Screen Blackout Problems

The problem:Administrators are unable to logon to a Windows 2003 Server. This is what they…

PowerShell: Remove IP Address Assignment Using Bluecat API

$bluecatUri='http://bluecat.kimconnect.com/Services/API' $bluecatUsername='svc-bluecat-api' $bluecatPassword='PASSWORD' $configId=17 $ipv4Address='10.10.162.54' $marker='toBeDeleted-' function confirmation($content,$testValue="I confirm",$maxAttempts=3){ $confirmed=$false; $attempts=0; $content|write-host write-host "Please review…