2015年04月21日 情報科学類 コンピュータリテラシ 筑波大学 システム情報系 情報工学域 新城 靖 <yas@cs.tsukuba.ac.jp>
このページは、次の URL にあります。
http://www.coins.tsukuba.ac.jp/~yas/coins/literacy-2015/2015-04-21
あるいは、次のページから手繰っていくこともできます。
http://www.coins.tsukuba.ac.jp/~yas/
http://www.cs.tsukuba.ac.jp/~yas/
木構造(tree structure)というのは、コンピュータ・サイエン ス(情報科学類で学ぶ学問)でよく使われる用語。 階層構造(hierarchical structure)ともいう。
木構造の例を、大学の組織を使って説明する(図?)。
図? 大学組織に見られる木構造
図? 大学組織に見られる木構造(領域的な見方)
筑波大学
コンピュータの中で、文字列(文字の並び)で木構造上の位置を表現する時に は、節が分かりやすくために、はっきりと区切りを入れて表現することがよく 行われる。
区切り文字としては、「.」(点)、「/」(スラッシュ)、「\」(バック スラッシュ)、「¥」(円記号)などがよく使われる。単語を並べる時に、木 の根に近いほうから書く流儀と遠い方から書く流儀がある。
コンピュータでは、次のような場所で木構造が使われている。
コンピュータ以外では、次のような場所で木構造が使われている。
http://www.softlab.cs.tsukuba.ac.jp/~yas/gen/it-2014-12-08/
図? 本物の木
図? ファイルとディレクトリの木
ルート・ディレクトリの名前は、「/
」 (スラッシュ 1 文字)。
ルートディレクトリから出発する方法で表記するパス名を、 絶対パス名(absolute path name)
絶対パス名は、ルートディレクトリを表す「/」の後に、 たどった枝の名前を並べ、間に区切りとして「/」をはさむ。
例:「/usr/bin/wc
」
usr
」という枝に進む
bin
」という枝に進む
wc
」という枝に進む
カレント・ワーキング・ディレクトリを表示するには、pwd (print working directory) コマンドを使う。
$ pwd
/USA/California
$
表示されている「/USA/California
」が絶対パス名で表示されたカレント・ワー
キング・ディレクトリの名前。
カレント・ワーキング・ディレクトリを変更するには cd
(change directory) コマ
ンドを使う。
$ cd dirname
「ディレクトリ dirname
に行く」とも言う。
$ pwd
/USA/California
$ cd /USA/Florida
$ pwd
/USA/Florida
$ cd /USA/California/San-Francisco
$ pwd
/USA/California/San-Francisco
$
カレントワーキングディレクトリは、名前「.
」で参照できる。
lsコマンドは引数にディレクトリを指定すると、そのディレクトリの中にある
ファイルの一覧を表示するが、引数を与えないと、「.
」が与えられたものとし
て働く。
$ ls
Los-Angeles San-Francisco
$ pwd
/USA/California
$ ls /USA/California
Los-Angeles San-Francisco
$ ls .
Los-Angeles San-Francisco
$
カレントワーキングディレクトリを起点としたパス名を
相対パス名(relative path name)という。
「/USA/California
」の時、
「San-Francisco
」は「/USA/California/San-Francisco
」、
「San-Francisco/China-Town
」は「/USA/California/San-Francisco/China-Town
」を意味する。
..
」で参照可能。
../ディレクトリ名
」で参照可能。
cd コマンドに引数を与えないと、ホーム・ディレクトリにもどる。
多くのシェル(csh,tcsh,bash,zsh
)
やEmacsなどでは、
ホームディレクトリを「~
」で指定できる
(指定できないプログラムもある。)
「〜
」は、英語の端末(ASCII,後述)の形。
日本語(JIS,後述) では、「 ̄
」となることがある。
他人のホーム・ディレクトリは、「~ユーザ名
」で指定できるプログラムがある。
(指定できないもプログラムもある。)
「~xxx
」と「~/xxx
」のように
「~
」直後に「/
」の有無で意味が違う。
ls -l コマンドを実行するとカレントディレクトリのファイルや ディレクトリの属性が表示される。
$ ls -l
total 99
drwx------ 4 yas prof 3072 4 10 17:37 Desktop
drwx------ 3 yas prof 1024 3 11 13:52 Documents
drwx------@ 34 yas wheel 2048 4 18 10:05 Library
drwx------ 20 yas prof 2048 4 18 10:57 Maildir
drwx------ 2 yas prof 1024 4 21 16:03 Music
-rw-r--r-- 1 yas prof 3178 4 15 15:56 file100.txt
-rw-r--r-- 1 yas prof 25 4 15 15:58 file30
-rw-r--r-- 1 yas prof 2550 4 15 15:59 literacy-a2.txt
-rw-r--r-- 1 yas prof 2550 4 15 15:57 literacy-a2.txt~
...
$
行単位に次のようなファイルやディレクトリの属性が表示さる。
drwx------,drwxr-xr-x,-rw-------
など)
6,4,17,... など
)
yas
)
prof
)
3072,1024,25
など)
4月10日17時37分
など)
Desktop
など)
ls -lu
で表示される。
ls -lc
で表示される「時刻」。
ファイルの型
モードの一番左1文字は、ファイルの型(type)を表わす。-
d
許可されたアクセス方法
モードからファイルの型を除いた部分はアクセスの可否を決めるための 情報。9文字ある。左から3文字の固まりが3組ある。各3文字はアクセス毎にその許可・拒否を表す。
r 読込み可 w 書込み可 x 実行可(ディレクトリの場合は探索可)モードで該当する部分が「
-
」の場合は、その種類のアクセスが許可さ
れてないことを意味する。
「読込み可」とは、その内容を参照できること意味する。たとえば、cp コマン ドでコピーできる。読出し可能なディレクトリなら、ls コマンドでそのディレ クトリ中のファイル名の一覧を表示できる。
「書込み可」とは、その内容を変更することができることを意味する。たとえ ば、テキスト・ファイルなら、エディタで修正したものを書き込むことができ る。書込み可能なディレクトリなら、mv コマンドでそのディレクトリのなかに あるファイル名前を変更できる。
「実行可」というのは、ファイルの内容がプログラムの場合は、 そのプログラムを実行することができる。
ディレクトリに対する 「検索可」というのは、その下にあるファイルやディレクトリを たどっていける(ファイルを開く(読み書きのため)、cd (change directory)できる)という意味である。
ディレクトリが「読込み可」でも、「検索可」でないと、 ディレクトリに「読込み可」のファイルがあっても、 ディレクトリに入ってファイルを読むことができない。 逆に、「検索可」でも、ディレクトリが「読込み可」でないと、 ディレクトリにあるファイル名やディレクトリ名を表示させることが できない。
そのディレクトリにあるファイル名を知っていて、そのファイルが「読み込み 可」なら読むことがでる。
アクセスするユーザによって異なったアクセスの許可・拒否がしたいことがあ る。そのために、rwxの指定は、ファイルの所有者、ファイルの属すグループ、 それ以外の人用に3セット用意されている。
例:モードが「rw-r--r--
」のファイル
rw-
」、
つまり「読み書きはできるが、実行はできない」
r--
」、つまり「読めるが書いたり実行したりはできない」
r--
」、つまり「読めるが書いたり実行したりはできない」
$ ls -l /etc/passwd
-rw-r--r-- 1 root wheel 5253 2 6 16:57 /etc/passwd
$
ls コマンドにディレクトリ名を与えると、ディレクトリそのものではなく、そ
の内容が表示される。
$ ls -l /home/lecture/syspro
total 27
drwxr-xr-x 2 syspro lecture 80 4 21 16:14 Applications
drwx------ 2 syspro lecture 1024 4 21 16:15 Desktop
drwx------ 13 syspro lecture 1024 4 21 16:15 Library
drwx------ 9 syspro lecture 1024 2 25 23:02 Maildir
drwx------ 2 syspro lecture 1024 4 21 16:15 Movies
drwx------ 2 syspro lecture 1024 4 21 16:15 Music
drwx------ 2 syspro lecture 1024 4 21 16:15 Pictures
drwx------ 12 syspro lecture 2048 4 21 16:15 _OLD_HOME_
drwxr-xr-x 4 syspro lecture 1024 3 13 15:31 public_html
drwxr-xr-x 11 syspro lecture 1024 3 13 15:27 syspro-2010
drwxr-xr-x 11 syspro lecture 1024 3 13 15:27 syspro-2011
drwxr-xr-x 11 syspro lecture 1024 3 13 15:27 syspro-2012
drwxr-xr-x 11 syspro lecture 1024 3 13 15:27 syspro-2013
$
ディレクトリ自身を表示したい時には、ls -l -d (ls -ld) とする。
ls -dl /home/lecture/syspro
drwxr-xr-x 19 syspro lecture 2048 4 21 16:15 /home/lecture/syspro
$
補完(completion)では、人間が目で確認するが、置き換えでは人間が確認する ことはない(見つからなければエラーになる)。
例:/usr/bin
にある at
で始まるファイルをすべて
ls
コマンドに引き渡したい。1つひとつ打つと疲れる。
$ ls -l /usr/bin/at /usr/bin/atos /usr/bin/atq /usr/bin/atrm /usr/bin/atsutil
-r-sr-xr-x 4 root wheel 75648 3 18 14:09 /usr/bin/at
-rwxr-xr-x 1 root wheel 90960 2 6 17:03 /usr/bin/atos
-r-sr-xr-x 4 root wheel 75648 3 18 14:09 /usr/bin/atq
-r-sr-xr-x 4 root wheel 75648 3 18 14:09 /usr/bin/atrm
-rwxr-xr-x 1 root wheel 25568 3 18 14:08 /usr/bin/atsutil
$
次のように、「*
」を使うと楽に打てる。
$ ls -l /usr/bin/at*
-r-sr-xr-x 4 root wheel 75648 3 18 14:09 /usr/bin/at
-rwxr-xr-x 1 root wheel 90960 2 6 17:03 /usr/bin/atos
-r-sr-xr-x 4 root wheel 75648 3 18 14:09 /usr/bin/atq
-r-sr-xr-x 4 root wheel 75648 3 18 14:09 /usr/bin/atrm
-rwxr-xr-x 1 root wheel 25568 3 18 14:08 /usr/bin/atsutil
$
この例では、シェルが「/usr/bin/at*
」を5つのファイル名に置き換え
ている。ls
が行っているのではない。ls
以外のどんなプログラ
ムでも有効である。
$ echo /usr/bin/at*
/usr/bin/at /usr/bin/atos /usr/bin/atq /usr/bin/atrm /usr/bin/atsutil
$ file /usr/bin/at*
/usr/bin/at: setuid Mach-O universal binary with 2 architectures
/usr/bin/at (for architecture x86_64): Mach-O 64-bit executable x86_64
/usr/bin/at (for architecture i386): Mach-O executable i386
/usr/bin/atos: Mach-O universal binary with 2 architectures
/usr/bin/atos (for architecture x86_64): Mach-O 64-bit executable x86_64
/usr/bin/atos (for architecture i386): Mach-O executable i386
/usr/bin/atq: setuid Mach-O universal binary with 2 architectures
/usr/bin/atq (for architecture x86_64): Mach-O 64-bit executable x86_64
/usr/bin/atq (for architecture i386): Mach-O executable i386
/usr/bin/atrm: setuid Mach-O universal binary with 2 architectures
/usr/bin/atrm (for architecture x86_64): Mach-O 64-bit executable x86_64
/usr/bin/atrm (for architecture i386): Mach-O executable i386
/usr/bin/atsutil: Mach-O 64-bit executable x86_64
$
echo は、引数をそのまま表示するコマンド、
file は、ファイルの種類を表示するコマンドである。
パタン | 意味 |
* | 任意の文字列(空でもよい)(.で始まるものを除く) |
? | 任意の1文字 |
[str] | strのなかの1文字。たとえば [aA] は、a か A とマッチする。「-」があると、ASCIIでその間の文字を意味する。たとえば [0-9](数字)や[a-zA-Z](アルファベット)がよく使われる。 |
{str1,str2,...} | 「,」で区切られたパタン str1, str2, ... を順にファイル名置換した結果を並べたもの |
~username | ユーザusernameのホーム・ディレクトリの絶対パス。 |
~/ | 自分自身のホーム・ディレクトリ |
~ | 自分自身のホーム・ディレクトリ |
よく使われる形式
*
」と「*.*
」は違う。Windowsで「*.*
」と書く
所、Unixでは、「*
」で十分なことが多い。
$ ls -ld /Applications/QuickTime Player.app
ls: /Applications/QuickTime: No such file or directory
ls: Player.app: No such file or directory
$
「?」や「*」で置き換え可能な場合が多い。
$ ls -ld /Applications/QuickTime*Player.app
drwxr-xr-x+ 3 root wheel 102 3 18 14:11 /Applications/QuickTime Player.app
$ ls -ld /Applications/QuickTime?Player.app
drwxr-xr-x+ 3 root wheel 102 3 18 14:11 /Applications/QuickTime Player.app
$
バックスラッシュ「\ 」や" " を使う方法もある。
$ ls -ld /Applications/QuickTime\ Player.app
drwxr-xr-x+ 3 root wheel 102 3 18 14:11 /Applications/QuickTime Player.app
$ ls -ld "/Applications/QuickTime Player.app"
drwxr-xr-x+ 3 root wheel 102 3 18 14:11 /Applications/QuickTime Player.app
$
ls
, ls -l
, ls -ld
, ls -a
cat
, head
, lv
cp
cd
、pwd
mkdir
, rmdir
mv
rm
ls
コマンドは、標準では、カレント・ワーキング・ディレクトリを意
味する「.
」や親ディレクトリを意味する「..
」を含めて、
「.
」から始まるファイル名を表示しない。-a
オプションを付
けると、「.
」から始まるファイル名も表示する。ホーム・ディレクト
リには、「.
」で始まるファイルがいくつか存在する。
$ ls
Desktop Library Music Public WinFiles
Documents Movies Pictures Sites public_html
$ ls -a
. .cshrc~ Movies
.. .emacs Music
.CFUserTextEncoding .emacs.d Pictures
.DS_Store .login Public
.Spotlight-V100 .profile Sites
.Xauthority .ssh WinFiles
.backupfiles2006 Desktop public_html
.bashrc Documents
.cshrc Library
$
ls
コマンドは、引数にディレクトリ名が与えられると、ディレクトリ
の内容を表示する。
$ ls -l ~/Maildir
total 113
drwx------ 2 yas prof 35840 4 18 11:00 cur
drwx------ 2 yas prof 17408 4 18 10:57 new
drwx------ 2 yas prof 1024 4 18 11:37 tmp
$
ディレクトリそのものを表示したい時(特に「-l
」と組み合わせて日付
等の属性を表示したい時)は、「-d
」オプションを付ける。
$ ls -ld ~/Maildir
drwx------ 20 yas prof 2048 4 18 10:57 /home/prof/yas/Maildir
$
$ cp file1 file2 dir
この場合、dir 以下に、(一番葉の部分だけ)同じ名前のファイルが作られる。
次の操作と概ね同じ動作をする。
$ cp file1 dir/file1
$ cp file2 dir/file2
ディレクトリを指定する時には、カレント・ワーキング・ディレクトリ 「.」 や ホーム・ディレクトリ「〜」も使える。
$ cp /etc/group .
$ cp /etc/group ~
$ cp ~yas/public_html/htdocs/coins/literacy-2015/2015-04-21/literacy-a100.txt .
ファイルを消すには、rm (remove) コマンドを用いる。
$ rm file1
このファイル file1 が削除される。
rm コマンドで消してしまったファイルは、一般的には2度と戻らない。 消す前に、本当に不要かどうかを確認すること。
コマンド名の置換え(alias)等て、rm を rm -i に変えることは、一般的には勧
められない。「rm -i y」という操作が、一連の操作として身に付い
てしまうので。
ファイル名を変更するには mv (move) コマンドを使う。
$ mv oldname newname
この結果、ファイルoldfileのファイル名をnewnameに変更される。
ここで、newname のファイルが存在した場合、元の newname は消されてしまう。
$ mkdir dirname
この結果、dirname
という名前のディレクトリが作らる。
mvコマンドは、ディレクトリの名前を変更するために使える。
$ mkdir dir1
$ ls
dir1
$ mv dir1 dir2
$ ls
dir2
$
$ mkdir ~/dir1
$ mv oldname ~/dir1/newname
この結果、カレント・ワーキング・ディレクトリにある
ファイルoldfileのファイル名は、
~/dir1/newnameに変更される。
次のように、ディレクトリだけを指定することもできる。
$ mv name ~/dir1/
この場合、次のものと同じになる。
$ mv name ~/dir1/name
また、変更先にディレクトリを指定する mv では、ファイル名は1個でなくて
も複数でもよい。
$ mv file1 file2 file3 ~/dir1
シェルの
ファイル名置換
を使うと便利。
$ ls file[1-3]
(ファイル名の確認)
$ mv file[1-3] ~/dir1
$ ls file?
(ファイル名の確認)
$ mv file? ~/dir1
$ ls file*
(ファイル名の確認)
$ mv file* ~/dir1
$ rmdir dirname
この結果、
ディレクトリdirnameが削除される。
空でないディレクトリは、rmdir コマンドでは 「Directory not empty」 というエラー・メッセージが表示され削除できない。 この場合は、子供のディレクトリやファイル を削除してからもう一度削除する。
No such file or directory
Permission denied
No space left on device
Disc quota exceeded
$ man man
$ man ls
$ man mkdir
$ man -k keyword
引数として調べたいコマンドの名前を与える。-k に続いてキーワードを与える
ことで検索もできる。
後述する Emacs に付属の説明書 info を読むためのコマンドもある。
$ info info
$ info bash
Emacs でファイル名を打つ時、補完機能を使うと便利である。 ファイル名を自分で打たなくても、Emacs が自動的に補ってくれる。
Emacs の補完機能(タブキー)を確認しなさい。
C-x C-f
でファイルを開く
/etc/passwd
をタブキーにより開く例である。
$ emacs ![[←]](../icons/screen-return.gif)
C-x C-f
Find file: ~/![[]](../icons/screen-cursor.gif)
~/
」の2文字を消す。Delete キーを 2 回打つ。
Find file: ![[]](../icons/screen-cursor.gif)
/e
」と打ちタブを打つ
Find file: /e![[tab]](../icons/screen-tab.gif)
すると、tc/
が補完される。
Find file: /etc/![[]](../icons/screen-cursor.gif)
pa
」と打ちタブを打つ
Find file: /etc/pa![[tab]](../icons/screen-tab.gif)
すると、いくつか候補が表示される。
In this buffer, type RET to select the completion near point.
Possible completions are:
pam.d/ passwd
passwordreset/ paths
paths.d/
<中略>
Find file: /etc/pa
s
」と打ちタブを打つ
Find file: /etc/pas![[tab]](../icons/screen-tab.gif)
すると、「wd
」が補完される。
Find file: /etc/passwd![[]](../icons/screen-cursor.gif)
Find file: /etc/passwd![[←]](../icons/screen-return.gif)
$ cd ~
$ ls
$ ls -a
$ ls -l
$ ls -la
$ ls -d
$ ls -dl
$ ls -adl
$ info info
$ info bash
$ info emacs
$ cd ディレクトリ名
$ pwd
$ ls
$ ls -l
(1) 引数無しで cd コマンドを実行
$ cd /
$ pwd
$ cd
$ pwd
(2) cd コマンドに、「~
」を与える。
$ cd /
$ pwd
$ cd ~
$ pwd
(3) cd コマンドに、「~ログイン名
」を与える。
$ cd /
$ pwd
$ cd ~ログイン名
$ pwd
$ ls -l
ディレクトリの場合、左端が「d
」になっている。
(
ファイルの属性
参照
)
cd コマンドを使って、カレント・ワーキング・ディレクトリを
ホーム・ディレクトリの下のディレクトリに変更しなさ
い。以下は、Music
に変更した例である。
$ cd
$ pwd
$ ls
$ cd Music
$ pwd
$ ls
Music
も含めて次のようなディレクトリについても、類似の操作を行い
なさい。
$ ls -lR ディレクトリ名
lv による表示。後ろに「| lv」をつける。
$ ls -lR ディレクトリ名 | lv
注意: 表示が止まらなくなった時、強制終了したい時には、
Control キー + c キー
も使える。
tree コマンドに -N オプションをつけると、ファイル名に漢字を含むものも表 示できる。
$ tree -N ディレクトリ名
lv による表示。後ろに「| lv」をつける。
$ tree -N ディレクトリ名 | lv
注意: 表示が止まらなくなった時、強制終了したい時には、
Control キー + c キー
も使える。
iTerm にペーストする時に「?」が表示されるならば、次のように すれば (UTF-8ではなく) ASCII 文字を使うようになる。
$ LANG=C tree -N ディレクトリ名
tree コマンドの次のオプションの意味を ls と比較しなさい。
$ open /Applications/Calculator.app
これを、ファイル名置換機能(*,?)を利用して、長い文字列をキーボードから打
たないで実行しなさい。
$ echo /App*/Cal*
(1つしか表示されない確認する。複数あると問題がある。)
$ open /App*/Cal*
同様に、/Applications にあるプログラムを実行してみなさい。
ls f1 f2 f3 ファイルの名前の一覧の表示 ls dir ディレクトリの内容の表示 ls カレント・ワーキング・ディレクトリの内容の表示 ls -d d1 d2 d3 ディレクトリの名前の一覧の表示 ls -a dir ディレクトリの内容の表示(.で始まる名前も表示) ls -l f1 f2 f3 ファイルの属性の表示 ls -ld d1 ls -l -d の組み合わせ pwd カレント・ワーキング・ディレクトリの表示 cd dir カレント・ワーキング・ディレクトリの変更 cd ホーム・ディレクトリへ移動 cat f1 ファイルの内容の表示 cat > f1 ファイルの作成 head f1 ファイルの内容の表示(最初の10行) cp f1 f2 ファイルのコピー cp f1 f2 f3 dir ファイルを指定されたディレクトリへコピー cp f1 f2 f3 . ファイルをカレント・ワーキング・ディレクトリへコピー rm f1 f2 f3 ファイルの削除 mv old new ファイルの名前を変える mv f1 f2 f3 dir ファイルの名前を変える(dir以下への移動) mkdir dir ディレクトリの作成 mkdir -p d1/d2/d3 ディレクトリの作成(親も自動的に作成) rmdir dir ディレクトリの削除(空のディレクトリの時) f1, f2, f3 はファイル名、dir, d1, d2, ... は、ディレクトリ名
cp ~yas/public_html/htdocs/coins/literacy-2015/2015-04-21/literacy-a4.txt .注意: このコピーは、1度だけ行うこと。2度以上行うと、それまでに行った編 集内容は失われる。
$ ls
(literacy-a4.txtが存在することを確認する)
$ emacs literacy-a4.txt
作成したファイルを、 レポート提出ページから提出しなさい。
正誤表
誤: 文字集合ISO 10648(Unicode)
正: 文字集合ISO 10646(Unicode)
誤: (6) [加点] ...木構造の表記方法としては、(8) と同じ方法(字下げ)を使いなさい。
正: (6) [加点] ...木構造の表記方法としては、(5) と同じ方法(字下げ)を使いなさい。