I am trying to create a shell script that will keep the last n files in a directory and delete the rest. The config file that I am using is as under:
#config
dir1="/home/user/test"
n1="2"
dir2="/home/user/test/temp"
n2="3"
What I intend to achieve is goto dir1 and keep the last n1 files. I am testing out the following code
source /home/cfolder/config
ls -t $dir1 | sed -e '1,'$n1'd' | xargs -d '\n' rm
How do I get the code to loop through all the config parameters without explicitly writing a line of code for each dir and n group?