LoginSignup
0
3

More than 5 years have passed since last update.

ls コマンドを使わずにファイル一覧を取得

Last updated at Posted at 2017-06-04

ls コマンドは echo で代用できます。

# ls
$ echo *

# ls -a
$ echo .* *

ただし、ファイル名に区切り文字 (space:\x20) が含まれる場合を考慮する必要があります。

$ echo "foo" > "bar baz.txt"

# NG
$ echo *.txt | xargs cat
cat: bar: No such file or directory
cat: baz.txt: No such file or directory

# OK
$ printf "%s\n" *.txt | while read x; do cat "$x"; done
foo

stat コマンドを使うと出力フォーマットを細かく調整できますが、ls コマンド同様、GNU/BSD で実装が異なる点に注意が必要です。

# GNU stat
$ stat -c "%A %n" .* *

# BSD stat
$ stat -f "%Sp %N" .* *

単にOS判定で切り替えると、macOS (BSD) ユーザが、Homebrew で GNU coreutils を入れて alias を定義しているケース等でうまく動きません。

alias ls="gls --color"
alias stat="gstat"

See Also

0
3
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
0
3