- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: "delete" 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
Discussions
Discussions
Forums
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
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
тАО10-20-2004 06:51 AM
тАО10-20-2004 06:51 AM
I need a shell script to monitor a directory's size. When there is no more space left on the disk I want to delete some subdirectories. The subdirectories are in the format:
backup_of_20041011
backup_of_20041012
where 20041011 is the date.
The last 10 (newest) subdirectories should not be deleted, only the oldest ones.
Can I easily find such a script, and if so where is a good place to look?
If not, what are the main functions needed to accomplish these tasks? I am new to Bash programming so any comments are welcome.
Regards.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-20-2004 07:26 AM
тАО10-20-2004 07:26 AM
Re: "delete" shell script
This script uses external libraries, but you don't need that.
My script cleans up pdf files but is adaptable.
Better than seeing if the filesystem is full, its a good idea to pick a certain number of days retention and be done with it. Makes the process less complex.
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-21-2004 05:18 AM
тАО10-21-2004 05:18 AM
Re: "delete" shell script
It is an impressive script that you attached. However I am looking for something quite simple. I decided to retain 10 directories.
ls -1 list my directories.
How do I put this output in an array so I can delete a directory per element reference? eg.
list{1)
list{2}
...
list{y}
And y-10=x, which means I should delete
list{1) to list{x}
Regards.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-21-2004 07:25 AM
тАО10-21-2004 07:25 AM
Solutionfrom the directory you are working with;
ls -l | grep '^d' | head -10 - this will produce a list of the first 10 directories.
ls -l | grep '^d' | tail -10 - this will producr a list of the last 10 directories.
rm -rf `ls -l | grep '^d' | head -10` - This will force remove the first 10 directories. You can add additional conditions to this command, such as grep -v. (Notice the backticks with the rm command.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-21-2004 07:28 AM
тАО10-21-2004 07:28 AM
Re: "delete" shell script
Cool answer- I will try it out.
Regards.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-27-2004 02:26 AM
тАО10-27-2004 02:26 AM
Re: "delete" shell script
Does anyone know what is wrong with this line in my script:
rm -rf `ls -1 $DIR | head -$TODELETE` &
The line proceeding this one prints the directories to be deleted perfectly, but this line does not remove anything, and also gives no error.
Regards.
Jano
#!/bin/sh
DIR="/mnt/nas/"
# amount of directories to keep
TOKEEP=10
TOTAL=0
# count total directories
TOTAL=`ls -l $DIR | grep '^d' | wc -l`
echo Total $TOTAL
echo Tokeep $TOKEEP
#test if TODELETE > 0
TODELETE=$[TOTAL-TOKEEP]
echo Todelete $TODELETE
if [ "$TODELETE" -gt 0 ] ; then
# something to do so let's continue
# ls -1 $DIR | head -$TODELETE
rm -rf `ls -1 $DIR | head -$TODELETE` &
else
# nothing to do so let's stop
echo nothing to delete
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-27-2004 10:24 AM
тАО10-27-2004 10:24 AM
Re: "delete" shell script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-28-2004 12:25 AM
тАО10-28-2004 12:25 AM
Re: "delete" shell script
From the last line of the debug I can see that the script is trying to delete the right directory (
+ rm -rf backup_of_20041018)
but it looks as though the path (DIR=/mnt/nas/) got lost somehow.
Here is the output as requested:
DIR="/mnt/nas/"
+ DIR=/mnt/nas/
# amount of directories to keep
TOKEEP=10
+ TOKEEP=10
TOTAL=0
+ TOTAL=0
# count total directories
TOTAL=`ls -l $DIR | grep '^d' | wc -l`
ls -l $DIR | grep '^d' | wc -l
++ ls -l /mnt/nas/
++ grep '^d'
++ wc -l
+ TOTAL= 11
echo Total $TOTAL
+ echo Total 11
Total 11
echo Tokeep $TOKEEP
+ echo Tokeep 10
Tokeep 10
#test if TODELETE > 0
TODELETE=$[TOTAL-TOKEEP]
+ TODELETE=1
echo Todelete $TODELETE
+ echo Todelete 1
Todelete 1
if [ "$TODELETE" -gt 0 ] ; then
# something to do so let's continue
# ls -1 $DIR | head -$TODELETE
rm -rf `ls -1 $DIR | head -$TODELETE` &
else
# nothing to do so let's stop
echo nothing to delete
fi
+ '[' 1 -gt 0 ']'
exit 0
+ exit 0
[root@server]# ++ head -1
+ rm -rf backup_of_20041018
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-29-2004 12:05 AM
тАО10-29-2004 12:05 AM
Re: "delete" shell script
The directory part given as argument to the ls command is not included in the output.
If you change this line:
rm -rf `ls -1 $DIR | head -$TODELETE` &
To:
ls $DIR | head -$TODELETE |
while read TARGET
do
rm -rf $DIR/$TARGET
done
But test it first by replacing the "rm -rf" part with "echo" to see that the correct dirs are displayed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-21-2008 07:26 AM
тАО11-21-2008 07:26 AM
Re: "delete" shell script
Saw your script, but my need to get the list of the versions in the partition having more disk usage., if we come across the no space left on the device we come across the struggle to increase space.
could you help me to give me some what helpful to me and all..
my requirement: to list versions occupy for a particular package, and how many times a same space contained versions are distributed.
thanks in advance in helping me..
Regards,
Shanker G.