Is there a way to use regex expressions to auto-fill filenames in bash?

6,288

Solution 1

The feature you are looking for is there. You are just missing a * in your example. Type cat file000[1-3]*ESC* and it should work. I think this is the case because the readline function insert-completions (which is bound to ESC*) will not expand the glob pattern if it does not match any files. And without the last * it does not match the files.

You can read about this in the man page, section "EXPANSION" subsection "Pathname Expansion".

Solution 2

Braces are what you need

cat file000{1,2,3}.txt

This gets converted to:

cat file0001.txt file0002.txt file0003.txt
Share:
6,288

Related videos on Youtube

moo
Author by

moo

Updated on September 18, 2022

Comments

  • moo
    moo over 1 year

    Assuming I have a directory with files as shown:

    $ ls
    file0001.txt file0002.txt file0003.txt file0004.txt file0005.txt someotherfile.txt
    

    Lets say I want to run the following command:

    $ cat file0001.txt file0002.txt file0003.txt file0004.txt file0005.txt 
    

    I could achieve this using a bash shortcut as follows to auto-fill the file names: cat file000 ESC *

    Now would it be possible to use a shortcut in a similar way to only autofill according to some regex (regular expression)? For example: cat file000[1-3] ESC * to get:

    $ cat file0001.txt file0002.txt file0003.txt
    

    Edit: The regex I should have used above for this example to make more sense: file000[1-3].txt or file000[1-3]*

    Just to be clear my question is about how to auto-fill on the bash with regex. And NOT how I can cat some files together using a bash script or for/while statements using regex.

  • Wildcard
    Wildcard about 8 years
    It's worth mentioning that it's not necessary to "insert completions" to run the command; the shell will expand globs before the command sees them anyway.
  • mleonard
    mleonard about 8 years
    Cannot reproduce. Under Bash 4.3.42 (Debian sid), a cat file000[1-3]*[Esc][*] expands only to the first matching file, under Bash 4.2.37 (Debian 7.5) there is no expansion at all. If I press [Enter] instead of [Esc][*], the correct files are cated. In both cases with default shopt settings.
  • Lucas
    Lucas about 8 years
    @Dubu sorry I don't know why. I'm on Arch Linux with Bash 4.3.42(1)-release. I tried it even without a ~/.inputrc file and a clean environment: env --ignore-environment bash --norc --noprofile and bind -p|grep -F '*' prints "\e*": insert-completions among other lines.
  • mleonard
    mleonard about 8 years
    @Lucas Seems to be a Debian problem. It works when I use your clean environment. I'm trying to find the difference ...
  • mleonard
    mleonard about 8 years
    Okay, apparently Debian's bash completion settings overrule something so that [Esc][*] does not work as intended. (I assume the solution can be found somewhere along the almost 2000 lines of the /usr/share/bash-completion/bash_completion file, but I do not have the time to debug.)
  • moo
    moo about 8 years
    Lucas and Dubu thank you for the very useful info. I am also trying this on one of my debian based machines (Ubuntu 14.04) and I am having the same problem as Dubu. Might just accept this as answer if I don't find a better solution for debian. And sorry about the regex mistake Lucas ;)
  • moo
    moo almost 5 years
    Not exactly what I was looking for. From my post: "Just to be clear my question is about how to auto-fill on the bash with regex. And NOT how I can cat some files together using a bash script or for/while statements using regex.". Whats key here is that I need the command to auto-fill. If you try cat file000 ESC * you will see the kind of auto-fill I'm looking for.