How to list excess files of certain type for removal?
Bo Berglund
bo.berglund at gmail.com
Wed Apr 7 09:04:23 UTC 2021
This is a general Linux question not particularly for Ubuntu...
I have a download script which downloads a video file from a youtube stream
every night. It is set up to run at certain times using "at" to get particular
hours of the stream.
The files are named like so:
2021-04-05_07AM_xxxxx.mp4
where the first part is a kind of "time stamp" for the video so they will be
listed in time order.
I am manually cleaning out older videos at least weekly, but now I would like to
automate also this. But I cannot figure out how such a script would look like...
What it should do is to give a list of the *excess* matching files over a
certain count given as argument. And these files should be from the top of the
list.
I have a script (getlastvideos) that can list the most recent matching files,
which I made maybe two years ago after asking online:
---
#/bin/bash
#Check target file argument
if [ -z "$1" ]; then
cnt="1"
else
cnt=$1
fi
lastvideos=`printf "%s\n" 202?-*.mp4 | tail -"$cnt"`
echo "$lastvideos"
---
This script prints a number of matching files depending on the call argument (a
number). It shows from the end of the list.
Now I would like to have a similar command but it should list *all* matching
files but *exclude* N of the most recent files where N is an argument to the
script.
Then I can use the output to remove these excess files leaving at least N files
in the directory.
If there are only N or less files no output should be generated.
So the selection is all matching fiiles *except* at least cnt most recent...
How would such a script look like?
I have tried replacing tail with head but then it displays the top cnt of the
listing.
I want the unknown number of files at the top of the list EXCEPT a certain
minimum number...
If I first can figure out the total number of matching files and do some math
against the cnt argument then maybe the argument to head could be calculated,
but how can that be done in a script?
--
Bo Berglund
Developer in Sweden
More information about the ubuntu-users
mailing list