This discussion is archived
2 Replies Latest reply: Apr 7, 2010 10:10 AM by 807559 RSS

Need help to find a file with the find command

807559 Newbie
Currently Being Moderated
I have a list of files that are named
Comparison_200910.xls
Comparison_200912.xls
Comparison_201002.xls etc

I need a command that deletes all files with the file name less than or equal to 200909. So basically if the file name is Comparison_200910.xls it won't be deleted but if it were Comparison_200908.xls then it is going to be deleted. The directory has thousands of files dating back to 2006.

Can anyone help me with this task?

This is for Solaris 10.

Thank you,
  • 1. Re: Need help to find a file with the find command
    user4994457 Newbie
    Currently Being Moderated
    Do you need to use the 'find' command? It's not impossible, but it would be very tedious because you'd need to do text matching. It would be much easier to parse the file.

    Some options:

    Use 'find' to find files that might match, then use awk or something to check the number and pass on to do the actual delete if it matches.

    Use 'perl' to do all the above steps. Here's a sample one-liner.
    $ perl -MFile::Find -e 'find(sub {if (-f and /^Comparison_(\d{6})\.xls$/ and $1 <= 200909) { unlink $_; }}, ".")'
    --
    Darren
  • 2. Re: Need help to find a file with the find command
    807559 Newbie
    Currently Being Moderated
    Prehaps something like:
    % rm Comparison_2006[0-9][0-9].xls
    or
    % rm Comparison_200[678][0-9][0-9].xls
    will help?

    Then you could do the 2009 ones manually.

    Assuming the files were created on the dates
    indicated by the names,
    you might also be able to use the -mtime options
    to find to do the job,
    but I'd have to try it a few times
    to determine the right way to use it.

    Yet another way is to "ls *.xls >remove.sh",
    and then edit the file into the appropriate
    rm commands.
    This might be tedious, depending
    on your text editor.

    Hope that helps,
    Peter.