Skip to content

speters.org

all t3h ev1l things that you can imagine

Menu
  • About
  • impressum
Menu

delete last 3 files

Posted on 2010-04-152010-04-15 by serge

I have a little problem. Every time I deploy a release, I move the old one into an old-folder. The problem is: is there a way to delete all files in this old-folder except the 3 last files? kind of an opposite of ls -tr | tail -n 3?

I took half an hour and hacked a little script the easy and st00pid way:


#!/bin/bash
#
# Name: del-except-3
# Author: pes
# Date: 08.04.2010
# Description
# Deletes all files in ${searchpath} except the last 3
#

debug=1
searchpath=$1

if [ ! -d ${searchpath} ]; then
    echo "${searchpath} does not exist"
    exit 1
fi

cd ${searchpath}

last3=`ls -tr | tail -3`

set -- ${last3}

last3_1=$1
last3_2=$2
last3_3=$3

if [ ${debug} -ne 0 ]; then
    echo "last 3 files in ${searchpath}"
    echo " 1: $last3_1"
    echo " 2: $last3_2"
    echo " 3: $last3_3"
fi

for i in `ls -1`; do
    if [ "${i}" != "${last3_1}" ]; then
        if [ "${i}" != "${last3_2}" ]; then
            if [ "${i}" != "${last3_3}" ]; then
                echo "rm ${searchpath}/${i}"
                rm ${i}
            fi
        fi
    fi
done

is there a smart combination of shell commands? someting that does not look that stupid?

2 thoughts on “delete last 3 files”

  1. enkei says:
    2010-08-27 at 08:19

    Das liefert dir alle Files ausser die letzten drei:

    ls -tr | head -n $((`ls | wc -l`-3))

    Den Rest musst du selber machen 😉

  2. serge says:
    2010-08-27 at 08:53

    IMPROVEMENT! 😀

    cool, danke! also an head -n hab ich schon gedacht, allerdings nicht an die berechnung der gesamtanzahl der dateien minus die gewünschten drei 😀 ph4t!

    ls -tr | head -n $((`ls | wc -l`-2)) is t3h r0x0r.

Comments are closed.

Categories

  • bike
  • c0de
  • cigars
  • followerpower
  • lecker
  • nöt züri
  • serge in danger
  • sinnfrei
  • sport
  • züri

Comments

  • enkei on men’s room
  • serge on delete last 3 files
  • enkei on delete last 3 files
  • serge on norman antivirus + eclipse + heap space
  • serge on heidelbeer muffins

Blogroll

  • blafasel.org
  • floriankohl.de
  • ichundderbaer.de
  • kunkelundkohl.de
  • revista verlag

Archives

  • July 2018 (1)
  • June 2018 (2)
  • May 2018 (1)
  • April 2018 (2)
  • October 2017 (1)
  • September 2017 (4)
  • August 2017 (1)
  • July 2017 (1)
  • June 2017 (5)
  • May 2017 (2)
  • October 2013 (2)
  • September 2013 (1)
  • August 2013 (2)
  • June 2013 (1)
  • May 2012 (1)
  • February 2012 (2)
  • May 2011 (1)
  • September 2010 (2)
  • August 2010 (2)
  • April 2010 (2)
  • March 2010 (1)
  • February 2010 (1)
  • October 2009 (2)
  • August 2009 (1)
  • June 2009 (1)
  • August 2008 (2)
  • July 2008 (2)
  • June 2008 (2)
  • April 2008 (5)
  • July 2007 (3)

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
March 2023
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  
« Jul    
© 2023 speters.org | Powered by Minimalist Blog WordPress Theme