Sunday, 23 October 2016

Script to delete files from particular path

$ vi delete
#!/bin/ksh
LOGFILE=/home/ flash /logs/delete.log
touch $LOGFILE
echo " ------Starting Time `date` --------------- ">>$LOGFILE
cd /home/flash/enemy/bad
echo " ------FILE COUNT-----">>$LOGFILE
ls -lR /home/flash/enemy/bad/* | wc -l >>$LOGFILE
ls -ltr *.bad | awk '{print $9}' | xargs \rm

·        Here delete is the script name.
·        Firstly we have created a LOGFILE variable to create logs and defined the path where the log is to be created.
·        Touch command will create and empty file.
·        Echo command output will be redirected (>>) to LOGFILE in which current date stamp is added.
·        When you want to print output of any command inside echo command use : `  at the beginning and end of command.
E.g.: `date`
·        cd will change directory and then write the current count of files into LOGFILE by wc-l command
ls -lR /home/flash/enemy/bad/* | wc -l >>$LOGFILE
·        Here * means alll the types of file. We can specify type of extension to delete specific file.
E.g.: *.bad, *.txt
·        Last command is to remove files from the directory.
·        Then chmod 755 to change permissions of the file.
·        Now  run the script by ./scriptname and will remove the files.

No comments:

Post a Comment