If you cannot know that only one file per day is generated (your failing command would work in that case), you have to pipe the output of find
in which you have a combination of the date-time-stamp of the file and the filename into sort
and then exclude the final line with sed
before splitting of the date-time-stamp (again with sed
) and feeding the resulting names into xargs -0 rm
:
find . -type f -name "dbf*" -printf "%T@:%p\0" | sort -z | sed -z '$d' | \
sed -z 's/[0-9\.]*://' | xargs -0 rm
As the filenames will start with ./
the second sed
will only match up to the first ':' even though matching is greedy (-printf "%T@:%p"
gives you something like 1424765805.0206990940:./dbf_xyz
)
This should work with filenames with spaces and newlines as well, because the whole chain is using NUL terminated "lines"
cp "$(ls -Art | tail -n 1)" /tmp
. I have not tested for newline files. – iyrin Feb 24 '15 at 10:40ls
, has been tried to circumvent here and to solve here – Anthon Feb 24 '15 at 11:01