ソートされたスペース(大文字と小文字を区別しない)を持つファイルのリストを繰り返すにはどうすればよいですか?

ソートされたスペース(大文字と小文字を区別しない)を持つファイルのリストを繰り返すにはどうすればよいですか?

私はbashを使ってOSXでこれをやっていますが、明らかにすべてのbashルールが使用されるわけではないので、あなたの提案が私に役立つことを願っています:)

次のファイルがあります

  1. fun bar1.txt
  2. Foo bar2.tXT(大文字のXTです)
  3. fun fun.txt

私の目的は、ソートされた方法でファイルのリストを繰り返すことですか?

それは次のとおりです。

for i in (my-sorted-file-list); do
    echo $i
done

これを行う方法についてのアイデアはありますか?ありがとう

ベストアンサー1

とても簡単です:

for i in *; do
  echo "<$i>"
done

これはbashのファイルワイルドカードを使用します。 bashはすでにパス名拡張子をソートしているため、ソートは必要ありません。
からman bash

   Pathname Expansion
       After word splitting, unless the -f option has been set, bash scans each word for
       the characters *, ?, and [. If one of these characters appears, then the word is
       regarded as a pattern, and replaced with an alphabetically sorted list of file
       names matching the pattern.

 

結果の例:

$ touch 'fun bar1.txt' 'Foo bar2.tXT' 'fun fun.txt'

$ for i in *; do echo "<$i>"; done
<Foo bar2.tXT>
<fun bar1.txt>
<fun fun.txt>

ソート順序はユーティリティLC_COLLATEと同じです。sort大文字と小文字を区別してソートするには、を使用しますLC_COLLATE=en_US.utf8。大文字と小文字を区別してソートするには、を使用しますLC_COLLATE=C

返品man bash

   LC_COLLATE
          This variable determines the collation order used when sorting the results
          of pathname expansion, and determines the behavior of range expressions,
          equivalence classes, and collating sequences within pathname expansion and
          pattern matching.

おすすめ記事