Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am looking for a way to build a list of files by parsing a file that contains a sequence of zsh globbing patterns.

The final goal is to pass this list of files to hg add in mercurial. For those interested in this specific part, my question is motived by this other question that I asked in StackOverflow:

A sample file (let's call it .hginclude) would look as follows:


and the output from the script should be a list of files obtained by collecting the output of

print globbing_pattern

on every line, where globbing_pattern refers to each line in the file .hginclude above.

Ultimately, the list of files should be a list that I can pass to hg add to track those files in mercurial.

The assumption for this part is that I have an .hgignore with the following:

syntax: glob

which means: don't track anything unless explicitly added via an hg add command.

share|improve this question

See hg help patterns. Mercurial supports reading file names from a file, so you can do

$ hg add listfile:myfiles.txt

and have myfiles.txt contain the file names to add. What's even more interesting for you is that a "file name" in Mercurial can be a glob pattern. So you can have myfiles.txt contain


etc, and the hg add command above will do the right thing.

You can even use file sets to select the files. This is a find-like language that lets you do things like

set:exec() and not binary()

to select executable files that are not binary, i.e., scripts.

The above commands will respect the .hgignore file and so you cannot use them directly to solve your problem. However, you can use

$ hg status --all --no-status listfile:myfiles.txt

to get a list of files matches by the patterns, including any files that would normally be ignored. You can then proceed to use this file list with hg add:

$ hg status -A -n -0 listfile:myfiles.txt | xargs -0 hg add
share|improve this answer
Thanks Martin. This looks exactly like what I need. One problem I noticed is that if I have syntax: glob * in my .hgignore (as I explained in the original post) then hg add listfile:myfiles.txt does not add the files, whereas if I them one by one, e.g. hg add .zshrc it works. Do you know why? – Amelio Vazquez-Reina Dec 14 '11 at 21:00
Yes, I just checked the source and unfortunately, patterns are implemented so that they respect the .hgignore file. I did not think of that when I gave the answer. I'll raise the issue on the mailinglist. – Martin Geisler Dec 15 '11 at 8:37

Try this:

 hg add `for i in `cat myfiles.txt` ; do ls $i ; done`

where myfiles.txt contains the globs. But of course the suggestion of initrpc is cleaner.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.