Check for files and move it to another directory - ksh


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Check for files and move it to another directory - ksh
# 1  
Old 09-17-2019
Check for files and move it to another directory - ksh

I'm trying to wirte ksh script for given requirement, but i unable to achive it.

In dir1 directory I need to check for the files which suffixed with .csv or .txt, If there is no files, then i need to exit. If any files found I need to move the each file found to dir2 directory. I have to repeat this for each file found.

Code:
#####sample code#######

a=/kayal/dir1
b=/kayal/dir2
ls -ltr | egrep -i '*.csvg|*.txt'| awk '{print $NF}'|while read filename
do
count=cat $filename|wc -l
done
if [ "$count" -eq 0 ]
then
echo "No files found , exiting RC=0"
RC=0
exit 1
elif [ "$count" -gt 0 ]
then
echo "Processing file $filename"
 if [ -d "$b" ];
              then
               cd $b
               mv $a/$filename $b/$filename
                 else
                      echo "unable $b"
                      echo "Unable to move file $filename to $b"
                    
             fi
    fi
fi
done<filename

Moderator's Comments:
Mod Comment
Please, wrap your code samples into code tags.

Last edited by Peasant; 09-18-2019 at 12:18 PM.. Reason: Added code tags.
# 2  
Old 09-17-2019
What are you trying to do here?
egrep -i '*.csvg|*.txt'|

Assuming your cut/paste was fine, unsure the rationale of the 'g' in 'csvg'
And you are doing the egrep to find either of those filetypes of .csv and .txt

Or, explain more what is going wrong.

Last edited by Peasant; 09-18-2019 at 12:20 PM.. Reason: Added icode tag.
# 3  
Old 09-17-2019
I'm trying to find the files suffixed with .csv or .txt , .csvg is typo.

--- Post updated at 06:01 PM ---

error : ?*+ not preceded by valid expression.
# 4  
Old 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
# 5  
Old 09-18-2019
Should a plain find / exec work, regardless of the shell and OS ?
Code:
DIR1=/your/source
DIR2=/your/dest
find $DIR1 -type f -name "*.[CcTt][SsXx][VvTt]" -print -exec mv {} $DIR2 \;

Test and / or reporting could be done using a report file and testing against it.
Notice the -print, which will print found files if there are any.

Regards
Peasant.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How Create new directory and move files to that directory.?

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

2. UNIX for Dummies Questions & Answers

How to move gz files from one source directory to destination directory?

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

3. Shell Programming and Scripting

List files with date, create directory, move to the created directory

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

4. Shell Programming and Scripting

find command to move the files to other folder - ksh 88

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

5. UNIX for Dummies Questions & Answers

Zip all files in a directory and move to another directory

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

check for file existance and generate exit code amd move to another directory

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

7. UNIX for Dummies Questions & Answers

How to move all files in a directory and subdirectory?

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

8. Shell Programming and Scripting

How to check files and move the results to differents files?

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

9. UNIX for Dummies Questions & Answers

Move all files in a directory tree to a signal directory?

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
Login or Register to Ask a Question