Regular expression for hidden files under unix

自作多情 提交于 2020-01-30 06:51:09

问题


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.


回答1:


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.




回答2:


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.




回答3:


The following one-liner will do the work (hide hidden and tilde ("~") terminated files):

set(CPACK_SOURCE_IGNORE_FILES "/\\\\..*$;~$;${CPACK_SOURCE_IGNORE_FILES}")



回答4:


You need to escape it.

^\..*



回答5:


Try this:

^[.].*


来源:https://stackoverflow.com/questions/727075/regular-expression-for-hidden-files-under-unix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!