Bash – way to use regex expressions to auto-fill filenames in bash

autocompletebashcommand linefilenamesregular expression

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.

Best Answer

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".

Related Question