概要
最近LPICレベル1の取得に向けて勉強を始めました。
今回は,半年前(入社して4カ月)の私がこれを知っていたら良かったというテーマで書いています
LinuxはOS?
Linuxは何かとWindowsやmacOSと並べて話に出てくることが多いため,当時はOSの一つと思い込んでいました
厳密な定義の話をすると,狭義の意味でLinuxとはカーネル(OSの中核部分)であり,メモリ管理や入出力管理などOSにとって,特に重要な機能を担う部分です
一方で広義の意味でOSとしてのLinuxを指すこともあります
「Linux」という言葉で話すときは,どういうものを指して,その言葉を使っているのかを考える必要があります
所感ですが,「Linux」という言葉が出るときはOSの(広義)の意味で使っていることが多いように感じます
Linuxディストリビューションってなんだ
Linuxディストリビューションとは,Linux(カーネル)にライブラリなどを追加して,UNIXに似たような動きをするようにされたOSのことを指します。
WindowsやmacOSと並べて話されるのは,このLinuxディストリビューションのことを指しています。
代表的なものにRed Hat社が開発する**Red Hat系ディストリビューション(CentOSなど)やDebian Projectが開発するDebian系ディストリビューション(ubuntuなど)**があります
以降はLinuxディストリビューションを総称して,LinuxOSと書きます
コマンドやオプションが覚えられないとき
LinuxOSの操作はCUI(Command Line Interface)が基本であり,マウスなどを使ったGUI(Graphical User Interface)による操作に慣れている人にとっては,少々難しいです。
たとえば,ディレクトリ内のファイル一覧を見るにはlsコマンドを使います
LinuxOSをうまく操作するには,lsのようなコマンドやそれらの動作に条件を付けるオプションを覚える必要がありますが,なかなか覚えられなかったりします
そんなときにはmanコマンドと**--help**オプションが便利です
$ man ls
NAME
ls - list directory contents
SYNOPSIS
ls [OPTION]... [FILE]...
DESCRIPTION
List information about the FILEs (the current directory by default). Sort entries alphabetically if none of
-cftuvSUX nor --sort is specified.
Mandatory arguments to long options are mandatory for short options too.
-a, --all
do not ignore entries starting with .
(略)
$ ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
Mandatory arguments to long options are mandatory for short options too.
-a, --all do not ignore entries starting with .
-A, --almost-all do not list implied . and ..
--author with -l, print the author of each file
-b, --escape print C-style escapes for nongraphic characters
--block-size=SIZE with -l, scale sizes by SIZE when printing them;
e.g., '--block-size=M'; see SIZE format below
-B, --ignore-backups do not list implied entries ending with ~
-c with -lt: sort by, and show, ctime (time of last
modification of file status information);
with -l: show ctime and sort by name;
otherwise: sort by ctime, newest first
-C list entries by columns
--color[=WHEN] colorize the output; WHEN can be 'always' (default
if omitted), 'auto', or 'never'; more info below
-d, --directory list directories themselves, not their contents
-D, --dired generate output designed for Emacs' dired mode
-f do not sort, enable -aU, disable -ls --color
-F, --classify append indicator (one of */=>@|) to entries
--file-type likewise, except do not append '*'
--format=WORD across -x, commas -m, horizontal -x, long -l,
single-column -1, verbose -l, vertical -C
--full-time like -l --time-style=full-iso
-g like -l, but do not list owner
(略)
manコマンドや**-help**オプションで指定したコマンドの説明やそのコマンドのオプションを表示してくれます
コマンド名やオプションは基本的に動作を示す単語の略であることが多いです
ただの文字列としてコマンドやオプションを覚えるよりも,その意味を知ることでかなり覚えやすくなります
例えば,現在の作業ディレクトリを表示するコマンドにpwdコマンドがあります
これをただの,「pwd」として覚えるか「print working directory」の略として覚えるかだと,記憶に残りやすいのは後者だと思います
コマンドの由来はこちらのページで見やすくまとめられています
エイリアス
lsコマンドの -lオプションはディレクトリ内のファイルの権限や所有者を表示してくれるオプションでとても便利なオプションです。
~/testdir# ls
test1.txt test2.txt
~/testdir# ls -l
total 0
-rw-r--r-- 1 root root 0 Jan 14 22:10 test1.txt
-rw-r--r-- 1 root root 0 Jan 14 22:10 test2.txt
このようによく使うコマンドにはエイリアス(別名)をつけることで,より簡単に操作を行うことができます
エイリアスをつけるには,aliasコマンドを使います
~/testdir# alias ll='ls -l'
~/testdir# ll
total 0
-rw-r--r-- 1 root root 0 Jan 14 22:10 test1.txt
-rw-r--r-- 1 root root 0 Jan 14 22:10 test2.txt
ファイルの検索
ファイルの検索にはlocateコマンドが便利です
[root@6f07fde89980 testdir]# ls
testFile1.txt testFile2.txt
[root@6f07fde89980 testdir]# locate testFile
/testdir/testFile1.txt
/testdir/testFile2.txt
似た動作を示すものにfindコマンドがありますが,locateコマンドの方が高速に検索をすることができます
これはfindコマンドはリアルタイムにファイルの検索を行うのに対し,locateコマンドはファイルパスが格納されたDBに対し,検索をかけるためです
したがって,locateコマンドでは作成直後のファイル(ファイルパスがDBに登録されていないファイル)は検索できないことがあります
ファイルパスのDBを最新にするには,updatedbコマンドを使います
locateコマンドは,LinuxOSに最初からインストールされていないこともあります
ubuntuでapt-getコマンド,centosではyumコマンドでインストールすることができます
# apt-get install mlocate
# yum install mlocate