- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Script help
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
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
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
тАО03-01-2002 08:22 AM
тАО03-01-2002 08:22 AM
There are subdir and files under /prod/moe/db directory, the subdir are Feb07_2002, Feb12_2002 and Feb18_2002, I want to write a script which looks for latest 3 directories and remove the rest (only directory not files), like if system create 1 dir today which is Mar_01_2002, so I want to remove Feb07_2002, any suggestion
Thanks in advance
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-01-2002 08:27 AM
тАО03-01-2002 08:27 AM
Re: Script help
I would also turn the naming convention around so that the directories are YYYY_MM_DD, then problem then becomes very easy.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-01-2002 08:28 AM
тАО03-01-2002 08:28 AM
Re: Script help
Other than that, you'd probably have to do something with date conversion in perl, etc.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-01-2002 08:31 AM
тАО03-01-2002 08:31 AM
Re: Script help
what about this:
mkdir /prod/moe/park
cd /prod/moe/db
DIRS=`ls -1rt | tail -n 3`
echo Directories to be saved: $DIRS
mv $DIRS /prod/moe/park
rmdir *
mv /prod/moe/park/* .
If the directories are not empty, script may fail with rmdir. check if "rm -r" would be a solution, but be careful with this.
May be you need to qualify wildcards in the "ls" if the directory contains other stuff.
Option to ls "one", not "ell"!
Hope this helps
Volker
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-01-2002 08:37 AM
тАО03-01-2002 08:37 AM
Re: Script help
cd /yourdbpath
ll -t | awk '/^d/{s=s+1 ; if (s > 3) system("rn -R " $9)}'
This will sort the output from a ll command by last modify date, pipe the output to awk who will only look for directories (/^d/), and only execute the "rm -r" after the first 3 have passed by.
-- Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-01-2002 08:43 AM
тАО03-01-2002 08:43 AM
Re: Script help
I like it but there is one problem that there are some files related to latest directory which i don't want to remove and these dir not empty it got 20 big files, thats the reason i want to get rid of old directries, Any idea
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-01-2002 08:52 AM
тАО03-01-2002 08:52 AM
Re: Script help
at least you must be distinctive in the naming of the other files.
Would *_*_* as a wildcard give you only the directories you need, or is it possible that the other files may contain two or more "_" as well ?
Another option might be to choose
DIRS=`ls -1rt \`find . -type d\` | tail -n 3`
Watchit 1: cascaded reverse quotes require \ sign
Watchit 2: ls -1rt -- sort by timestamp, NOT by name, so if the directories are created with name and date not matching, you might be trapped.
Consider to change to YYYYMMDD convention if possible, or better YYYYMMDD_MYDIR because *_MYDIR would give you a very distinct wildcard.
Volker
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-01-2002 09:08 AM
тАО03-01-2002 09:08 AM
Re: Script help
ls -1drt
"d" to list only the directory name if it is a directory. Not the contents of the dir !
V.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-01-2002 10:09 AM
тАО03-01-2002 10:09 AM
Re: Script help
However, if there are files in that directory you will have to either move them some where or remove them before you can remove the directory.
Once you know the directory in /prod/moe/db you could try
x=`ls /prod/moe/db/$last_dir`
for file in $x
do
mv $file /new_dir
done
rmdir /prod/moe/db/$last_dir
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-01-2002 10:31 AM
тАО03-01-2002 10:31 AM
Re: Script help
It will go through the current directory and find all directories that start with Jan, Feb, Mar, etc. and count them. If the count is greater than 3, then it will find the oldest directories starting with Jan, Feb, Mar, etc. and remove them.
#!/sbin/sh
# Find number of dirs in current dir
NUM_DIR=`find . -type d -name "[Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec]*" | wc -l`
echo "$NUM_DIR directories here"
if [ $NUM_DIR -gt 3 ]
then
let NUM_TO_REMOVE=$NUM_DIR-3
echo "$NUM_TO_REMOVE to remove"
DIR=`find . -type d -name "[Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec]*" -print`
DIR_TO_RM=`ls -1drt $DIR | head -$NUM_TO_REMOVE`
echo "The following directories should be removed:"
echo "$DIR_TO_RM"
for i in `echo $DIR_TO_RM`
do
echo "Removing $i"
rm -r $i
done
else
echo "No directories to remove"
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-01-2002 10:39 AM
тАО03-01-2002 10:39 AM
SolutionHi Anthony,
I presume the directories Feb07_2002, Feb12_2002 etc.. are created by some rotatelogs script and the directories correspond to the actual date like this:
Mar 1 12:32 Mar01_2002/
Feb 18 12:30 Feb18_2002/
Feb 12 12:30 Feb12_2002/
Feb 7 12:30 Feb07_2002/
If that is the case and if there is some consistency in creating the directories then you could do something like this:
LOGDIR="/prod/moe/db"
cd "${LOGDIR}"
find . -type d -mtime +20 -exec rm -rf {} \;
rm -rf is a dangerous and powerful command ... I would test several scenarios before deploying it...
Hope this helps..
Thanks,
Shabu