09-18-2019
HI,
This has taken a little time to dig out, but here are a couple of scripts that should do what you want - modify them as you please - and use at your own risk.
The fist shell is a general purpose logger and should sit in /usr/local/bin or somewhere similar depending on your system setup.
Code :
#!/usr/bin/bash
###############################################################################
#
# This shell used to write system log file.
#
###############################################################################
###############################################################################
#
# Script information.
#
###############################################################################
#
# Script : logger.sh
# Author :
# Date :
# Version: 1.00.00
#
###############################################################################
#
# If $LOG not set up then writes to live daily LOG file in a default daily log.
#
###############################################################################
#
# This script should be resident in the users bin directory and should be
# excutable, it will either inherit a $LOG variable from the calling script or
# failing that will create it's own log in the /var/log directory.
#
###############################################################################
#
# If $LOG not set up then writes to live daily LOG file in the location below.
#
###############################################################################
if [ ${LOG} ]
then
:
else
LOG="/export/home/e415243/logs/general_log`date +%Y``date +%m%d`"
export LOG
fi
MSG="`date '+%d %b %T'` $*"
###############################################################################
#
# Make sure that we can write to the logfile if it exists.
#
###############################################################################
if [ -f ${LOG} ]
then
:
else
touch ${LOG}
chmod 666 ${LOG}
fi
###############################################################################
#
# If we can't write to the logfile lets tell someone, anyone will do!
#
###############################################################################
if echo "`date '+%d %b %T'` $*" >> ${LOG}
then
:
else
ERROR=$?
echo "logger.sh FAILED TO WRITE TO LOG FILE, error=${ERROR}"
exit ${ERROR}
fi
This next scipt does what you want - I think. But it will need to be setup for your source and target directories. You will have to refine the error checking and logging to suit your environment.
Code :
#!/usr/bin/bash
##############################################################################################
#
# Author :-
# Date :-
# Version:-
#
# History Latest at the top.
#
##############################################################################################
##############################################################################################
#
# Short description of script and any dependencies
#
#
# Requires the logger.sh script to be in the bin directory and to have a log file and local
# log directory setup.
#
# Script to move files by extensin, from a source directory to a target directory.
#
##############################################################################################
#
# Using coloured text output on screen, in order to proceed with this option there is a
# requitement to manipulate screem attributes. This can be tested using the colour.sh script
# and the appropriate handling codes used to change the output. It should be noted that the
# code should be opened and closed as a single action, there is also a subset of three already
# configured messages below which should be commented out or uncommented as required.
#
##############################################################################################
#
# The colours.sh script - run from a shell to test the screen output available.
#
##############################################################################################
# #!/bin/bash
# ##################################################################################
# #
# # Test script to show the available colours on a monitor, this allows the generation
# # of highlighted output from ordinary shell scripts - to be integrated with the
# # logger shell to make pretty output.
# #
# ##################################################################################
#
# T='XXX' # The test text
#
# echo -e "\n 40m 41m 42m 43m\
# 44m 45m 46m 47m";
#
# for FGs in ' m' ' 1m' ' 30m' '1;30m' ' 31m' '1;31m' ' 32m' \
# '1;32m' ' 33m' '1;33m' ' 34m' '1;34m' ' 35m' '1;35m' \
# ' 36m' '1;36m' ' 37m' '1;37m';
# do FG=${FGs// /}
# echo -en " $FGs \033[$FG $T "
# for BG in 40m 41m 42m 43m 44m 45m 46m 47m;
# do echo -en "$EINS \033[$FG\033[$BG $T \033[0m";
# done
# echo;
# done
# echo
##############################################################################################
#
# Set up the logging requirements, pay some attention to the detail here as it will probably
# be helpful if your scripting is anything like mine.
#
# MSG Prints a message to the logfile in the following format.
# MSG "I'm a log entry."
#
# JMSG Prints a Journal Message to the screen in the following format.
# JMSG "Put this message on the screen."
#
# DMSG Prints to the screen and the logfile in a single call.
#
# SMSG Success Mesage Prints in Green - Edit for Keyword.
#
# WMSG Warning Message Prints in Amber - Edit for Keyword.
#
# EMSG Error Message Prints in Red - Edit for Keywork.
#
# ERR Prints two lines to the logfile, the first carries a text message as above and the
# second carries the system error code this is formatted as follows.
# ERR "Something has gone wrong."
#
# ERR_COUNT Prints three lines to the logfile but unlike the straight forward "ERR", it is not
# fatal and the script will continue.
# ERR_COUNT "Something has gone wrong - but script is continuing, generally accompanied by a
# JMSG to print something on the screen.
#
# STARTMSG Puts a timestamp message with the script name in the logfile.
#
# ENDMSG Puts a timestamp message with scriptname in logfile.
#
##############################################################################################
MSG()
{
/usr/local/bin/logger.sh "${THISPROG} $*"
}
DMSG()
{
/usr/local/bin/logger.sh "${THISPROG} $*"
echo "\n$*"
}
SMSG()
{
echo "$*" | /usr/bin/awk '/Success/ { $0="\x1b[1;42m" $0 "\x1b[0m"; } 1'
/usr/local/bin/logger.sh "${THISPROG} $*"
}
WMSG()
{
echo "$*" | /usr/bin/awk '/Warning/ { $0="\x1b[1;43m" $0 "\x1b[0m"; } 1'
/usr/local/bin/logger.sh "${THISPROG} $*"
}
EMSG()
{
echo " $*" | /usr/bin/awk '/Error/ { $0="\x1b[1;41m" $0 "\x1b[0m"; } 1'
/usr/local/bin/logger.sh "${THISPROG} $*"
}
JMSG()
{
echo "$*"
}
ERR()
{
/usr/local/bin/logger.sh "${THISPROG} $*"
/usr/local/bin/logger.sh "${THISPROG} FAILS"
exit 1
}
ERR_COUNT()
{
/usr/local/bin/logger.sh "${THISPROG} $*"
/usr/local/bin/logger.sh "${THISPROG} Error Encountered."
COUNT=$((COUNT+1))
/usr/local/bin/logger.sh "${THISPROG} Error Count is now ${COUNT}."
}
ENDMSG()
{
/usr/local/bin/logger.sh "${THISPROG} COMPLETED"
exit 0
}
STARTMSG()
{
/usr/local/bin/logger.sh "${THISPROG} STARTED"
}
##############################################################################################
#
# Set up Globals.
#
##############################################################################################
BOLDON=`tput sgr1`
BOLDOFF=`tput sgr0`
export BOLDON BOLDOFF
TMPDIR=/var/tmp
BINDIR=/usr/local/bin
LOGDIR=/var/log
export BINDIR LOGDIR TMPDIR
THISPROG=$0
export THISPROG
LOG="${LOGDIR}/dailylog_`date +%Y``date +%m%d`.log"
REGRESS="${LOGDIR}/regress_`date +%Y``date +%m%d`"
COUNTER=0
COUNT=0
export LOG REGRESS COUNTER COUNT
HOSTNAME=`uname -n`
DATE=`date +%d%m%Y`
export HOSTNAME DATE
##############################################################################################
#
# Print the start message to the logfile.
#
##############################################################################################
STARTMSG
##############################################################################################
#
# Put the body of the script and any script specific variables in this section, it will make
# problem solving easier if you keep it all together.
#
##############################################################################################
SOURCE=/home/e434069/data
TARGET=/home/e434069/tmp
export SOURCE TARGET
if [ -d "${SOURCE}" ]
then
JMSG "Directory ${SOURCE} exists."
else
MSG "The source directory ${SOURCE} does not exist."
JMSG "The source directory ${SOURCE} does not exist."
ERR "Script Exiting."
fi
if [ -d "${TARGET}" ]
then
JMSG "Directory ${TARGET} exists."
else
MSG "The target directory ${TARGET} does not exist."
JMSG "The target directory ${TARGET} does not exist."
ERR "Script Exiting."
fi
##############################################################################################
# Setup file types to be moved from source to target directories.
##############################################################################################
FT1="*.txt"
FT2="*.csv"
export FT1 FT2
##############################################################################################
# Check that the file types exist and setup the counters.
##############################################################################################
FT1RC=`ls -l ${SOURCE}/${FT1} > /dev/null 2>&1`
FT2RC=`ls -l ${SOURCS}/${FT2} > /dev/null 2>&1`
COUNTFT1=`ls -l ${SOURCE}/${FT1} | wc -l | awk '{ print $1 }'`
COUNTFT2=`ls -l ${SOURCE}/${FT2} | wc -l | awk '{ print $1 }'`
##############################################################################################
# Move the files.
##############################################################################################
if [ "${FT1RC}" != "0" ]
then
if [ "${COUNTFT1}" != "0" ]
then
JMSG "There are ${COUNTFT1} files of type ${FT1} to move."
for i in `ls ${SOURCE}/${FT1}`
do
JMSG "Moving ${i} from ${SOURCE} to ${TARGET}"
mv ${i} ${TARGET}/
MVRC=$?
if [ "${MVRC}" == "0" ]
then
SMSG "Successfully moved file ${i}"
else
ERR_COUNT "Error there was a problem with ${i}"
fi
done
else
JMSG "There are no files of type ${FT1} to be moved."
fi
else
:
fi
if [ "${FT2RC}" != "0" ]
then
if [ "${COUNTFT2}" != "0" ]
then
JMSG "There are ${COUNTFT2} files of type ${FT2} to move."
for i in `ls ${SOURCE}/${FT2}`
do
JMSG "Moving ${i} from ${SOURCE} to ${TARGET}"
mv ${i} ${TARGET}/
MVRC=$?
if [ "${MVRC}" == "0" ]
then
SMSG "Successfully moved file ${i}"
else
ERR_COUNT "Error there was a problem with ${i}"
fi
done
else
JMSG "There are no files of type ${FT1} to be moved."
fi
else
:
fi
##############################################################################################
#
# Print the end message to the logfile.
#
##############################################################################################
if [[ ${COUNT} -ne 0 ]]
then
EMSG "The script encountered Errors - Please check ${LOGDIR}/${LOG}"
else
SMSG "The script completed with a status of Success."
fi
ENDMSG
You might have to tweak them both for your systems, it works like this.
Code :
[root@fbakirpomd4 bin]# ls ../data
file_01.dat test_01.csv test_01.txt test_02.dat test_03.txt test_05.csv
file_02.dat test_01.dat test_02.csv test_02.txt test_04.csv
[root@fbakirpomd4 bin]# ./mover.sh
Directory /home/e434069/data exists.
Directory /home/e434069/tmp exists.
There are 3 files of type *.txt to move.
Moving /home/e434069/data/test_01.txt from /home/e434069/data to /home/e434069/tmp
Successfully moved file /home/e434069/data/test_01.txt
Moving /home/e434069/data/test_02.txt from /home/e434069/data to /home/e434069/tmp
Successfully moved file /home/e434069/data/test_02.txt
Moving /home/e434069/data/test_03.txt from /home/e434069/data to /home/e434069/tmp
Successfully moved file /home/e434069/data/test_03.txt
There are 4 files of type *.csv to move.
Moving /home/e434069/data/test_01.csv from /home/e434069/data to /home/e434069/tmp
Successfully moved file /home/e434069/data/test_01.csv
Moving /home/e434069/data/test_02.csv from /home/e434069/data to /home/e434069/tmp
Successfully moved file /home/e434069/data/test_02.csv
Moving /home/e434069/data/test_04.csv from /home/e434069/data to /home/e434069/tmp
Successfully moved file /home/e434069/data/test_04.csv
Moving /home/e434069/data/test_05.csv from /home/e434069/data to /home/e434069/tmp
Successfully moved file /home/e434069/data/test_05.csv
The script completed with a status of Success.
[root@fbakirpomd4 bin]# ls ../data
file_01.dat file_02.dat test_01.dat test_02.dat
[root@fbakirpomd4 bin]# ls ../tmp
test_01.csv test_01.txt test_02.csv test_02.txt test_03.txt test_04.csv test_05.csv
[root@fbakirpomd4 bin]# cat /var/log/daily*
18 Sep 14:34:29 ./mover.sh STARTED
18 Sep 14:34:29 ./mover.sh Successfully moved file /home/e434069/data/test_01.txt
18 Sep 14:34:29 ./mover.sh Successfully moved file /home/e434069/data/test_02.txt
18 Sep 14:34:29 ./mover.sh Successfully moved file /home/e434069/data/test_03.txt
18 Sep 14:34:29 ./mover.sh Successfully moved file /home/e434069/data/test_01.csv
18 Sep 14:34:29 ./mover.sh Successfully moved file /home/e434069/data/test_02.csv
18 Sep 14:34:29 ./mover.sh Successfully moved file /home/e434069/data/test_04.csv
18 Sep 14:34:29 ./mover.sh Successfully moved file /home/e434069/data/test_05.csv
18 Sep 14:34:29 ./mover.sh The script completed with a status of Success.
18 Sep 14:34:29 ./mover.sh COMPLETED
[root@fbakirpomd4 bin]#
Regards
Gull04
9 More Discussions You Might Find Interesting
1. UNIX for Dummies Questions & Answers
Is this possible? Let me know If I need specify further on what I am trying to do- I just want to spare you the boring details of my personal file management.
Thanks in advance-
Brian- (2 Replies)
Discussion started by: briandanielz
2 Replies
2. Shell Programming and Scripting
Hi,
I am a newbie to shell scripting. here is my objective:
1)The shell program should take 2 parameters - ie-> DestinationFolder, WebFolder
2)Destination folder contains few files that has to has be verified and deleted.
3)WebFolder is a folder containing a list of master files
4)It... (1 Reply)
Discussion started by: sandhyagupta
1 Replies
3. UNIX for Dummies Questions & Answers
I'm trying to organize my MB Pro by moving all my jpeg files to a single folder from the desktop. There are some on the desktop that are not in any folder. I was at the command line and typed
mv *.jpg "Jpeg files"
but it only moved the files that were on the desktop, not any of the ones that... (3 Replies)
Discussion started by: Straitsfan
3 Replies
4. Shell Programming and Scripting
Hi..
i have a file ABC_*.txt in source directory which will come evry 30 min and same file will be moved to working directory first time ...and will perform some operations then we archive ABC_*.txt ..this will run for 30 min to 45 min
from 2nd time onwards requirement is ...i need to check... (3 Replies)
Discussion started by: dssyadav
3 Replies
5. UNIX for Dummies Questions & Answers
Hi,
need to zip all files in a directory and move to another directory after the zip..
i am using this one but didnt help me...
zip -r my_proj_`date +%Y%m%d%H%MS`.zip /path/my_proj
mv in_proj_`date +%Y%m%d%H%M%S`.zip /path/source/
i am trying to zip all the files in my_proj... (0 Replies)
Discussion started by: dssyadav
0 Replies
6. Shell Programming and Scripting
Hi ,
I've learnt that the following command will remove the files from the given folder for given no.of days
find /home/etc -type f -atime -10 -exec rm -f {} \;
But how can I change the above command that will move the files to another specified directory instead of removing the... (1 Reply)
Discussion started by: smile689
1 Replies
7. Shell Programming and Scripting
Hi all,
i have a folder, with tons of files containing as following,
on /my/folder/jobs/
some_name_2016-01-17-22-38-58_some name_0_0.zip.done
some_name_2016-01-17-22-40-30_some name_0_0.zip.done
some_name_2016-01-17-22-48-50_some name_0_0.zip.done
and these can be lots of similar files,... (6 Replies)
Discussion started by: charli1
6 Replies
8. UNIX for Dummies Questions & Answers
Hi All,
Daily i am doing the house keeping in one of my server and manually moving the files which were older than 90 days and moving to destination folder.
using the find command . Could you please assist me how to put the automation using the shell script .
... (11 Replies)
Discussion started by: venkat918
11 Replies
9. Shell Programming and Scripting
Hi All,
We have main directory called "head"
under this we have several sub directories and under these directories we have sub directories.
My requirement is I have to find the SQL files which are having the string "procedure" under "head" directory and sub directories as well.
And create... (14 Replies)
Discussion started by: ROCK_PLSQL
14 Replies