- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Unix Shell Script
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2002 12:51 AM
05-20-2002 12:51 AM
i need to write a script(UNIX) to delete files created older than 30 days
i'm only a beginner and i only found the above function -mtime
i tried this :
for fn in `ls *.*`
do
if [find ${WKDIR}/${fn} -mtime +30]; then
rm ${fn}
fi
done
*but this doesnt work
can someone help?
thanks a lot..reply soon..urgent
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2002 12:59 AM
05-20-2002 12:59 AM
Re: Unix Shell Script
Hello,
find $dir -type f -mtime +20 -exec rm {} \;
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2002 01:01 AM
05-20-2002 01:01 AM
Re: Unix Shell Script
cd $DIR
find . -mtime +30 -type f | xargs rm
Be aware that this command will do a recursive list of all dirs bellow $DIR, and delte all files older that 30 days.
Check what this will do first:
ll $( find . -mtime +30 -type f ) | more
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2002 01:07 AM
05-20-2002 01:07 AM
Re: Unix Shell Script
Carlos is right.
It will remove all files recursively under the directory.
do a list of files
find $dir -type f -mtime +20 -exec ll {} \;
then
remove them
find $dir -type f -mtime +20 -exec rm {} \;
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2002 01:18 AM
05-20-2002 01:18 AM
Re: Unix Shell Script
for a specified directory in Unix, i need to do these three things.
1. for files within a month, the files will remain
2. for files that are more than 4 mths old, these files are to be removed
3. for files that are between 2nd mth and 4th mth ,these files are to be gzip and tar.
may i know how can u do that in one simgle script?
i dun hav any idea at all..
is ther any other function i can use?
pls help..thanks to all~
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2002 01:36 AM
05-20-2002 01:36 AM
SolutionAssuming
- 4 months = approx 120 days
- 1 months = approx 30 days
1. for files within a month, the files will remain
# Do nothing
2. for files that are more than 4 mths old, these files are to be removed
# find $dir -mtime +120 -exec rm -f {} \;
3. for files that are between 2nd mth and 4th mth ,these files are to be gzip and tar.
# tar cvf oldfiles.tar `find $dir -mtime +30 -print`
Your script simply executes each in sequence:
archive.sh
====================================
#!/sbin/sh
dir=$1
find $dir -mtime +120 -exec rm -f {} \;
tar cvf oldfiles.tar `find $dir -mtime +30 -print`
====================================
To execute, e.g.
# archive.sh /tmp/testdir
WARNING: Always test your script first on a test directory before actually running it on a production basis.
Hope this helps. Regards.
Steven Sim Kok Leong
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2002 01:49 AM
05-20-2002 01:49 AM
Re: Unix Shell Script
If you move older files to another(s) directories you will never find any file older that x months.
See man sh-posix.... search test
See man touch
touch MMDDhhmm date1
touch MMDDhhmm date2 # older date
for file in *
do
if ( $file -nt $date1 ) ; then
echo $file >> files_to_remove
else if ( $file -nt $date2 ); then
echo $file >> files_to_tar
fi
fi
done
It is not checked, only the basis of a script...
Good Luck.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2002 07:45 PM
05-20-2002 07:45 PM
Re: Unix Shell Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2002 08:08 PM
05-20-2002 08:08 PM
Re: Unix Shell Script
# cd /dira
# find . \( -name dirb -prune \) -o -mtime +30 -exec rm -f {} \;
That will exclude "dirb" from being "processed". Another option you can use is the "-newer" option. For example you want to list all files under /dira which has a timestamp later than say May08 02:05am you can do this ..
# touch 05080205 ref
===> create an "empty file" dated May08 02:05
# cd /dira
# find . -newer /tmp/ref -exec ll {} \;
===> list all files newer than /tmp/ref
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2002 10:42 PM
05-20-2002 10:42 PM
Re: Unix Shell Script
appreciated..script completed.