github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/docs/commands/g.md (about)

     1  # `g`
     2  
     3  > Glob pattern matching for file system objects (eg `*.txt`)
     4  
     5  ## Description
     6  
     7  Returns a list of files and directories that match a glob pattern.
     8  
     9  Output is a JSON list.
    10  
    11  ## Usage
    12  
    13  ```
    14  g pattern -> <stdout>
    15  
    16  [ <stdin> -> ] @g command pattern [ -> <stdout> ]
    17  
    18  !g pattern -> <stdout>
    19  
    20  <stdin> -> g pattern -> <stdout>
    21  
    22  <stdin> -> !g pattern -> <stdout>
    23  ```
    24  
    25  ## Examples
    26  
    27  Inline globbing:
    28  
    29  ```
    30  cat @{ g *.txt }
    31  ```
    32  
    33  Writing a JSON array of files to disk:
    34  
    35  ```
    36  g *.txt |> filelist.json
    37  ```
    38  
    39  Writing a list of files to disk:
    40  
    41  ```
    42  g *.txt -> format str |> filelist.txt
    43  ```
    44  
    45  Checking if a file exists:
    46  
    47  ```
    48  if { g somefile.txt } then {
    49      # file exists
    50  }
    51  ```
    52  
    53  Checking if a file does not exist:
    54  
    55  ```
    56  !if { g somefile.txt } then {
    57      # file does not exist
    58  }
    59  ```
    60  
    61  Return all files apart from text files:
    62  
    63  ```
    64  !g *.txt
    65  ```
    66  
    67  Filtering a file list based on glob matches:
    68  
    69  ```
    70  f +f -> g *.md
    71  ```
    72  
    73  Remove any glob matches from a file list:
    74  
    75  ```
    76  f +f -> !g *.md
    77  ```
    78  
    79  ## Detail
    80  
    81  ### Pattern Reference
    82  
    83  * `*` matches any number of (including zero) characters
    84  * `?` matches any single character
    85  
    86  ### Inverse Matches
    87  
    88  If you want to exclude any matches based on wildcards, rather than include
    89  them, then you can use the bang prefix. eg
    90  
    91  ```
    92  » g READ*
    93  [
    94      "README.md"
    95  ]
    96  
    97  » !g *
    98  Error in `!g` (1,1): No data returned.
    99  ```
   100  
   101  ### When Used As A Method
   102  
   103  `!g` first looks for files that match its pattern, then it reads the file list
   104  from STDIN. If STDIN contains contents that are not files then `!g` might not
   105  handle those list items correctly. This shouldn't be an issue with `frx` in its
   106  normal mode because it is only looking for matches however when used as `!g`
   107  any items that are not files will leak through.
   108  
   109  This is its designed feature and not a bug. If you wish to remove anything that
   110  also isn't a file then you should first pipe into either `g *`, `rx .*`, or
   111  `f +f` and then pipe that into `!g`.
   112  
   113  The reason for this behavior is to separate this from `!regexp` and `!match`.
   114  
   115  ## Synonyms
   116  
   117  * `g`
   118  * `!g`
   119  
   120  
   121  ## See Also
   122  
   123  * [`f`](../commands/f.md):
   124    Lists or filters file system objects (eg files)
   125  * [`match`](../commands/match.md):
   126    Match an exact value in an array
   127  * [`regexp`](../commands/regexp.md):
   128    Regexp tools for arrays / lists of strings
   129  * [`rx`](../commands/rx.md):
   130    Regexp pattern matching for file system objects (eg `.*\\.txt`)
   131  
   132  <hr/>
   133  
   134  This document was generated from [builtins/core/io/g_doc.yaml](https://github.com/lmorg/murex/blob/master/builtins/core/io/g_doc.yaml).