1.1. Linux コマンド早見表/Linux commands cheet-sheet

このページでは、たくさんあるLinuxのコマンドのうち、特に基本的な操作 である、ファイルの一覧、移動、削除といったファイル関連のコマンドの 使い方の例を示します。ざっと見渡せるように、説明は最小限にとどめます。 Linux操作がはじめてという人は、まずこれらをマスターしましょう。

コマンドライン操作を本でしっかり勉強してみたいという方には、 [三宅2015][中島2018] といった本をお勧めします。

In this page, the most basic commands are shown, which are the file related commands, such as listing, copying, moving, deleting files. To make it easy to glimpse through, minimal explanation is given. If you are a beginner to linux, these are the commands you should learn first.

1.1.1. ファイルに関するコマンド/commands on files

1.1.1.1. ls ディレクトリの中身を見る/list the contents of a directory

カレントディレクトリの中身を見る:

list the contents of the current working directory:

% ls
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos  snap

隠しファイル (ファイル名が”.” で始まるファイル)も含めて全てのファイルを見る:

list ALL files, including hidden files (files with names starting with a period):

% ls -a
.          .bash_history  .dbus   .profile   Documents  Pictures   Videos
..         .bashrc        .gnupg  .synaptic  Downloads  Public     snap
.aptitude  .cache         .local  Desktop    Music      Templates

ブロック数で数えたファイルの長さを表示に含める:

Include the file length counted in terms of disk blocks:

% ls -s
合計 36
4 Desktop    4 Downloads  4 Pictures  4 Templates  4 snap
4 Documents  4 Music      4 Public    4 Videos

バイト数で数えたファイルの長さや、持ち主などの詳細な情報を含む長形式で表示する:

List in LONG form, including the file length in bytes, and owner names:

% ls -l
合計 36
drwxr-xr-x 2 root root 4096 12 30  2017 Desktop
drwxr-xr-x 2 root root 4096 12 30  2017 Documents
drwxr-xr-x 2 root root 4096 12 30  2017 Downloads
drwxr-xr-x 2 root root 4096 12 30  2017 Music
drwxr-xr-x 2 root root 4096 12 30  2017 Pictures
drwxr-xr-x 2 root root 4096 12 30  2017 Public
drwxr-xr-x 2 root root 4096 12 30  2017 Templates
drwxr-xr-x 2 root root 4096 12 30  2017 Videos
drwxr-xr-x 7 root root 4096 12 31  2017 snap

普通のファイルなのかディレクトリなのかを区別する記号をつけて表示する:

Append a character to the file name representing the file type such as a directory:

% ls -F
Desktop/    Downloads/  Pictures/  Templates/  snap/
Documents/  Music/      Public/    Videos/

色をつけて表示する:

Use colors:

% ls --color=auto

複数のオプションを組み合わせて表示する:

Combine multiple options at once:

% ls --color=auto -Fs

どんなオプションがあるのか調べる:

Check what options are available:

% ls --help
% man ls

1.1.1.2. pwd 現在のディレクトリを確認する/print the current working directory

シェルの現在の作業ディレクトリを表示する:

Print the shell’s current working directory:

% pwd
/home/takahashi

pwd = print working directory

1.1.1.3. cd ディレクトリの移動/change the working directory

サブディレクトリに移動する:

go to a subdirectory:

% cd subdirectory_name

親ディレクトリに移動する:

go to the parent directory:

% cd ..

ホームディレクトリに移動する:

Go to your home directory:

% cd

どこか奥まったディレクトリで作業しているときに、間違えてcdにディレクトリ名を 指定しそびれると、この形式になってしまってホームディレクトリに移動してしまう ので、注意しましょう。

When you are working in some deep place in the directory tree, and use cd by mistake without an argument, this form will take effect and you will be taken right back to your home directory, so be careful.

相対パス名で別のディレクトリに移動する:

go to another directory using a relative path name:

% cd docs/chapter1/section2

絶対パス名で別のディレクトリに移動する:

go to another directory using an absolute path name:

% cd /usr/share

1.1.1.4. mkdir ディレクトリの作成/creating directories

ディレクトリを一つ作成する:

make one directory:

% mkdir my_project

ディレクトリを複数作成する:

make a couple of directories at once:

% mkdir include src

末端のディレクトリに加えて、存在していない親ディレクトリも作成する:

make not just the target directory but also any not-yet-created PARENT directories as well:

% mkdir -p my_project/src

1.1.1.5. cp ファイルのコピー/copying files

コピー元、コピー先、それぞれのファイル名を指定してコピーする(gitの管理下のファイルの削除はgitのコマンドに依る) :

Copy one file specifying the source file name and the destination file name (Use git commands for files managed by git):

% cp source_file_name destination_file_name

コピー先ディレクトリ名を指定してコピーする:

Copy one or more files specifying the source file names and one destination directory name:

% cp file_name1 file_name2 ...  directory_name

ディレクトリの中身まで、再帰的にコピーする:

Copy directory contents RECURSIVELY:

% cp -r source_directory_name destination_directory_name

1.1.1.6. ワイルドカード/wildcards

拡張子 .h で終わるファイル全てを include サブディレクトリにコピーする:

Copy all files that ends with “.h” into subdirectory “include”:

% cp *.h include

ワイルドカードはシェルの機能なので、どのコマンドでも作用する。

The wildcard expansion function is provided by the shell, so it applies to any command.

1.1.1.7. mv ファイルの名称変更や移動/renaming and moving files

ファイル名だけを変更する、あるいは格納先ディレクトリを変更する、あるいは両者を一度に変更する(gitの管理下のファイルの削除はgitのコマンドに依る):

Change the file name, or change the directory where the file belongs, or change both at once.(Use git commands for files managed by git):

% mv old_file_name new_file_name
% mv old_directory/file_name new_directory/file_name
% mv old_directory/old_file_name new_directory/new_file_name

複数のファイルを別のディレクトリに移動させる:

Move multiple files into one directory:

% mv file_name1 file_name2 ... directory_name

1.1.1.8. rm ファイルの削除/deleting (removing) files

ファイルを削除する(gitの管理下のファイルの削除はgitのコマンドに依る):

Remove files(Use git commands for files managed by git):

% rm file_name1 file_name2 ...

1.1.1.9. テキストファイルの作成や編集/creating and editing text files

テキストファイルの作成にはテキストエディタを使います。 「テキストエディタ」というのは、ワープロソフトのように、テキスト ファイルを編集するためのプログラムです。ワープロソフトと異なり、 文字データそのものを編集する能力しか持たず、フォントを細かく 変えたりすることはできません。

To create a text file, you use a text editor program. A “text editor” is a program for typing text files, just like a word processor. Unlike a word processor program, you can enter and edit text data only, and there is no such thing as changing the font or color.

エディタにはいろいろなものがあります。 テキストエディタ/Text Editors を参照。

There are many available text editors. See テキストエディタ/Text Editors.

nanoエディタでファイル名を指定してテキストエディタを起動する:

Invoke the ‘nano’ text editor giving the file name:

% nano hello.c

1.1.1.10. less テキストファイルの中身の確認/checking the content of text files

テキストファイルを表示させ、キー操作で前後にスクロールさせる:

Show the content of a text file and provide keys to scroll:

% less hello.c

主なキー操作:

  • q 終了

  • SPACE 1ページ進める

  • b 1ページ戻す

  • g 先頭に戻る

  • G 末尾に進む

  • /文字列[ENTER] 末尾に向けて文字列を検索する

  • ?文字列[ENTER] 先頭に向けて文字列を検索する

Commonly used functions:

  • q quit

  • SPACE move one page forward(towards the end of file)

  • b move one page backward

  • g go to the top of the file

  • G go to the end of the file

  • /pattern[ENTER] search a pattern towards the end of the file

  • ?pattern[ENTER] search a pattern towards the beginning of the file

1.1.2. シェルの変数/shell variables

1.1.2.1. シェル変数・環境変数/shell variables and environment variables

シェル変数 に値を設定するには、「変数名=値」と書きます。 変数名と”=”の間に空白をいれてはいけません:

To assign a value to a shell variable, use the form “NAME=VALUE”. Do not put a space before “=”:

SOMEVAR=123
OPTIONS="-Fs"
MORE_OPTIONS="$OPTIONS --color=auto"

シェル変数の参照には、「$変数名」と書きます:

To get the value of a shell variable, use the form “$VAR_NAME”:

DESTDIR=../../some/distant/directory
cp my_source_file.c $DESTDIR

“export シェル変数名” というコマンドにより、値を設定済みのshell変数を、 環境変数 に変換することができます:

With the command “export variable-name”, an existing shell variable can be converted to an environment variable:

export SOMEVAR

1.1.2.2. 変数の内容を表示する/showing variable values

echoコマンドに渡す引数をそのままオウム返しに表示する(echo backする):

The echo command will “echo back” the arguments:

% SOMEVAR="one two three"
% echo $SOMEVAR
one two three
% echo The value of SOMEVAR is $SOMEVAR
The value of SOMEVAR is one two three

echoコマンドに引数が渡される手前で、shellが$SOMEVARの値を展開し、 echoコマンドは渡された引数をそのまま表示するので、中身を確認できます。

The shell will replace “$SOMEVAR” with the content of SOMEVAR, and the echo command will receive the replaced result as arguments so the content of the variables will be shown. In this way, echo can be used to check the value of shell variables.

1.1.2.3. 引数につける引用符/quotes on command arguments

空白を含む引数をコマンドに渡すには、ダブルクォートを使います:

Use double quotes to pass arguments that include spaces:

% echo These words will be separate commandline arguments.
% echo "But these words will be a single commandline argument."

ダブルクォートの中でも、変数の展開は行われます:

Variables references is active inside double quotes:

% SOMEVAR=one two three
% echo "The value of SOMEVAR is now : $SOMEVAR"
The value of SOMEVAR is now : one two three

変数の展開も抑止したい場合(「$」と表示したい場合など)は、 シングルクォートを使います:

When you want to inhibit variable references, (e.g. when you want to print the “$” character), use single quotes.:

% echo 'Use the notation $VARNAME to get the value of VARNAME.'
Use the notation $VARNAME to get the value of VARNAME.

引用符の中で引用符を書きたい場合には、その文字の前にエスケープ文字の バックスラッシュまたは円マーク(文字コードは同じものだが、端末や フォントの設定によって表示が変わる)をつけます。:

echo 'Inside single quotes, \'$VARNAME\' will not be expanded.'

1.1.3. 環境変数の設定/Setting environment variables

1.1.3.1. 環境変数PATHを一時的に設定する/temporarily set the environment variable PATH

すでに定義されているPATHの値に加えて、ホームディレクトリ「$HOME」の下のbinディレクトリ を追加する例:

Append the “bin” directory inside your home directory to the environment variable PATH:

% export PATH=$PATH:$HOME/bin
  • export は、変数が環境変数であることを宣言するキーワード

  • 代入の右辺の$PATHは、もとのPATHの値に展開される

  • $HOME はホームディレクトリのパス名に展開される

シェルに直接exportと入力するこの方法では、ターミナルを追加で開いたり、 次回ログインした際には設定が失われるので、一時的な設定で構わないときに使います。

The above method of typing in “export” to a shell will have the effect only in that shell, and when you open a new terminal or login again, the setting will be lost. So use this style when a temporary setting is enough.

1.1.3.2. 環境変数PATHを、常に有効になるように設定する/permanently set the environment variable PATH

環境変数への設定をシェルの初期化ファイルに書いておくと、ログイン時に 毎回設定されるようになります。

ホームディレクトリ直下の .profileファイルにテキストエディタでPATHへの代入を書き加えます。

Open or create the .profile file in your home directory with a text editor and add an assignment to PATH.

# This file is .profile
export PATH=$PATH:$HOME/bin

変更が有効になるのは、このファイルがシェルに読み込まれるタイミングです。 macosの場合は、ターミナルウィンドウを閉じて開き直せば、その時点で読み込まれます。 Linuxの場合は、ログインしなおした時点で有効になります。

The change will take effect when the shell reads it. For macos, when you close the terminal window and reopen it, the file will be read. For linux, the file will be read on the next login.

シェルの初期化スクリプト/Shell startup scripts に、さらに詳しい説明があります。

See also シェルの初期化スクリプト/Shell startup scripts.

1.1.4. history コマンド履歴/command history

コマンドの履歴を表示する:

show the command history:

% history

番号を指定して履歴の中のコマンドを再度実行する:

run a command in the history specifying the history number:

% !73

入力したコマンドの先頭の数文字を入力して、コマンドを再実行する:

run a command in the history specifying the first few letters of the command line:

% !ma

この例では、「ma」で始まる、もっとも最近入力したコマンド(“make”など)が再度実行される。

In this case, the most recent command that starts with “ma” will be run (e.g. “make”).

1.1.5. ジョブ管理(バックグラウンド実行)/Job control(background execution)

ジョブ管理の概念については ジョブ制御/Job control 参照。 (スパコンでのプログラム実行の仕組みにも「ジョブ」という言葉が出てきますが、別のものです。)

See ジョブ制御/Job control for a description on job control.

バックグラウンドジョブとしてコマンドを起動する:

Invoke a command as a background job:

% firefox &

実行中のフォアグラウンドジョブをサスペンドする:

Suspend the current foreground job:

Ctrl-z

サスペンド中、およびバックグラウンド中のジョブの一覧を表示する:

Show all jobs that are suspended or running in the background:

% jobs

サスペンドしたジョブを再びフォアグラウンドジョブとして処理再開させる:

Resume the suspended job as a foreground job:

% fg

サスペンドしたジョブをバックグラウンドジョブとして処理再開させる:

Resume the suspended job as a background job:

% bg