5

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.

2
  • This IS an option in ZSH as I can type in cat .file*.[t][x][t] and hit tab and it will auto fill all of it for me. I don't know what configuration I have in my .zshrc file but it is possible. I can't get this to work in bash. Zsh also allows you to combine multiple stars such as ls */*/. Commented Apr 7, 2016 at 3:22
  • I just noticed that all of this is about file globing patterns and not regex, strictly speaking. Gave my answer none the less :)
    – Lucas
    Commented Apr 7, 2016 at 16:56

2 Answers 2

6

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

6
  • 1
    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.
    – Wildcard
    Commented Apr 7, 2016 at 6:58
  • 1
    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.
    – Dubu
    Commented Apr 7, 2016 at 7:33
  • 1
    @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.
    – Lucas
    Commented Apr 7, 2016 at 8:45
  • @Lucas Seems to be a Debian problem. It works when I use your clean environment. I'm trying to find the difference ...
    – Dubu
    Commented Apr 7, 2016 at 8:48
  • 2
    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.)
    – Dubu
    Commented Apr 7, 2016 at 9:13
1

Braces are what you need

cat file000{1,2,3}.txt

This gets converted to:

cat file0001.txt file0002.txt file0003.txt
1
  • 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.
    – moo
    Commented Jul 12, 2019 at 5:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .