I have directory where new sub directories and files being created every few minutes. The directories are like abc_date, def_date, ghi_date. I am looking to keep the latest 2 unique directories and delete everything else.
Here is what I have so far
This gives me unique names excluding the date.
ls -lt | awk '{ print $9 }' | cut -d_ -f1 | sort | uniq
This will give me the files that I want to delete for the uniq word abc
ls -lt |grep abc | sort -r | tail +3
My question is, how do i pass the output of first command to the second one and delete what is needed? Any help is greatly appreciated.
I have directories being created by some automated scripts like this
Broker_01022009.15.23
Broker_03042009.12.15
Broker_03062009.16.15
Dealer_02172009.14.20
Dealer_01222009.17.45
Dealer_02272009.16.40
What I want is to leave the last 2 and delete the rest directories and their contents. In this ex. what should be left is
Broker_03062009.16.15
Broker_03042009.12.15
Dealer_02272009.16.40
Dealer_02172009.14.20
Be careful when writing this type of script.
If the directories are being created by multiple concurrent processes rather than consecutive single processes you will need to be sure that a the process has finished before deleting the directory.
BTW: The while construct proposed by aixylinux is sound and works when there are no directories to delete. IMHO it needs a minor tweak to extract the directory name rather than the files in the directory.
Note the use of ls - one rather than ls - ell because we just want the name.
For example:
for PREFIX in "Broker_" "Dealer_"
do
ls -1d ${PREFIX}* 2>/dev/null | sort -r | tail +3 | while read DIR
do
if [ -d "${DIR}" ]
then
echo "Old directory: ${DIR}"
fi
done
done