Wednesday, April 20, 2011

Regular expression for hidden files under unix.

I'm looking for a regex to match every file begining with a "." in a directory.

I'm using CMake (from CMake doc : "CMake expects regular expressions, not globs") and want to ignore every file begining with a dot (hidden files) BUT "\..*" or "^\..*" doesn't work :(

The strange thing : this works (thanks to rq's answer) and remove every hidden files and temp files ("~" terminated files)

file(GLOB DOT ".*")
file(GLOB TILD "*~")

set (CPACK_SOURCE_IGNORE_FILES "${DOT};${TILD}")

But I can't find the right thing to write directly into CPACK_SOURCE_IGNORE_FILES to have the same result!

Here is the "doc" of this variable.

From stackoverflow
  • Using standard regex syntax:

    ^\..*
    

    Since CMake apparently doesn't like this, it may use something like:

    ^\\..*
    

    That's just a guess, though, since I don't have/use CMake.

    claferri : I need a regex to use in CMake, your expression gives me the result : "CMake Error: Invalid escape sequence \."
    Sergej Andrejev : maybe you nead to add slashes /^\..*/
    Hank Gay : CMake is doing you wrong, then. That's the standard regex syntax to do what you asked. Maybe it requires you to use ^\\..* instead?
    claferri : doesnt work neither :(
  • You need to escape it.

    ^\..*
    
  • Try this:

    ^[.].*
    
    claferri : doesn't work neither (but doesn't produce error)
  • Sounds like GLOB is probably what you want.

    Try this. Open a file "test.cmake" and add the following:

    file(GLOB ALL "*")
    file(GLOB DOT ".*")
    file(GLOB NOTDOT "[^.]*")
    
    message("All Files ${ALL}")
    message("Dot files ${DOT}")
    message("Not dot files ${NOTDOT}")
    

    Then create a couple of test files:

    touch .dotfile
    touch notdot
    

    Then run "cmake -P test.cmake". The output is:

    All Files /tmp/cmake_test/.dotfile;/tmp/cmake_test/notdot;/tmp/cmake_test/test.cmake
    Dot files /tmp/cmake_test/.dotfile
    Not dot files /tmp/cmake_test/notdot;/tmp/cmake_test/test.cmake
    

    This was tested with cmake 2.6.0.

    claferri : Even if "CMake expects regular expressions, not globs", your solution help me do what I want using CMake variables to list all files I want to ignore. What is weird : I can't find the right thing to write directly into the variable in order to avoid listing the file using your solution. Thx anyway!

0 comments:

Post a Comment