Need help in writing a script to move old files...
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Need help in writing a script to move old files...
hey guys i could really use a hand. i'm taking a Unix class at school and I need to write a shell script that will read the devices and when the memory is low will take the oldest files and move them to a new device or server...I'm so lost somebody please help. The script can be scheduled in crontab to execute when the system is not being used.
Per the LQ Rules, please do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and Google searches) and we'll do our best to help. Also, keep in mind that your instructor might also be an LQ member.
This is what I have so far...from what i understand this will set the alert level to 90 percent and if the storage space is at that mark it will search for files older then 60 days, but now i need to know how to move them to another device so clear space...
Excellent. You're almost there. If you would have searched LQ for "find exec" you'd have found at least three recent threads on the topic: 'find /var/log -mtime +60 -type f -exec echo mv {} /some/dir \;'. Remove the 'echo' and replace /shome/dir when you've verified this is what you want. And welcome to LQ BTW.
#!/bin/sh --
# Enable for debugging purposes
# set -xev
# Naming variables is important for understanding the script.
# While I don't alsways adhere either they should be in caps not lower or camelcase.
THRESHOLD=90
# Awk pick only /dev/hd? and /dev/sd/?
# And you don't want the partition but the mountpoint name, easier.
df -H | awk '/\/dev\/[h,s]d/ { print $5, $NF }' | while read USAGE MOUNTPOINT; do
# Strip out the percentage char.
if [ ${USAGE//%/} -ge $THRESHOLD ]; then
# Moving files can be a bad thing, so logging it helps troubleshooting and
# moving them to their own subdir at least gives you a chance to recover them.
logger -t kern.crit "Moving stale files on $MOUNTPOINT to /oldfiles/dir/$MOUNTPOINT."
# If the subdir doesn't exist we should create it. If it somehow fails we shouldn't try and continue.
[ -d /oldfiles/dir/$MOUNTPOINT ] || { mkdir -p /oldfiles/dir/$MOUNTPOINT || exit 127; }
# If it somehow fails we shouldn't try and continue either.
find $MOUNTPOINT -mtime +60 -type f -exec mv -f {} -t /oldfiles/dir/$MOUNTPOINT || exit 127
fi
done
# A script should be terminated with the "all is well that ends well" exit line:
exit 0
* To improve this think of 0) handing off the actual find-and-move action to 'at' (echo 'some_action'|at some_timestamp ) because it'll execute in the background allowing you to process the next partition, 1) check file usage (fuser, lsof) before moving and 2) preserving (extended) attributes.
You're better off using 'df -P' (POSIX format output) in scripts. On my system if you use 'df -H' it can split the output over multiple lines which breaks the parsing of the output you were using. e.g.
Another thing you want to do is make sure to use the -xdev option on the 'find' command to restrict the processing to a single filesystem. Without it, you could end up processing filesystems that you shouldn't be.
I had a play with writing a solution for this last night out of curiosity, here's what I came up with. I don't see any harm in posting it now that unSpawn has already posted one solution.
DISCLAIMER: The following script is an example for educational purposes only. If you run it and it deletes all your files or otherwise screws up your system, don't come crying to me. READ IT CAREFULLY BEFORE USE, AND USE AT YOUR OWN RISK!
Code:
#!/bin/bash
# Filesystems to monitor for migration to archive:
# Specify Filesystem mountpoints only, not subdirectories.
# Note: Do NOT include trailing '/'
MONITOR_LIST='/srv/files /srv/music /srv/backup'
# Filesystem Percentage Threshold to trigger migration processing:
# Note: Do NOT include % sign
THRESHOLD=90
# Location of the Archive filesystem to migrate qualifying files to:
# Note: Do NOT include trailing '/'
ARCHIVE='/archive'
# Number of days old the file has to be to qualify for migration:
AGE=60
while read fsdev blocks used available capacity mount
do
capacity=${capacity%\%} # strip % sign
if [ "$capacity" -ge "$THRESHOLD" ] ; then
echo "# Filesystem: ${mount} at ${capacity}, ABOVE THRESHOLD!"
while read filename
do
directory=$(dirname "$filename")
mkdir -p "${ARCHIVE}${directory}" && \
mv "${filename}" "${ARCHIVE}${filename}"
done < <(find ${mount} -xdev -type f -mtime +${AGE} -print)
fi
done < <(df -P ${MONITOR_LIST} | tail +2 )
exit 0
# end
I took a slightly different approach to unSpawn. Hopefully, between us, we've given you a few ideas to use in your own scripts.
Anyway, Hope that was of value. Have fun scripting...
You're better off using 'df -P' (POSIX format output) in scripts. On my system if you use 'df -H' it can split the output over multiple lines which breaks the parsing of the output you were using. e.g.
Is that screen representation or has 'df' some cut-off at which it really adds a newline?
Code:
]$ df -H | grep bindfs | while read s; do echo "${#s}"; done
105
]$ df -H | grep bindfs
bindfs 21G 5.1G 15G 26% /var
/tmp/some/ridiculously/no/ludicrously/weird/mountpo
int
]$
The above looks crooked but really is one line.
Regardless you're absolutely right about POSIX format output.
Is that screen representation or has 'df' some cut-off at which it really adds a newline?
It appears that without the (-P) option, if the filesystem device is over a certain length, df adds a \n after the filesystem device and splits the statistics onto a second line. I only discovered this myself when I was playing with the OP's grep|awk|while|read line.
I suspect that unless you're using something like LVM, you're not likely to encounter this problem as the device names are usually short enough to not trigger this behaviour.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.