#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2008
    Posts
    22
    Rep Power
    0

    Listing files using pattern matching


    I have a requirement to list files using find command

    My folder contains below list of files with out extention.

    ABC.123.MNO.1234
    ABC.123.1234
    ABC.123.7890
    ABC.123.MNO.654
    KLM.123.MNO.456

    File with extension

    ABC.123.1234.txt

    I have a requirement to exclude only ABC.123.* type files and list others. Even though files having MNO contains this pattern i should not exclude. Even if file ends with .txt or .doc it should not be excluded. That is ABC.123.1234.txt should not be excluded.

    So I am writing my find command as

    find -type f ! -name "ABC.[0-9].[0-9]*^."

    But I am not getting what is required. Can any one please let me know if I am doing wrong any where. As per my requirement I cannot use grep, -regex, or -regex attributes to find command.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2008
    Posts
    22
    Rep Power
    0
    any thoughts...please help
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0
    facing same sought of problem as my team lead had gave this work to me and started shunting please can anyone help me too also
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,451
    Rep Power
    1751
    Originally Posted by reference2me
    I have a requirement to list files using find command

    ...

    But I am not getting what is required. Can any one please let me know if I am doing wrong any where. As per my requirement I cannot use grep, -regex, or -regex attributes to find command.
    Any reason why you are not allowed to use -regex, etc.? That makes it smack of homework.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2008
    Posts
    22
    Rep Power
    0
    Simon,

    Actually there are many find based conditions in the script..i guess..the only thought might be is that using regex may disturb others..however, now it is fine. I was approved to use regex.

    So, I tried using like this.

    find -type f ! -regex "ABC.[0-9].[0-9]*^."

    Still it is not working. Can you help me.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2008
    Posts
    22
    Rep Power
    0
    i tried as below but still not working

    find -type f ! -regex "/ABC.[0-9].[0-9]*^./"

    please help
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2008
    Posts
    22
    Rep Power
    0
    Please let me know if there is any wrong in the syntax
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,451
    Rep Power
    1751
    Sorry not to have responded - had some timing and power issues ...!
    If I find (pardon the pun!) that I have to use negation when doing things, what I often do is invert the negation and start simple, just to prove what I'm trying actually works.
    What you will find is that the -regex operator deals with the entire path string returned by the find which is unlike the -name operator which acts purely and wholly on the name.
    Assuming a directory content of:
    Code:
    ABC.123.1234
    ABC.123.1234.txt
    ABC.123.7890
    ABC.123.7890.txt
    ABC.123.MNO.1234
    ABC.123.MNO.1234.txt
    ABC.123.MNO.654
    ABC.123.MNO.654.txt
    KLM.123.MNO.456
    KLM.123.MNO.456.txt
    My initial stab is as follows. It is almost certainly wrong since trying to exclude from exclusions can be a little hard to follow!
    Code:
    find . -maxdepth 1 -regextype posix-basic -type f ! -regex "^./ABC.*.[0-9]$" -o -name "*MNO*"
    ./ABC.123.MNO.1234
    ./ABC.123.MNO.654
    ./KLM.123.MNO.456
    ./ABC.123.MNO.1234.txt
    ./ABC.123.1234.txt
    ./ABC.123.7890.txt
    ./ABC.123.MNO.654.txt
    ./KLM.123.MNO.456.txt
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc

IMN logo majestic logo threadwatch logo seochat tools logo