This might be in essence a duplicate of this question, however the perl script provided did not work in my case as far as I can tell. There are also many questions about finding multiple patterns, but they are surprisingly difficult to search through because Google doesn't seem to make a difference between a "file of patterns" and a "pattern of filename".

I would like to emulate the behaviour of

git status --ignored --porcelain | awk '{print $2}'

in a non-repository folder. That is, I would like to list files and directories which match the patterns listed in a (specified) file, with the following constraints:

  • the patterns are written with the gitignore syntax, in particular empty lines and comments should be ignored by the script;
  • the script should walk from the current working directory;
  • the script should not walk into matched directories (ie, a match is always a leaf), or into symbolic links.

Here is a simple example:

$> cat .gitignore 

$> tree -a .
|-- .DS_store
|-- .gitignore
|-- a
|   |-- b
|   |   |-- foo
|   |   |   `-- file
|   |   `-- tmp
|   |-- foo
|   `-- tmp
|       `-- file
`-- c
    |-- .DS_Store?
    `-- foo

5 directories, 8 files

$> git status --ignored --porcelain | awk '{print $2}'

If possible at all, I would be particularly interested in solutions which work both on osx and linux, and which can be extended to consider patterns in posix-egrep format instead. For that last part, something similar to find . -print | egrep -f patternfile could work, although this would walk into matching directories, which could potentially make things very slow.

  • 1
    Please edit your question and add an example of a directory and the listing you would like to see. Assume we don't know what the git command does (and we shouldn't need to know some other command in order to understand your question). – terdon Feb 23 '17 at 12:18
  • 1
    This is exactly the threshold at which you'd give up on shell and write the thing in Perl or Python. – Satō Katsura Feb 23 '17 at 12:37
  • @terdon Is the example I included helpful, or too complicated? – Sheljohn Feb 23 '17 at 14:09
  • @SatoKatsura That is fine with me, I was just reluctant about reinventing the wheel, since git-status already does what I want. I was wondering whether it was possible to run the command in a non-repository, and if I have to implement this myself, if there was a library/package you would recommend for parsing gitignore syntax? – Sheljohn Feb 23 '17 at 14:11
  • The example is fine, but are you sure about your results? Why is c/.DS_Store? shown if *.DS_Store* is in .gitignore? Why is a/b/foo/ shown if you are ignoring a/**/foo? I also don't understand what you mean by "script should not walk into matched directories". You seem to be descending into subdirs there. – terdon Feb 23 '17 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.