find command Help!!

I am using below command in shell script. All parameters are getting replaced correctly but files are not getting deleted. Please help :

Variables :-

FILE_LOCATION = /var/core
FILE_NAME=core*
OLDER_THAN=0

find ${FILE_LOCATION}/ \( -name ${FILE_NAME} -a -mtime ${OLDER_THAN} \) -exec rm {} \;

i wrote you script exactly as you did and it worked on ym system. I ran it as root and changed the ownership but it works fine. i did chmod 700 test chown root:other test. then i created a /var/core dir. then touched multiple files with numbers in them i ran it and they were gone.

When I run my script in debug mode I see that \ is not getting executed as part of command. It is being treated as escape character.

find ${DATABASE_PARAM_PATH} -mtime +${RETENTION_PERIOD} -exec rm -rf {} \;

Command execution output in debug mode :

find /pgmfgfpws/app/oracle/mfgfpwsdb/10.2.0/admin/mfgfpws_webisstg70/bdump -mtime +1 -exec ls -ltr {} ;

Backslash is missing. Without blackslash if bdump directory is empty after deleting all files which meet deletion criterion, find command deletes bdump directory as well. I don't want bdump directory to be deleted even though it is empty.

Any help is greatly appreciated.

Add -type f to only delete files and not directories matching the criteria.

I don't think the lack of a backslash is the explanation. If you mean the one before the ; it's required to pass through a literal semicolon; you could equivalently put the semicolon in single quotes, for example, to prevent the shell from interpreting it as a command separator. What you see with set -v or equivalent is what actually gets executed after quoting and backslash substitutions have been processed by the shell.

I would assume that the reason the directory gets removed is that it meets the criteria you have, plain and simple. Its modification time will be updated when you delete files in it so specifying -depthfirst if your find has that might also be a workaround.

ls is not a good test case because it's hard to see when it's listing files in the directory vs when it's listing the whole directory. Adding the -d option would help disambiguate, or you could simply use echo for debugging.

Thanks Era for elaborated explaination. Your updates are always insightful and help me understand unix command better.

Why cant you shorten it this way?
CORE_FILE="/var/core/core*"
OLD=0
find {CORE_FILE} -mtime ${OLD} -exec rm {} \;

Wouldn't be very wise to do it like this.

The wildcard (*) will be interpreted by the shell.

Suppose there are a few hunderd core files present, the effective find command will be

find /var/core/core0001 /var/core/core0002 /var/core/core003 ............. -mtime 0 -exec rm {} \;

This will result in an enormous CPU load.

Never use wildcards in a find command unless you either escape (\) them or put them withing single quotes (')

In the reply of "era" are the proper directions to handle something like this.