find command -No such file error

Hi ,

I tried the below code to remove the files older that 30 days .

#!/bin/ksh
set -x
file_path1="/home/etc"
file_path2="/home/hst"
file_nm="HST"
days=30
find $file_path1/*$file_nm* -type f -mtime +$days -exec rm -f {} \;
find $file_path2/*$file_nm* -type f -mtime +$days -exec rm -f {} \;

The file with the abvoe required name doesn't exist in the past 30 days .
But still I want the script to run by ignoring the following error .
The following error stops executing the another find command in my script .
Error is:

find: stat() error /home/etc/*HST*: No such file or directory

Please Suggest

Quick and dirty, suppress ALL error messages:

set +e # on error continue
find $file_path1/*$file_nm* -type f -mtime +$days -exec rm -f {} \; 2>/dev/null
find $file_path2/*$file_nm* -type f -mtime +$days -exec rm -f {} \; 2>/dev/null

Or use a for loop and test if each file exists

for file in $file_path1/*$file_nm*  $file_path2/*$file_nm*
do
  [ -e "$file" ] &&
  find "$file" -type f -mtime +$days -exec rm -f {} \;
done
1 Like

Change the command to:

find $file_path1 -name "*$file_nm*" -type f -mtime +$days -exec rm -f {} \;

as it should be.

1 Like

Specify the name as a parameter:

find $file_path1 -name *$file_nm* -type f -mtime +$days
1 Like