I am facing problem in zsh scripting. I have group of files in a directory such as:
001_20160223.txt /delete
001_20161102.txt /delete
001_20161209.txt /keep
005_20160502.txt /delete
005_20161105.txt /delete
005_20161206.txt /keep
009_20160502.txt /delete
009_20161105.txt /delete
009_20161205.txt /keep
Now, we have to sort files having same starting number for eg. 001 and then we have find latest file based on the date written in file name and delete old files. The same way we have to perform for other files also. So the final output in directory would be following:
001_20161209.txt /keep
005_20161206.txt /keep
009_20161205.txt /keep
This is what I have tried so far:
files=($(find . -name "???_*.txt")) | if ((#files > 1)); then rm -- ${files[1,-2]}; fi
but it is deleting all files except the last one. I want to create a separate group of files having same starting name and then to delete it.
zargs
? – Lucas Apr 11 '16 at 21:48zargs
until now? Can you post the code that you tried? (I know how to autoload it.) What is the problem with the above answer, why do you need to usezargs
? – Lucas Apr 13 '16 at 04:21