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: Update a List of Multiple Windows Machines

Version 3: # updateWindowsList.ps1 # Version 0.0.3 # # Features: # - New feature over…

PowerShell: Excute Function Remotely

$computername=$env:computerName $adminUsername='doeMainAdmin' $adminPassword='frackingPassword' $adminCredential=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminUsername,$(ConvertTo-securestring $adminPassword -AsPlainText -Force) function execFunctionRemotely{ param( [Parameter(Mandatory=$true)][string]$computerName,…

Disable Screensaver on CentOS 8

Here are a couple of tricks that worked in the past. I don't think they…