Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am learning the shell commands and came across the short tags eg.[0-9],[[:digit:]] etc.. As a proof of concept i tried deleting all the files with the rm command(i know its not a good practise but i am trying to understand how things work),like this

rm [0-9].txt

there were two files in the directory 0.txt and 9.txt and it deleted the files 0.txt and 9.txt .I guessed that the expression [0-9] is expanded and then read as 0.txt 1.txt 2.txt .... However when you try only rm 5.txt,and the file does not exist, an error is thrown..

someone please tell me how the shorthand commands work when used with rm or ls.

share|improve this question
Don't guess how things are expanded - check. For example by prepending echo. echo rm [0-9].txt would show you. In Bash you can also use set -x to turn on debug mode, (and set -x to turn it off again). For expansion the TAB key may also work - type rm [0-9].txt<TAB> and it will show you the possibilities. – frostschutz Mar 23 '13 at 21:31
@frostschutz It's set +x to turn it off. – nyuszika7h Jun 16 '14 at 12:08
up vote 4 down vote accepted

This is called globbing (link to bash documentation, but this is not specific to bash).

When you ran rm [0-9].txt, the shell expanded [0-9].txt to the list of files present in the directory that matched that pattern. Then that list is passed as an argument to rm (each file as a separate argument).
So no, the shell didn't expand it to 0.txt 1.txt ... 9.txt, it looks at the files that matched.

Why you run just rm 5.txt, there is no glob pattern to expand, so the argument is passed as-is to rm, which notices that the file doesn't exist and complains.

Try something else: same command rm [0-9].txt, but in a directory that doesn't have any file that matches the pattern. You'll notice rm complains again, but this time it will say:

rm: cannot remove '[0-9].txt': No such file or directory

This is what happens (by default anyway) if a glob pattern doesn't match anything: the shell doesn't expand it and leaves it untouched.

POSIX reference for this sort of pattern matching: Pattern Matching Notation.

share|improve this answer
That was quick and precise.Thanks for the help.. – Works On Mine Mar 23 '13 at 21:43

Don't test with rm, test with echo!

[0-9].txt is a wildcard pattern (also called a glob). It expands to a list of existing file names. In a glob pattern, you can use three kinds of wildcards:

  • The character * matches any sequence of characters (including the empty sequence).
  • The character ? matches any one character.
  • The bracket expression […] matches any one character that appears within the brackets.
    [0-9] is a shortcut for [0123456789]. You can use a - between two characters inside brackets to mean those two characters and any two characters in between. For example, [0-9a-z_] matches a digit or a lowercase letter or an underscore.
    There are a few more rules regarding brackets that I won't mention here.

To reiterate, all those constructs only match existing files. [0-9].txt won't expand to 5.txt if there is no file with that name, any more than *.txt would expand to dfiojedfoi.txt if there is no file with that name.


$ ls
0.txt  10.txt  1a.txt  9.txt  hello.txt  world.txt
$ echo [0-9].txt
0.txt 9.txt
$ echo [0-9][0-9].txt
$ echo [0-9][0-9][0-9].txt
$ echo [0-9]?.txt
1a.txt 10.txt
$ echo [10-19].txt
0.txt 9.txt
$ echo *.txt
0.txt 1a.txt 9.txt 10.txt hello.txt world.txt
$ echo *l*
hello.txt world.txt

echo [0-9][0-9][0-9].txt just repeats the pattern because there is no matching file. Make sure you understand why [10-19].txt matched just those two files and not 10.txt or 19.txt.

share|improve this answer
Thanks for the detailed answer.. and for the echo recommendation. – Works On Mine Mar 23 '13 at 21:49
Also you can type echo [0-9]*.txt and press TAB twice for bash expansion (note the *) if your shell doesn't expands with echo. – jyz Mar 23 '13 at 22:40

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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