Deleting files using UNIX

Hi All ,

I am using the below if condition to delete files .

if [ "$LINKTRIGGER" != "" ]
then
if [ ! -f ${LANDINGDIR}/${LINKTRIGGER}.* ]; then
log_err "Trigger File ${LINKTRIGGER} does not exist!"
fi
log_msg "Deleting the Linktrigger"
rm -v ${LANDINGDIR}/${LINKTRIGGER}.*
else
if [ ! -f ${LANDINGDIR}/${TRGFILE}.* ]; then
log_err "Trigger File ${TRGFILE} does not exist!"
echo nofile
fi
log_msg "Deleting the individual trigger"
rm -v ${LANDINGDIR}/${TRGFILE}.*
fi
 

This is working fine however this piece of code is not working

 
if [ ! -f ${LANDINGDIR}/${LINKTRIGGER}.* ]; then
log_err "Trigger File ${LINKTRIGGER} does not exist!"
fi

Please let me know whats wrong

Thanks
dj

Hello dj,

It was a little mess with place of else . Following may help.

if [ "$LINKTRIGGER" != "" ]
then
 if [ ! -f ${LANDINGDIR}/${LINKTRIGGER}.* ]; then
  log_err "Trigger File ${LINKTRIGGER} does not exist!"
 else
  log_msg "Deleting the Linktrigger"
  rm -v ${LANDINGDIR}/${LINKTRIGGER}.*
 fi
 if [ ! -f ${LANDINGDIR}/${TRGFILE}.* ]; then
  log_err "Trigger File ${TRGFILE} does not exist!"
  echo nofile
 else
  log_msg "Deleting the individual trigger"
  rm -v ${LANDINGDIR}/${TRGFILE}.*
 fi 
fi

Thanks,
R. Singh

Hi all ,

Can u please let me know how do i search for file name with any extension in a particular folder

I am using the below code

if [ -f ${LANDINGDIR}/${TRGFILE} .* ] 

but this is not working

Posted by Hypesslearner:

Hello,

Following may help.

LANDINGDIR="/tmp"
TRGFILE="TEST.MATCHTEXT"
if [[ -f "$LANDINGDIR""/""$TRGFILE" ]]
then
 echo "present."
else
 echo "NOT present."
fi

This is an example for searching the file in any directory.

Thanks,
R. Singh

Thankyou . But i need to find Test.txt ot Test.trigger or any file with text.* . How do i do this ?

Hello,

Could you please try following command and let me kno if this helps.
This is just an example.

find /tmp -name "test*"
Syntax: find PATH -name "name"

Output will be as follows.

/tmp/test4
/tmp/test3
/tmp/test2
/tmp/test1
/tmp/test1213
/tmp/test1212
/tmp/test1211
/tmp/test122.ksh

Thanks,
R. Singh

Yes it is working but how do i use this in if condition as i need to use this in IF condition . My requirement is to check for test and delete all files like test1 or test2

Hello Hypesslearner,

Following may help.

find /tmp -name "test*" -exec rm -rf {} \;
Syntax: find PATH -name "FILE_NAME" -exec rm -rf {} \;

But be sure while running this command as it will delete directpries as well as files which will match name segment here.

Thanks,
R. Singh

Thankyou but Now this is not working

if [[ -n $(find {LANDINGDIR} -name "${TRGFILE*}") ]];

Posted by Hypesslearner:

Hello Hypesslearner,

There is no need for if here as find will take care of same. Here is another example for same.

LANDINGDIR="/tmp"
TRGFILE="test"
find "$LANDINGDIR" -type f -name "$TRGFILE*"
/tmp/test12
/tmp/test6
/tmp/test.sh
/tmp/test1
/tmp/test11
/tmp/test5
/tmp/test2
/tmp/test13
/tmp/test3
/tmp/test4

Then if you are getting correct results then run following command to delete the files.

find "$LANDINGDIR" -type f -name "$TRGFILE*" -exec rm -rf {} \;

Thanks,
R. Singh

try

fileexist=0
ls -d "$LANDINGDIR"/"$LINKTRIGGER."* |\
while read file ; do
     if [ -f "$file" ] ; then
          fileexist=1
     fi
done
if [ $fileexist -eq 1 ] ; then
     log_err "At least one Trigger File ${LINKTRIGGER} does exist!"
else
     log_err "Trigger File ${LINKTRIGGER} does not exist!"
fi

The else / if syntax is something basic you can look for on the internet with a simple search looking for bash syntax.

If you want to delete more than one file you need it to be recursive, with the -r option for rm command.

Bash expands all the coincidences in that directory but it is only supported with [[ ]] syntax.

Maybe you are looking for something like this.

if [ "$LINKTRIGGER" != "" ] 
then 
   if [[ ! ${LANDINGDIR}/${LINKTRIGGER}.* ]]
  then   
    log_err "Trigger File ${LINKTRIGGER} does not exist!"  
  else   
    log_msg "Deleting the Linktrigger"   
    rm -rv ${LANDINGDIR}/${LINKTRIGGER}.*  
  fi
  if [[ ! ${LANDINGDIR}/${TRGFILE}.* ]]
  then   
    log_err "Trigger File ${TRGFILE} does not exist!"   
    echo nofile  
  else   
    log_msg "Deleting the individual trigger"   
    rm -rv ${LANDINGDIR}/${TRGFILE}.*  
  fi  
fi