Python – Compare modification dates between files and find time difference

The script searches in a specific location for files and it gets in POSIX the time of modification of all files. It also creates a temp file (and write text in it) within the same directory (which gets deleted once the script exits). This temp file is used to get today’s modification date. Then all POSIX dates are converted into human readable format and a comparison is done between the temp file and the files we are examining. If the files we are examining are three months or older, then …. you can enter what ever custom action you want.

#!/usr/bin/python
#The script searches in a specific location for files and it gets in POSIX the time of modification of all files.
#It also creates a temp file (and write text in it)  within the same directory (which gets deleted once the script exits)
#This temp file is used to get today's modification date. Then all POSIX dates are converted into human readable format
#and a comparison is done between the temp file and the files we are examining. If the files we are examining are
#three months or older, then .... you can enter what ever custom action you want.

import os.path
import tempfile
import datetime
import dateutil.relativedelta
import dateutil

#Path of where we are looking for the files
drc = '/root/Music'

#We create a temp file so we can get today's date. File is deleted after script is done executing
tempF = tempfile.NamedTemporaryFile(dir=drc)#, delete=False)
tempF.write('something')#We put in some text in so we are sure to get a time modified.
fileT =  os.stat(tempF.name)[8] #We are getting the mtime aka time of modification
fileT_human = datetime.datetime.fromtimestamp(fileT)#We are converting it to human readeable

for dirpath, dirname, filename in os.walk(drc):  #we are going huntinf for...
        for fname in filename:                   #...files
                path = os.path.join(dirpath, fname) #full path of files
                mtime = os.stat(path)[8] #We are getting time of modification of all the files
                mtime_human  = datetime.datetime.fromtimestamp(mtime) #We humanize the date

                #We are comparing each file with the fileT_human in order to get the difference in date when it comes to modification time
                diff = dateutil.relativedelta.relativedelta (fileT_human, mtime_human)

                #Just a sample output. You can use years, months, days to do the comparison between files.
                #print "%d years, %d months, %d days, %d hours, %d minutes and %d seconds" % (diff.years, diff.months, diff.days, diff.hours, diff.minutes, diff.seconds)
                months = diff.months
                if  months > 3:
                        print "Deleting file ", path
                        #Put whatever action you want here.
                        os.remove(path) #it will delete files only
                else:
                        exit;
Share Button

Centos 7 PostgresSQL 9.4 – Install database and use a non default PGDATA location, auto start on Boot (systemd)

These instructions show how to install postgresSQL 9.4 on Centos7. It also shows how to use a non default location from where to run your database engine. This is done by altering the PGDATA variable which dictates where the database engine is running from. It should be noted that when installing Postgres, a user Postgres is also created. This user account has environmental variables that deviate from the other accounts. You can cat the /etc/passwd file and see the default location for the home directory. As a result in order to change the environmental variable PGDATA you will have to alter the .bash_profile in that location in order for the change to take effect.

 

Step 1) Install the yum Repo for  PostgresSQL 9.4

yum -y localinstall http://yum.postgresql.org/9.4/redhat/rhel-7-x86_64/pgdg-centos94-9.4-1.noarch.rpm

Step 2) Download the RPMS and install the database engine

yum -y install   postgresql94-contrib  postgresql94-server

Step 3) Create the directories from where the database will be running from. In this case it will be from /home/postgres

mkdir /home/postgres; chown -R postgres:postgres /home/postgres

Step 4) Initialize the database. 

cd  /usr/pgsql-9.4/bin/; ./initdb -D /home/postgres

Step 5)  Alter the default location from where your database will be running from.

vim /var/lib/pgsql/.bash_profile
#Alter the PGDATA as follows. See sample below
#PGDATA=/var/lib/pgsql/9.4/data
PGDATA=/home/postgres
export PGDATA

6) Configure systemd to start the database on boot-up

 vi /usr/lib/systemd/system/postgresql-9.4.service
# Note: avoid inserting whitespace in these Environment= lines, or you may
# break postgresql-setup.
# Location of database directory
#Environment=PGDATA=/var/lib/pgsql/9.4/data/
Environment=PGDATA=/home/postgres/
systemctl daemon-reload
systemctl enable  postgresql-9.4.service
systemctl start  postgresql-9.4.service
Share Button