I'm trying to create a bash script that trashes the oldest files in a directory with a variable setting the amount of files to retain.
My first step was to count the number of files older than a set number of 10 newest files to be retained:
#!/bin/bash
# How many files in the current directory are older than the retained files
olderThanRetain=$(find -maxdepth 1 -type f | sort -r | sed -e '1,10d' | wc -l)
echo $olderThanRetain
Works fine.
But when I try and set a variable for the number of files to be retained:
#!/bin/bash
retain=10
# How many files in the current directory are older than the retained files
olderThanRetain=$(find -maxdepth 1 -type f | sort -r | sed -e "1,(( $retain ))d" | wc -l)
echo $olderThanRetain
But this gives me the error:
sed: -e expression #1, char 3: unexpected `,'
What have I missed?