[Solved] Issue with deleting files through find

Hi,
I have a script similar to this

#!/bin/ksh
cd /orcl/bir/eod_badfiles
find ./ -type f -name "*.csv" -mtime +6 -exec rm -f {} \;
find ./ -type f -name "*.bad" -mtime +6 -exec rm -f {} \;
cd /orcl/bir
find ./ -type f -name "*.log" -mtime +6 -exec rm -f {} \;

This was working fine in one of the environment. When I'm using it in another environment it's giving an error.

find: ./lost+found: Permission denied

This is because a root directory lost+found is present. How can i skip subdirectories and delete only the files only from the current directory?

Or is there a way where the script would proceed ignoring the permission denied error?

you can try ignoring the directory

 
find ./ -type d \( ! lost+found \) -type f -name "*.csv" -mtime +6 -exec rm -f {} \;

if you consider it as warning messages

redirect the find output to 2> /dev/null

1 Like

I'm new to unix. I would like to ignore the error. Probably that's better as in other environments there will be other subdirectories.

So u mean to say, I've to do this way. I hope it will ignore any permission denied error and proceed ahead. Also this is running through Autosys job so I hope the job will turn successful.

#!/bin/ksh
cd /orcl/bir/eod_badfiles
find ./ -type f -name "*.csv" -mtime +6 -exec rm -f {} \; 2> /dev/null
find ./ -type f -name "*.bad" -mtime +6 -exec rm -f {} \; 2> /dev/null
cd /orcl/bir
find ./ -type f -name "*.log" -mtime +6 -exec rm -f {} \; 2> /dev/null

---------- Post updated at 09:45 AM ---------- Previous update was at 07:29 AM ----------

This is not working. It is giving error.

find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]

---------- Post updated at 09:46 AM ---------- Previous update was at 09:45 AM ----------

Can someone please help with this request?

which OS you are using? Is it solaris?

You can use maxdepth option in find command to search in current directory only.

Cheers!
-R

1 Like

Yup. I used the max depth option and it worked.

find ./ -maxdepth 1 -type f -name "*.csv" -mtime +6 -exec rm -f {} \;

Thanks everyone for all your help.