Edit I missed the point of ls -t
there.
Might I suggest doing it much simpler, e.g.
find /home/backups \
-type f -iregex '.*\.t?gz$' \
-mtime +60 -exec rm {} \;
which will delete any matching file older than a specific age (60 days, in the example)
You used tail
but haven't told it to look for null-delimiters.
Regardless, here is a util that you could use to return the last 0-delimited element:
#include <string>
#include <iostream>
#include <cstdio>
int main(int argc, const char *argv[])
{
std::cin.unsetf(std::ios::skipws);
if (! (freopen(NULL, "wb", stdout) && freopen(NULL, "rb", stdin) ))
{
perror("Cannot open stdout/in in binary mode");
return 255;
}
std::string previous, element;
while (std::getline(std::cin, element, '\0'))
{
previous = element;
// if you have c++0x support, use this _instead_ for performance:
previous = std::move(element);
}
std::cout << previous << '\0' << std::flush;
}
Use it as
find /home/backups -type f \( -name \*.tgz -o -name \*.gz \) -print0 | ./mytail | xargs -0 rm