How to delete files in UNIX using shell script | 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.

How to delete files in UNIX using shell script

Shell Programming and Scripting


Tags
mtime

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-02-2006
theguy16 theguy16 is offline
Registered User
 
Join Date: Feb 2006
Last Activity: 23 June 2006, 6:09 AM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Question How to delete files in UNIX using shell script

Hi,

I have the following task to perform using shell script.

The user will provide a directory name along with a date. The script will delete all the files in the specified directory that was created earlier to that date. Also it should display the number of files that has been deleted.

Please help experts! Thanks in advance!
Sponsored Links
    #2  
Old 02-02-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 28 July 2014, 9:14 AM EDT
Location: NM
Posts: 10,156
Thanks: 269
Thanked 781 Times in 731 Posts
This deletes files in a directory tree that are less than 5 days old, and asks if it is okay to delete each file:


Code:
find /path/to/directory -mtime -5  -ok  rm -f {} \;

You need to do a search on the FAQ here on the ofrums for date arithmetic, so your script can calculcate the number of days to put in the above command, or a coomand like it.
Sponsored Links
    #3  
Old 02-03-2006
theguy16 theguy16 is offline
Registered User
 
Join Date: Feb 2006
Last Activity: 23 June 2006, 6:09 AM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the reply.

Still there is one issue left. How can we determine the number of files it has deleted after we run the script ?
    #4  
Old 02-03-2006
hegemaro hegemaro is offline
Registered User
 
Join Date: Feb 2006
Last Activity: 3 February 2012, 11:19 PM EST
Location: Schenectady, NY
Posts: 134
Thanks: 0
Thanked 2 Times in 2 Posts
Piping the previous command into wc

find /path/to/directory -mtime +5 -ok rm -f {} \; | wc -l

will give you a line count.

If you're confident in what you're deleting, and you VERIFY that the directory provided isn't a system directory, you can streamline the command without a prompt. Also, add one more exprssion to make certain you're deleting only FILES:

find /path/to/directory -mtime +5 -type f -print -exec rm -f {} \; 1>/tmp/out
wc -l /tmp/out

The print command displays the file being deleted. I'd recommend saving the output for review. You can then do a line count on the output file.
Sponsored Links
    #5  
Old 02-03-2006
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 5 July 2014, 11:14 AM EDT
Location: classification algos
Posts: 3,203
Thanks: 19
Thanked 30 Times in 27 Posts
else have an illustration as follows,


Code:
delcnt=0
for files in `find /path/to/directory -mtime -5 -print`
do
   echo "Deleting file $file"
   /bin/rm $file
   delcnt=$(($delcnt + 1))
done

echo "deleted $delcnt files"

Sponsored Links
    #6  
Old 03-24-2006
theguy16 theguy16 is offline
Registered User
 
Join Date: Feb 2006
Last Activity: 23 June 2006, 6:09 AM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Question How to delete files in UNIX using shell script

Hi,

find /path/to/directory -mtime -5 -ok rm -f {} \

is working fine. But there is one more concern. This command deletes all the files in the specified directory along with files in sub-directories also.

The requirement is like, it should delete files that are 5 Days old only in the directory specified and not the subdirectories within it.

Any idea on how to do that?

Thanks in advance
Sponsored Links
    #7  
Old 04-09-2008
ruleant ruleant is offline
Registered User
 
Join Date: Apr 2008
Last Activity: 9 April 2008, 5:17 AM EDT
Location: Belgium
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Adding -maxdepth 1 would do the trick, this way the find command only searches 1 level deep in the directory structure.

The complete command looks like this :

find /path/to/directory -mtime -5 -maxdepth 1 -ok rm -f {} \

Edit: I noticed this is an old thread, but I wanted to add a solution for completeness and to help people who would stumble upon this thread in the future.

Last edited by ruleant; 04-09-2008 at 04:50 AM.. Reason: reaction to later post
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
perl script to check if empty files are created and delete them and run a shell script hussa1n Shell Programming and Scripting 2 06-26-2010 09:34 PM
delete old files thru unix script snair001 Shell Programming and Scripting 3 02-08-2010 06:27 PM
Delete files older than 2 days using shell script in Unix satishpabba Shell Programming and Scripting 3 02-26-2009 05:23 AM
shell script for delete old files krishnarao Shell Programming and Scripting 4 01-13-2009 03:33 AM
how to delete empty files in a shell script rpnuge UNIX for Advanced & Expert Users 1 07-11-2002 04:56 PM



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