Bash – Difference between * and ?* glob in Bash

bashwildcards

  • * is roughly a wild card character with a unlimited amount of length.
  • ? is roughly a wild card character for one or zero length.

Is there a difference between using * vs ?* when searching for strings in bash?

Best Answer

The difference is that in bash (as you tagged the question) * matches any string with length zero or more characters, while ?* matches a string with at least 1 character. Consider for example two files: file.txt and xfile.txt and try to list them with ls ?*file.txt or ls *file.txt.

One real case scenario when I use such construct is to list hidden files. Very often I just do

ls .??*

Double question marks are here to prevent listing the current directory . and the parent directory .., like it would be with a simpler form ls .*.


I need to point here that my .??* is not perfect; for example filenames with only two characters, like .f, don't match this pattern. More reliable solution is ls {..?,.[!.]}*, but usually that is too much to type for me.

Related Question