Search code examples
shellperlglob

How do you use globbing in perl for a one-liner with many files, avoiding xargs/find/etc


When there are too many matching files, shells like bash break if you include a glob pattern on the commandline like

perl -pi -e 's/hi/bye/' too_many_files*

You can work around this with xargs, gnu parallel, or find, but for complex commands, these can be difficult to get right in terms of quoting, and they can also be less efficient than running perl once.

Is there a way to use perl's built-in globbing support for something like this? (which does not work)

perl -pi -e 's/hi/bye/' 'manyfiles*' # <-- Does not work.

Solution

  • As noted in this answer, you can use a BEGIN block to have perl (rather than the shell) expand the file list:

    slightly modified from the original:

    Leave globing to perl instead of bash which has limitations,

    perl -pi -e 'BEGIN{ @ARGV = glob(pop) } s/#//g' "*"
    

    or when there are spaces in globed directory,

    perl -MFile::Glob=bsd_glob -pi -e 'BEGIN{ @ARGV = bsd_glob(pop) } s/#//g' "*"
    

    For more about glob vs bsd_glob, see this post.

    (This is intentionally duplicated as I had trouble finding the answer quickly with search terms I had in mind.)