【Shell】ワイルドカード(*)や任意の文字を含む/含まないファイル名の検索

■今回やりたいこと

同一階層(/work/tmp)に存在するファイルについて、「 ls 」コマンドを使用し、特定のファイルのみを表示する。
※オプションとして1行ずつ表示する[ -1 ]をつける。

同一階層(/work/tmp)に存在するファイル名一覧

ファイル名概要
samplefile.a拡張子が[.a]
samplefile.c拡張子が[.c]
samplefile.h拡張子が[.h]
samplefile.txt拡張子が[.txt]
samplefile.conf拡張子が[.conf]
samplefile1.conf拡張子が[.conf]
ファイル名の末尾に[1]
samplefile2.conf拡張子が[.conf]
ファイル名の末尾に[2]
samplefile3.conf拡張子が[.conf]
ファイル名の末尾に[3]
samplefile4.conf拡張子が[.conf]
ファイル名の末尾に[4]
samplefileA.conf拡張子が[.conf]
ファイル名の末尾に[A]
samplefileB.conf拡張子が[.conf]
ファイル名の末尾に[B]
samplefileC.conf拡張子が[.conf]
ファイル名の末尾に[C]
samplefileD.conf拡張子が[.conf]
ファイル名の末尾に[D]
samplefile-.conf拡張子が[.conf]
ファイル名の末尾に[-]
samplefile!.conf拡張子が[.conf]
ファイル名の末尾に[!]
samplefile].conf拡張子が[.conf]
ファイル名の末尾に[ ” ] ” ]
samplefile^.conf拡張子が[.conf]
ファイル名の末尾に[^]

▼ディレクトリ・ファイル構成図

■ファイル名全量を取得

▼実行コマンド

ls -1 /work/tmp/

▼実行結果

samplefile!.conf
samplefile-.conf
samplefile.a
samplefile.c
samplefile.conf
samplefile.h
samplefile.txt
samplefile1.conf
samplefile2.conf
samplefile3.conf
samplefile4.conf
samplefileA.conf
samplefileB.conf
samplefileC.conf
samplefileD.conf
samplefile].conf
samplefile^.conf

■拡張子が任意の1文字、3文字のファイル名を検索

拡張子が任意の1文字のファイル名を検索する場合は、ファイル名.の後ろに「 ? 」をつける。

▼実行コマンド

ls -1 /work/tmp/samplefile.?

▼実行結果

/work/tmp/samplefile.a
/work/tmp/samplefile.c
/work/tmp/samplefile.h

拡張子が任意の3文字のファイル名を検索する場合は、ファイル名.の後ろに「 ? 」を3つつける。

▼実行コマンド

ls -1 /work/tmp/samplefile.???

▼実行結果

/work/tmp/samplefile.txt

■拡張子が[.conf]のファイル名を検索

拡張子が[.conf]のファイル名を検索する場合は、ファイル名をワイルドカード「 * 」をつける。

▼実行コマンド

ls -1 /work/tmp/*.conf

▼実行結果

/work/tmp/samplefile!.conf
/work/tmp/samplefile-.conf
/work/tmp/samplefile.conf
/work/tmp/samplefile1.conf
/work/tmp/samplefile2.conf
/work/tmp/samplefile3.conf
/work/tmp/samplefile4.conf
/work/tmp/samplefileA.conf
/work/tmp/samplefileB.conf
/work/tmp/samplefileC.conf
/work/tmp/samplefileD.conf
/work/tmp/samplefile].conf
/work/tmp/samplefile^.conf

■ファイル名、拡張子に任意の文字を含む/含まないファイル名を検索

拡張子が[c]または[h]のファイル名を検索する場合は、ファイル名.の後ろに[ch]を指定する。

▼実行コマンド

ls -1 /work/tmp/samplefile.[ch]

▼実行結果

/work/tmp/samplefile.c
/work/tmp/samplefile.h

ファイル名の末尾に[1から3]を含むファイル名を検索する場合は、ファイル名の後ろに[1-3]を指定する。

▼実行コマンド

ls -1 /work/tmp/samplefile[1-3].conf

▼実行結果

/work/tmp/samplefile1.conf
/work/tmp/samplefile2.conf
/work/tmp/samplefile3.conf

ファイル名の末尾に[AからC]を含むファイル名を検索する場合は、ファイル名の後ろに[A-C]を指定する。

▼実行コマンド

ls -1 /work/tmp/samplefile[A-C].conf

▼実行結果

/work/tmp/samplefileA.conf
/work/tmp/samplefileB.conf
/work/tmp/samplefileC.conf

ファイル名の末尾に[-]を含むファイル名を検索する場合は、ファイル名の後ろに[-]を指定する。
★[-]、[A]、[C]と複数検索する場合、[-]は先頭もしくは最後に指定する

▼実行コマンド

ls  -1 /work/tmp/samplefile[-AC].conf    # ①
ls  -1 /work/tmp/samplefile[AC-].conf    # ②

▼実行結果(①・②ともに結果は同じ)

/work/tmp/samplefile-.conf
/work/tmp/samplefileA.conf
/work/tmp/samplefileC.conf

/work/tmp/samplefile-.conf
/work/tmp/samplefileA.conf
/work/tmp/samplefileC.conf

ファイル名の末尾に1から3を含まないファイル名を検索する場合は、ファイル名の後ろに[!]もしくは[^]を指定する。
★含まない条件で検索する場合、[!]、[^]は先頭に指定する

▼実行コマンド

ls -1 /work/tmp/samplefile[!1-3].conf    # ①
ls -1 /work/tmp/samplefile[^1-3].conf    # ②

▼実行結果(①・②ともに結果は同じ)

/work/tmp/samplefile!.conf
/work/tmp/samplefile-.conf
/work/tmp/samplefile4.conf
/work/tmp/samplefileA.conf
/work/tmp/samplefileB.conf
/work/tmp/samplefileC.conf
/work/tmp/samplefileD.conf
/work/tmp/samplefile].conf
/work/tmp/samplefile^.conf

/work/tmp/samplefile!.conf
/work/tmp/samplefile-.conf
/work/tmp/samplefile4.conf
/work/tmp/samplefileA.conf
/work/tmp/samplefileB.conf
/work/tmp/samplefileC.conf
/work/tmp/samplefileD.conf
/work/tmp/samplefile].conf
/work/tmp/samplefile^.conf

ファイル名の末尾に[!]もしくは[^]を含むファイル名を検索する場合は、ファイル名の後ろに[!]もしくは[^]を指定する。
★先頭に[!]、[^]を指定すると含まない判定になってしまうため、エスケープ[\]をつける
または、他の検索文字の後ろに[!]、[^]を指定する

▼実行コマンド

ls -1 /work/tmp/samplefile[\!^1].conf    # ①
ls -1 /work/tmp/samplefile[1!^].conf    # ②

▼実行結果(①・②ともに結果は同じ)

/work/tmp/samplefile!.conf
/work/tmp/samplefile1.conf
/work/tmp/samplefile^.conf

/work/tmp/samplefile!.conf
/work/tmp/samplefile1.conf
/work/tmp/samplefile^.conf

ファイル名の末尾に” ] ”を含むファイル名を検索する場合は、以下のように必ず最初に” ] ”を指定する。

▼実行コマンド

ls -1 /work/tmp/samplefile[]A].conf

▼実行結果(①・②ともに結果は同じ)

/work/tmp/samplefileA.conf
/work/tmp/samplefile].conf
タイトルとURLをコピーしました