[Solved] Issue with deleting files through find | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

[Solved] Issue with deleting files through find

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-10-2013
Gangadhar Reddy Gangadhar Reddy is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 16 August 2013, 12:49 AM EDT
Posts: 38
Thanks: 19
Thanked 0 Times in 0 Posts
[Solved] Issue with deleting files through find

Hi,
I have a script similar to this

Code:
#!/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.

Code:
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?

Last edited by Don Cragun; 07-10-2013 at 12:53 PM..
Sponsored Links
    #2  
Old 07-10-2013
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Advisor  
The Tutor
 
Join Date: Jun 2008
Last Activity: 9 April 2014, 1:31 AM EDT
Location: INDIA, Bangalore
Posts: 2,049
Thanks: 16
Thanked 106 Times in 102 Posts
you can try ignoring the directory


Code:
 
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
The Following User Says Thank You to vidyadhar85 For This Useful Post:
Gangadhar Reddy (07-10-2013)
Sponsored Links
    #3  
Old 07-10-2013
Gangadhar Reddy Gangadhar Reddy is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 16 August 2013, 12:49 AM EDT
Posts: 38
Thanks: 19
Thanked 0 Times in 0 Posts
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.


Code:
#!/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 ----------

Quote:
Originally Posted by vidyadhar85 View Post
you can try ignoring the directory


Code:
 
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
This is not working. It is giving error.

Code:
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?
    #4  
Old 07-10-2013
millan millan is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 1 August 2014, 4:15 AM EDT
Posts: 118
Thanks: 3
Thanked 5 Times in 5 Posts
which OS you are using? Is it solaris?
Sponsored Links
    #5  
Old 07-10-2013
rangarasan's Avatar
rangarasan rangarasan is offline
Registered User
 
Join Date: Jul 2011
Last Activity: 2 September 2014, 7:23 AM EDT
Location: Chennai, India
Posts: 496
Thanks: 9
Thanked 124 Times in 120 Posts
Quote:
Originally Posted by Gangadhar Reddy View Post
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.


Code:
#!/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.

Code:
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?
You can use maxdepth option in find command to search in current directory only.

Cheers!
-R
The Following User Says Thank You to rangarasan For This Useful Post:
Gangadhar Reddy (07-10-2013)
Sponsored Links
    #6  
Old 07-10-2013
Gangadhar Reddy Gangadhar Reddy is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 16 August 2013, 12:49 AM EDT
Posts: 38
Thanks: 19
Thanked 0 Times in 0 Posts
Yup. I used the max depth option and it worked.

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

Thanks everyone for all your help.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
find . -name '*.req' -mtime +2 -exec rm {} \; not deleting files rehantayyab82 Red Hat 5 11-05-2012 03:48 AM
issue while moving files using find command mail2mura Shell Programming and Scripting 1 02-08-2012 09:10 AM
Find in files issue kalik UNIX for Dummies Questions & Answers 3 11-28-2010 06:33 PM
deleting files inside shell script - ( using find) Jayaram.Nambura Shell Programming and Scripting 2 05-27-2009 09:10 AM
Deleting files using find command deepaklanka Shell Programming and Scripting 8 11-05-2008 09:17 AM



All times are GMT -4. The time now is 04:22 PM.