LoginSignup
8
7

More than 5 years have passed since last update.

ls -1(数字の1)とは

Last updated at Posted at 2018-08-19

前書き

今回は、ちょっとしたコマンドの引数を紹介します。

ls -1

l(小文字のエル)では無く1(数字のイチ)です。
manコマンドにも記載されていない引数なので、意外と知らない方も多いのではないでしょうか。

lsコマンドとは

lsコマンドはディレクトリ内のファイル・ディレクトリの一覧を表示するコマンドです。引数を指定せずに実行すると以下のように表示されます。

[root@test01 log]# ls
anaconda  cron-20180819  maillog-20180819   secure-20180819   wtmp
audit     dmesg          messages           spooler           yum.log
boot.log  dmesg.old      messages-20180819  spooler-20180819
btmp      firewalld      ppp                tallylog
chrony    lastlog        rhsm               tuned
cron      maillog        secure             vmware-vmsvc.log
[root@test01 log]#

引数無しだと可読性に難があるため、表示を縦にするために-l(小文字のエル)という引数をよく使います1

[root@test01 log]# ls -l
合計 1108
drwxr-xr-x. 2 root   root     4096  8月 17 23:22 anaconda
drwx------. 2 root   root     4096  8月 17 23:24 audit
-rw-r--r--. 1 root   root     8576  8月 18 01:24 boot.log
-rw-------. 1 root   utmp        0  8月 17 23:18 btmp
drwxr-xr-x. 2 chrony chrony   4096 11月 15  2016 chrony
-rw-------. 1 root   root     3192  8月 19 17:01 cron
-rw-------. 1 root   root     9248  8月 19 03:25 cron-20180819
-rw-r--r--. 1 root   root   116035  8月 18 01:24 dmesg
-rw-r--r--. 1 root   root   116117  8月 17 23:46 dmesg.old
-rw-r--r--. 1 root   root      113  8月 17 23:46 firewalld
-rw-r--r--. 1 root   root   291708  8月 18 01:24 lastlog
-rw-------. 1 root   root        0  8月 19 03:25 maillog
~以下省略~
[root@test01 log]#

ただし、-lだとアクセス権などの付加情報も表示されてしまうため、単に縦に並べたい時やシェル内で加工する時に不便だったりします。その場合に最適なのが-1(数字のイチ)という引数です。

[root@test01 log]# ls -1
anaconda
audit
boot.log
btmp
chrony
cron
cron-20180819
dmesg
dmesg.old
firewalld
lastlog
maillog
~以下省略~
[root@test01 log]#

同じことをls -1を使用せずに実行しようとすると、以下のようにちょっと面倒になります2

[root@test01 log]# for i in `ls`;do echo $i;done
anaconda
audit
boot.log
btmp
chrony
cron
cron-20180819
dmesg
dmesg.old
firewalld
lastlog
maillog
~以下省略~
[root@test01 log]#

ファイルやディレクトリの一覧を、一旦ファイルに落とし込む時などに便利ですので、ぜひ活用してみてください。

追記

パイプやリダイレクトを使えばlsのみで1ファイル1行となるとのご指摘を頂きました。ありがとうございます。

[root@test01 log]# ls |cat
anaconda
audit
boot.log
btmp
chrony
cron
cron-20180819
dmesg
dmesg.old
firewalld
lastlog
maillog
~以下省略~
[root@test01 log]#

以上


  1. よく使うllコマンドはls -l のエイリアスコマンドです。 

  2. ls -lの出力にawkcutを使用することでも実現できます。 

8
7
2

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
8
7