![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| shell script for delete old files | krishnarao | Shell Programming and Scripting | 4 | 01-13-2009 04:33 AM |
| Unix shell script for finding top ten files of maximum size | abhilashnair | UNIX for Dummies Questions & Answers | 10 | 02-18-2008 01:50 AM |
| shell script to transfer files from Unix to Windows | knag | Shell Programming and Scripting | 15 | 12-05-2006 05:46 PM |
| How To create Flat Files using Unix Shell Script? | Aparna_k82 | Shell Programming and Scripting | 4 | 02-10-2005 05:49 AM |
| how to delete empty files in a shell script | rpnuge | UNIX for Advanced & Expert Users | 1 | 07-11-2002 04:56 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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! |
|
||||
|
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 {} \;
|
|
||||
|
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. |
|
||||
|
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" |
|
||||
|
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 |
|
||||
|
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 |
![]() |
| Bookmarks |
| Tags |
| mtime |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|