Suppose I want to type something like this:

rm build/* tmp/*

I am about to run it but I want to expand the wildcards before executing so I can verify what is about to happen. How can I do this?

This is the workflow I am looking for:

$ rm build/* tmp/* <press some magic key>
$ rm build/foo build/bar tmp/wiz tmp/buzz

(Edit) Another example workflow:

$ # Check what is in directory: 
$ ls -0
$ # I want to delete foo1 and foo2, but want to do so carefully: 
$ rm f*o <press some magic key>
$ rm foo1 foo2

I can expand other things like this:

cd `hg root` <Ctrl-x>
cd /users/bob/myMercurialRepository

(Because I have Control-x shell-expand-line in my ~/.inputrc).

I dug though man bash looking at the descriptions of globbing but couldn't find anything interesting.

  • 1
    It depends on the editing mode of your shell. For example I am using vi-mode and type <Esc> and * and the wildcards will get expanded. – Janis Apr 10 '15 at 23:13
  • One of the things I like about zsh is that this works out of the box, using the <tab> key. Same with */ recursive functionality, which I think does not always work with bash. – Keith Bennett Apr 15 '16 at 9:49

It depends on the editing mode of your shell. For example I am using vi-mode and type Esc and * and the wildcards will get expanded.

  • 1
    Indeed, in the default emacs mode, you can Ctrl+X * – cpugeniusmv Apr 13 '15 at 22:55

How about

echo rm build/* tmp/*

Or, on my version of bash, hitting tabtab immediately after the wildcard gives me a list of matching files.

To "delete files carefully", I would recommend either using the interaction flag to rm, or again prefixing with echo

echo rm -i f*o
rm -i f*o

Admittedly neither will give you the list up front as preferred, though.

  • That isn't exactly the behavior I am looking for. You can enter rm b*ld and see that b*ld matches build. – sixtyfootersdude Apr 13 '15 at 16:54
  • @sixtyfootersdude true, but not what you asked for in your Question – roaima Apr 13 '15 at 17:27
  • updated the question to clarify. – sixtyfootersdude Apr 13 '15 at 19:52

Actually the behavior is the following (as of O'Reilly's "Learning the bash Shell").

  1. Esc\ will complete up to the longest common prefix if there is more than one match, or complete the file/directory/... name if there's only one match (adding a / if it is a directory).
  2. Esc* behaves similarly, except that it lists all completions if there is more than one.
  3. Esc= echoes what you would obtain with Esc* and gives back what you typed, without changes.

As an example (I've tried myself), given a directory containing the files

aaabbb  aaaccc  aaaddd
  1. ...$ vi aEsc\ would produce ...$ vi aaa• and leave you in insert mode at the .
  2. ...$ vi aEsc* would produce ...$ vi aaabbb aaaccc aaaddd • and leave you in insert mode at the (as far as I've tried, it adds no / if it is a directory).
  3. ...$ vi aEsc= would echo aaabbb aaaccc aaaddd and give you back ...$ vi a, leaving you in command mode.

It is clear that the trailing * in the regex you used is implied; you could have written

rm build/ tmp/

and then pressed Esc* on both (yes, as far as I can tell and try, Esc* expands all the wildcards only in the space-delimited word the cursor is on).

But if you put in one wildcard, then the last one is no more implied and you have to supply it explicitly. Indeed, if there is a file named aaazzzwww, then writing a*w, aaazzzw, a*z*, or a*w* and pressing Esc\ would complete to aaazzzwww, but a*z wouldn't!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.