Find files created on weekend,monthend,quarteley etc
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.
For what I know you can only find your files based on the modification time and not on the creation time. At least not with linux e*fs. I'm not sure with other filesystems though. Still there are other possible ways to find the files. Some of these are by:
(a) depending on the average time a backup process goes - if it's just quick, you can base it from the modification time
(b) the filename of the file - are there dates added to the file?
(c) or the contents of the files - If dates are included inside the files, maybe the first lines can help
for each 7th day of the month do you mean something like this:
Code:
year=2008
month=01
ls ${year}${month}{07,14,21,28}.tgz
If you mean every weekend and also for the last day, you'll need a function or script that understands the calendar system. You'll use that script to .. for example.. know the number of days in the month in question and the position of the first day of the month .. for example sunday.
You can use time(), date(), mktime() and strtotime() in php but i don't know how to do that in perl. You can use the external command date but only for only the current time or else you'll have to go alternating your clock and that won't be good.
Last edited by konsolebox; 03-14-2008 at 03:56 AM.
Well, xargs is not suitable here, but you can read the output of find into a while loop, and perform some operations on each file to determine which ones you want to handle.
Here's an example shell script which will identify files from the end of week and end of month. Note that a date can be BOTH at the end of the week, AND at the end of the month. When you modify this script to do something with the files, make sure you take that into account.
Code:
#!/bin/bash
EOW_DAY=Sun
# prevent locale from affecting date output
export LANG=C
unset LC_TIME
unset LC_ALL
main () {
for f in "$@"; do
# chop the .tar.gz off the end
d=${f%.tar.gz}
# take the last 8 characters, assumed to be yyyymmdd date
d=${d:$((${#d}-8)):${#d}}
# split year, month and day
y=${d:0:4}
m=${d:4:2}
d=${d:6:2}
# test to see if we have an end of week and end of month date
if is_end_of_week $y $m $d ; then
echo "$f is end of week"
fi
if is_end_of_month $y $m $d ; then
echo "$f is end of month"
fi
done
}
is_end_of_week () {
# should get three parameters - year, month, day.
dow=$(date +%a -d $1-$2-$3)
if [ "$dow" = "$EOW_DAY" ]; then
return 0
else
return 1
fi
}
is_end_of_month () {
# should get three parameters - year, month, day.
case "$2" in
01|03|05|07|08|10|20)
lastday=31
;;
02)
if [ $(( $1 % 4 )) -eq 0 ]; then
# leap year
lastday=29
else
lastday=28
fi
;;
*)
lastday=30
;;
esac
if [ "$lastday" = "$3" ]; then
return 0
else
return 1
fi
}
main "$@"
Save this into a file called myscript.sh, chmod 755 that file, and run it using the full path, and pass the names of files to process as parameters, e.g. if you create the file in your HOME directory, called myscript.sh:
Code:
chmod 755 ~/myscript
cd /where/your/logs/are
~/myscript.sh *.tar.gz
is_end_of_month () {
# should get three parameters - year, month, day.
case "$2" in
01|03|05|07|08|10|20)
lastday=31
;;
02)
if [ $(( $1 % 4 )) -eq 0 ]; then
# leap year
lastday=29
else
lastday=28
fi
;;
*)
lastday=30
;;
esac
You know the daft thing is that I actually listened to this story being talked about on a podcast and still just wrote this bug. Hmm. How can I vindicate myself? Urr, aha! yes, this script fails to get all leap years correctly as a feature, to maintain compatibility with Microsoft Excel! Refuse to fix!
I need to move the above files into another folder ie created on each weekends (sunday),end day of month etc.. ie for segrating weekend backups,monthly backup etc
Thanks
Sujith
Quote:
Originally Posted by matthewg42
Well, xargs is not suitable here, but you can read the output of find into a while loop, and perform some operations on each file to determine which ones you want to handle.
is_end_of_month () {
# should get three parameters - year, month, day.
case "$2" in
01|03|05|07|08|10|20)
lastday=31
;;
02)
if is_leap_year $1; then
lastday=29
else
lastday=28
fi
;;
*)
lastday=30
;;
esac
if [ "$lastday" = "$3" ]; then
return 0
else
return 1
fi
}
is_leap_year () {
# take a year as a parameter
if [ $(($1 % 4)) -eq 0 ]; then
if [ $(($1 % 100)) -eq 0 ]; then
if [ $(($1 % 400)) -eq 0 ]; then
return 0
else
return 1
fi
else
return 0
fi
else
return 1
fi
}
You know the daft thing is that I actually listened to this story being talked about on a podcast and still just wrote this bug. Hmm. How can I vindicate myself? Urr, aha! yes, this script fails to get all leap years correctly as a feature, to maintain compatibility with Microsoft Excel! Refuse to fix!
So you listen to Linux Outlaws I'm guessing.
I'm constantly amazed how some things that seem they should be easy in fact cause great difficulty. Different countries had adopted the Gregorian calendar during different years. Different states in the US on different dates as well. If you have a database that records historic events, that needs to be taken into account.
Have you ever heard of leap-seconds? Due to tidal forces, the rotation of the earth slows down over time. Every few years a second is added before the New Year. However, there are two dates in the year when might be added. And the US wants to not do this for the standard atomic clock. I think the argument is to count the seconds from the base time and let the clients do the adjusting.
Funny thing is since 2000, the earth's has started rotating faster! No one knows why. ( At least according to the Wikipedia article on Leap Seconds )
I applied the updates last month and one of them was for a 30 minute adjustment for the time in Venezuela. Their dictator is obviously the center of the Universe! It reminded me of the movie "Banana's" where it was declared that underwear should be worn on the outside of the pants.
As I understand it, there is only one timezone in all of China! I wonder if it is called "CPCT"?
Distribution: OpenSuse 10.3, SLED 10 SP2, Ubuntu 8.04 and 9.04
Posts: 23
Rep:
I see this as a classroom assignment for use of 'date' command. For details see 'info date'. Putting everything inside 'find' is just classroom sadism.
Code:
#!/bin/bash
# hro 2008-03-14
for f in /var/log/*.bz2
do (
da=$(date -r $f +%Y%m%d)
weekday=$(date '--date='$da +%w)
day=${da:6:2}
month=${da:4:2}
nextday=$(date '--date=tomorrow '$da +%d)
echo -n 'm='$month 'd='$day' '
case $nextday in
01) echo -n "last day ";;
*) echo -n "........ ";;
esac
case $weekday in
0 | 6) echo "weekend" $f ;;
*) echo "workday" $f ;;
esac
) done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.