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.
It works great, now I want to add another command to the script to remove the directory that is 8 days old, so, that I have the current seven days directories.
I have looked at man pages date and expr they don't help.
I am working my way through Rute and it does not give me anything to work with yet.
I haven't found anything useful on google or ldp.org
I do not know the syntax to make this work. I know that some how I need to have the script calculate the date that is 8 days from the current date so that I would end up with it looking for a directory like this:
today = serverbackup101104
8 days back from today's date = serverbackup100404
then the script would remove serverbackup100404 and the contents of that directory.
I suppose this part of the script is going to be a little more complex than the part I am using now.
Will anyone point me in the right direction so I can learn how to do this?
serverbackup100204: 9 days old - Removing
serverbackup100304: 8 days old - Removing
serverbackup100404: 7 days old
serverbackup100504: 6 days old
serverbackup100604: 5 days old
serverbackup100704: 4 days old
serverbackup100804: 3 days old
serverbackup100904: 2 days old
serverbackup101004: 1 days old
serverbackup101104: 0 days old
Notice I left the "rm -rf $file" commented. You should throughly test this before actually enabling that. Placing a dynamic "rm" in a shell script makes me cringe with thoughts of $file somehow ending up being equal to "/" and then getting executed as root. There is somewhat of a sanity check in place though, since it makes sure the directroy starts with "serverbackup" before it would remove it, but you may still want to include a few more safety nets. Anyway, you should at least get an idea of how you can calculate the difference in the dates on end of the file name.
Edit:
Oops, that won't work when the month and/or year changes, sorry.
The find utility has time calculation features like modification time ( mtime ) and access time ( atime ) so you can search for files or directories which are older than or newer than the present time. Be careful when using the rm command, especially if you use it with -rf
#Delete old directories with the following command
find /home/user_name/serverbackup/* -type d -mtime +192 -exec rm -rf {} \;
#Delete old text files with the following command
find /home/user_name/serverbackup -type f -name '*.txt' -mtime +192 -exec rm {} \;
Here is a correction to what I did above, still using the directory name itself. You'll need to switch your date format to yymmdd though for it to work.
Code:
#!/bin/sh
cleanup()
{
for file in `ls`;
do
namechk=`echo $file | cut -c 1,2,3,4,5,6,7,8,9,10,11,12`;
filedate=`echo $file | cut -c 13,14,15,16,17,18`;
filesecs=`date -d "$filedate" +%s` # Get seconds since 00:00:00 1970-01-01 UTC
if test -d $file && test "$namechk" == "serverbackup"; then
diff=`expr $tsecs - $filesecs`
days=`expr $diff / 86400`;
if test $diff -gt 691200; then
echo "$file: $days days old - Removing"
#rm -rf $file
else
echo "$file: $days days old"
fi;
fi;
done;
}
today=`date +%y%m%d`
tsecs=`date +%s` # Get seconds since 00:00:00 1970-01-01 UTC
cleanup
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.