0

I'm running a script on a group of mint/ubuntu VMs that every weeks downloads exactly 23 files, elaborates them one at time, and creates one big file (1..3 GB) for each downloaded file.

Downloaded files are in a folder, and elaborated files are in a different one folder.

Because the sript handle one downloaded file at time, creating one single new elaborated file, I'd like, at end of elaboration, to delete the oldest files automatically, in both folders.

My idea was to list folder content sorted by modified time descending, and to delete file starting from 24th.

Is it possibile to realize this using bash (so I can cron it) ?

realtebo
  • 23,922
  • 37
  • 112
  • 189
  • 1
    Sure, it should look at something like `ls -ltr | tail -1` – GMichael Jul 04 '16 at 08:06
  • Your command show me the oldest one, but my goal is to get a list starting from 24th oldest, because I need to keep on dis the 23 files most recent, and delete all the others, if any. Note that could be that in a particular week I have less of 23 files, so it is important that the script delete only from 24th oldest one, and only if at least 23 files are on disk. – realtebo Jul 04 '16 at 08:14
  • http://stackoverflow.com/a/299911/2088135 – Tom Fenech Jul 04 '16 at 08:19

1 Answers1

1

If you have a list of anything, and you want line 24 out of that, you may pipe it through the following simple sed script:

thing_that_generates_list | sed -n '24p'

If you want all lines from line 24 to the end, use

thing_that_generates_list | sed -n '24,$p'
Kusalananda
  • 14,885
  • 3
  • 41
  • 52
  • I used `ls -lt | sed -n '25,$p'`. I used `25` instead of `24` because the first line of the output is the total size in bytes. Thanks – realtebo Jul 04 '16 at 08:49