LoginSignup
7
5

More than 5 years have passed since last update.

Bash 拡張の globstar ファイルパターンマッチ(`**`によるマッチ)のメモ

Last updated at Posted at 2016-03-05

要約: bash でも、ruby/python の glob (**)的なことができるよ!

$ tree
.
|-- a.txt
|-- b.log
`-- dir
    |-- c.txt
    |-- d.log
    `-- subdir
        `-- e.txt

2 directories, 5 files

# 普通の * は、ディレクトリの中までは展開しない
$ echo *
a.txt b.log dir

# 普通は、 ** を使っても意味がない
$ echo **
a.txt b.log dir

# ** を可能にする、 bash 拡張を on にするコマンド
# bash 4 以降で導入された模様。
$ shopt -s globstar

# 「0個以上のファイル・(サブ)ディレクトリ名」にマッチする
$ echo **
a.txt b.log dir dir/c.txt dir/d.log dir/subdir dir/subdir/e.txt

# 末尾に / がつくと、**/ 全体で「0個以上のディレクトリ名」にマッチする
$ echo **/
dir/ dir/subdir/

# 0 個以上、の意味
$ echo **/*.txt
a.txt dir/c.txt dir/subdir/e.txt

# ファイル名に完全マッチし得ない文脈で使うと、ただの * と同じような挙動っぽい
$ echo **.txt
a.txt
7
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5