Home > Linux, SLES, SSH > ls xargs rm

ls xargs rm

June 4th, 2012

I keep forgetting the proper xargs arguments when I need them.
My intention today was to list old backup files based on the year time stamp and remove them.
Here is the one liner that works specifically because I have named my backups with the dd-mm-yyyy convention:

[root@webserver1 httpd]# ls -latf|grep 2009|xargs -L 1 rm

Here is a better approach that is independent from the file naming convention:

[root@webserver1 httpd]# ls -lat|grep 2008|awk ‘{print $9}’
ssl_access_log
ssl_request_log

The above can be easily fed to xargs for a rm.

HTH,

St0ma

Categories: Linux, SLES, SSH Tags:
  1. June 6th, 2012 at 14:13 | #1

    Your use of xargs can lead to nasty surprises because of the separator
    problem http://en.wikipedia.org/wiki/Xargs#The_separator_problem

    It is, however, fine for logfiles, where you know exactly what the files are called.

  2. June 6th, 2012 at 21:45 | #2

    Hi Ole,
    Spot on. I am never sure with xargs so I suggest people to test before moving forward with script implementations.
    St0ma

Comments are closed.