コマンドラインの基本
Linux を初めて使用するユーザーにとって、コマンド ラインは見慣れないもの、あるいは怖くさえあるものかもしれません。しかし実際には、コマンド ラインは Linux システムで最も強力で柔軟なツールです。基本的なコマンド ライン操作をマスターすると、システム管理が効率化されるだけでなく、Linux が内部でどのように動作するかを理解するのにも役立ちます。
この記事では、ゼロからターミナルとシェルの概念を理解し、最も一般的に使用されるコマンドを習得し、ファイル パス、パイプ、アクセス許可などの基本的な知識を学習します。
ターミナルとシェルとは何ですか
ターミナル
ターミナルは、テキスト コマンドを入力してコンピュータと対話できるウィンドウを提供するアプリケーションです。 Ubuntu デスクトップ環境では、ターミナルは黒い (または暗い) 背景のウィンドウです。
Ubuntu 26.04 のデフォルトのターミナル アプリケーションは Ptyxis です。従来の GNOME ターミナルを置き換え、より優れたコンテナ統合を提供します。必要に応じて、従来の GNOME ターミナル をインストールすることもできます。
より最新の GPU アクセラレーション端末を試したい場合は、Ghostty が 26.04 ユニバース リポジトリのプレビュー パッケージとして利用可能です。
sudo apt install ghosttyGhostty はデフォルトの端末ではありません。 OpenGL 4.3 をサポートしていないデバイスでは、ソフトウェア レンダリングのフォールバックが必要になる場合があります。
シェル
シェル は、実際にコマンドを処理する端末の背後にあるプログラムです。ターミナルにコマンドを入力すると、シェルがコマンドを解析して実行します。 Ubuntu のデフォルトのシェルは Bash (Bourne Again Shell) です。
一般的なシェルには次のものがあります。
| シェル | 説明 |
|---|---|
| バッシュ | Ubuntu のデフォルトのシェル、機能が豊富で互換性が高い |
| ズシュ | オートコンプリートとテーマが改善された、より現代的なシェル |
| 魚 | すぐに使える構文強調表示を備えた使いやすいシェル |
| ダッシュ | Ubuntu がシステム スクリプトに使用する軽量シェル |
次のコマンドを使用して、現在のシェルを確認できます。
echo $SHELLターミナルを開く方法
Ubuntu 26.04 では、ターミナルを開く方法がいくつかあります。
方法 1: キーボード ショートカット
最も速い方法は Ctrl + Alt + T を押すことです。これにより、ターミナル ウィンドウが直接開きます。
方法 2: アプリケーション メニューから
- 左上隅にある アクティビティ ボタンをクリックします (または Super キーを押します)。
- 検索バーに「ターミナル」と入力します
- ターミナルアイコンをクリックして開きます
方法 3: ファイルマネージャーから
GNOME ファイル マネージャー (Nautilus) では、空の領域を右クリックし、「ターミナルで開く」 を選択できます。ターミナルは自動的に現在のディレクトリに移動します。
方法 4: TTY 仮想端末
Ctrl + Alt + F3 (F3 ~ F6) を押して、テキスト専用の仮想端末に切り替えます。 Ctrl + Alt + F2 を押して、グラフィカル インターフェイスに戻ります。これは、グラフィカル インターフェイスに問題がある場合に特に役立ちます。
基本コマンド
pwd - 印刷作業ディレクトリ
pwd (印刷作業ディレクトリ) には、現在のディレクトリのパスが表示されます。
pwd
# Example output: /home/usernamels - ディレクトリの内容をリストする
ls (リスト) は、ディレクトリ内のファイルとフォルダーを表示するために最も一般的に使用されるコマンドの 1 つです。
# List current directory contents
ls
# Show detailed information (permissions, size, modification time, etc.)
ls -l
# Show hidden files (files starting with .)
ls -a
# Combined usage: detailed info + hidden files + human-readable file sizes
ls -lah
# List contents of a specific directory
ls /etc
# Sort by modification time (newest first)
ls -ltcd - ディレクトリを変更する
cd (ディレクトリの変更) は、ディレクトリ間の切り替えに使用されます。
# Enter a specific directory
cd /var/log
# Go up one level
cd ..
# Return to the home directory
cd ~
# Or simply type
cd
# Return to the previous directory
cd -
# Enter a multi-level directory
cd /usr/share/docmkdir - ディレクトリの作成
# Create a single directory
mkdir myproject
# Create nested directories (-p auto-creates parent directories)
mkdir -p projects/web/frontend
# Create multiple directories at once
mkdir dir1 dir2 dir3cp - ファイルとディレクトリのコピー
# Copy a file
cp file.txt backup.txt
# Copy a file to a specific directory
cp file.txt /home/username/Documents/
# Copy an entire directory (requires -r for recursive copy)
cp -r myproject myproject-backup
# Copy while preserving file attributes (permissions, timestamps, etc.)
cp -a source/ destination/mv - 移動または名前変更
# Rename a file
mv oldname.txt newname.txt
# Move a file to another directory
mv file.txt /home/username/Documents/
# Move and rename
mv file.txt /home/username/Documents/newname.txt
# Move an entire directory
mv myproject /opt/rm - ファイルとディレクトリの削除
# Remove a file
rm file.txt
# Remove a directory and its contents (recursive deletion)
rm -r myproject
# Force removal (no confirmation prompt)
rm -f file.txt
# Confirm before each deletion
rm -i *.txtrm で削除されたファイルはゴミ箱には移動せず、ただちに完全に削除されます。特に、rm -rf / のようなコマンドはシステム全体を破壊します。 rm を使用する前に、正しいファイルを削除していることを必ず確認してください。初心者は、まず ls を使用してどのファイルが一致するかを確認し、確認してから削除してください。
cat - ファイルの内容を表示
# View the complete contents of a file
cat file.txt
# Display line numbers
cat -n file.txt
# Concatenate multiple files
cat file1.txt file2.txt
# Create a new file (Ctrl+D to finish input)
cat > newfile.txt以下 - ページ単位の表示
ファイルの内容が非常に長い場合は、less よりも cat の方が適しています。
less /var/log/syslogless での一般的な操作:
- スペース: 次のページ
- b: 前のページ
- g: ファイルの先頭にジャンプします
- G: ファイルの末尾にジャンプします
- /キーワード: 検索
- n: 次の検索結果
- q: やめます
head and tail - ファイルの先頭と末尾を表示
# View the first 10 lines (default)
head file.txt
# View the first 20 lines
head -n 20 file.txt
# View the last 10 lines
tail file.txt
# View the last 50 lines
tail -n 50 file.txt
# Follow file changes in real time (commonly used for viewing logs)
tail -f /var/log/syslogファイルパス
ファイル パスを理解することは、コマンド ラインを使用するための基本です。 Linux ファイル システムは / (ルート ディレクトリ) から始まり、すべてのファイルとディレクトリはこのツリー構造で編成されます。
絶対パスと相対パス
絶対パスは、ルート ディレクトリ / から始まり、ファイルの完全な場所を示します。
# Absolute path examples
/home/username/Documents/file.txt
/etc/nginx/nginx.conf
/var/log/syslog相対パスは現在のディレクトリから計算されます。
# Assuming you are in /home/username
# The following two are equivalent:
cat /home/username/Documents/file.txt # Absolute path
cat Documents/file.txt # Relative path特殊なパス記号
| シンボル | 意味 | 例 |
|---|---|---|
/ | ルートディレクトリ | cd / |
~ | 現在のユーザーのホームディレクトリ | cd ~ は cd /home/username と同じです |
. | 現在のディレクトリ | ./script.sh は現在のディレクトリでスクリプトを実行します |
.. | 親ディレクトリ | cd ../.. は 2 レベル上がります |
- | 前のディレクトリ (cd のみ) | cd - |
# Practical examples
# Return to the home directory from anywhere
cd ~
# View the contents of the parent directory
ls ..
# Run a script in the current directory
./my-script.sh
# Copy a file from two levels up to the current directory
cp ../../config.txt .パイプとリダイレクト
パイプとリダイレクトは、コマンド ラインの非常に強力な機能です。これらを使用すると、複数の単純なコマンドを組み合わせて、複雑なタスクを実行できます。
パイプ (|)
パイプは、あるコマンドの出力を別のコマンドの入力として渡します。
# View all processes and search for lines containing "nginx"
ps aux | grep nginx
# List directory contents and display them page by page
ls -la /etc | less
# Count the number of files in the current directory
ls | wc -l
# View disk usage, sorted by size
du -sh * | sort -h
# Multi-level pipe: find errors in logs, deduplicate, and sort
cat /var/log/syslog | grep "error" | sort | uniq -c | sort -rn出力リダイレクト (> および >>)
# Write command output to a file (overwrites existing content)
echo "Hello, Ubuntu!" > hello.txt
# Append command output to the end of a file
echo "Another line" >> hello.txt
# Save a command listing to a file
ls -la > filelist.txt
# Redirect error output to a file
command 2> errors.log
# Redirect both standard output and error output
command > output.log 2>&1
# Or the shorter version
command &> output.log入力リダイレクト <
# Read input from a file
wc -l < file.txt
# Common usage: import an SQL file into a database
mysql -u root -p database_name < backup.sql権限の基本
Linux はマルチユーザー システムであり、権限管理によりシステムのセキュリティが保証されます。
sudo - 管理者として実行
sudo (スーパー ユーザー Do) により、一般ユーザーは管理者 (root) 権限でコマンドを実行できます。
# Install software
sudo apt install vim
# Edit a system configuration file
sudo nano /etc/hosts
# Open a shell as root (use with caution)
sudo -i
# Check sudo privileges
sudo -l初めて sudo を使用するときは、(root パスワードではなく) 自分のユーザー パスワードを要求されます。パスワードは一定期間記憶されます。
chmod - ファイル権限の変更
Linux のファイル権限は、所有者 (u)、グループ (g)、その他 (o) の 3 つのグループに分かれています。各グループには、読み取り (r=4)、書き込み (w=2)、および 実行 (x=1) 権限があります。
# View file permissions
ls -l file.txt
# Example output: -rw-r--r-- 1 user group 1024 Jan 1 12:00 file.txt
# Add execute permission to a file
chmod +x script.sh
# Set exact permissions (owner: rwx, group: rx, others: rx)
chmod 755 script.sh
# Common permission combinations
chmod 644 file.txt # Owner read/write, others read-only
chmod 600 secret.key # Owner read/write only
chmod 777 shared.txt # Full permissions for everyone (not recommended)
# Recursively modify directory permissions
chmod -R 755 /var/www/html一般的なキーボード ショートカット
キーボード ショートカットをマスターすると、コマンド ラインの効率が大幅に向上します。
| ショートカット | 機能 |
|---|---|
| タブ | オートコンプリートコマンド、ファイル名、ディレクトリ名 |
| タブタブ | 可能なすべての補完オプションを表示する |
| Ctrl + C | 現在実行中のコマンドを中断します |
| Ctrl + D | 現在のシェルを終了します (exit と入力するのと同等) |
| Ctrl + R | コマンド履歴の逆検索 |
| Ctrl + L | 画面をクリアします (clear コマンドと同等) |
| Ctrl + A | カーソルを行頭に移動します |
| Ctrl + E | カーソルを行末に移動します |
| Ctrl + W | カーソルの前の単語を削除します |
| Ctrl + U | カーソルから行頭までを削除 |
| Ctrl + K | カーソルから行末までを削除 |
| 上/下矢印 | コマンド履歴を参照する |
| !! | 最後のコマンドを再実行します |
| !$ | 前のコマンドの最後の引数を参照します |
# Tab completion example
cd /etc/ng[Tab] # Auto-completes to cd /etc/nginx/
# Typical usage of !!: forgot to add sudo
apt install vim
# Permission error, then:
sudo !!
# Equivalent to sudo apt install vim
# Ctrl+R search history
# Press Ctrl+R, type "docker", and the most recent command containing "docker" will appear助けを求める
Linux では、ヘルプを得る方法がたくさんあります。
man - マニュアルページ
man (マニュアル) は、最も信頼できるコマンド ドキュメントです。
# View the manual for the ls command
man ls
# Search manuals (by keyword)
man -k "copy file"
# View a specific section of the manual
man 5 crontab # View the crontab file format (section 5)—help フラグ
ほとんどのコマンドは、簡潔な使用法の概要を表示する --help フラグをサポートしています。
ls --help
cp --help
systemctl --helptldr - 簡易マニュアル
tldr (長すぎます; 読みませんでした) は、初心者に最適な簡潔で実用的なコマンドの例を提供します。
# Install tldr
sudo apt install tldr
# Update the database
tldr --update
# View command usage
tldr tar
tldr rsync
tldr systemctlその他のヘルプリソース
# View a brief description of a command
whatis ls
# Find the path of a command
which python3
whereis nginx
# Check the command type (built-in, external, alias, etc.)
type cd
type ls初心者向けの必須コマンドチートシート
ここでは、初心者向けに最もよく使用される 20 のコマンドを紹介します。参考にブックマークしてください!一度にすべてを暗記する必要はありません。必要なときに調べるだけで、時間が経つにつれて自然に覚えられます。
| コマンド | 目的 | 一般的な例 |
|---|---|---|
pwd | 現在のディレクトリを表示 | pwd |
ls | ディレクトリの内容をリストする | ls -lah |
cd | ディレクトリを変更する | cd /var/log |
mkdir | ディレクトリを作成する | mkdir -p a/b/c |
cp | ファイル/ディレクトリをコピーする | cp -r src/ dst/ |
mv | 移動/名前変更 | mv old.txt new.txt |
rm | ファイル/ディレクトリを削除する | rm -r dirname |
cat | ファイルの内容を表示 | cat file.txt |
less | ファイルをページごとに表示する | less /var/log/syslog |
head/tail | ファイルの先頭/末尾を表示 | tail -f log.txt |
grep | テキストを検索 | grep "error" log.txt |
find | ファイルの検索 | find / -name "*.conf" |
sudo | 管理者権限で実行 | sudo apt update |
apt | パッケージマネージャー | sudo apt install vim |
chmod | 権限を変更する | chmod +x script.sh |
nano/vim | テキストエディタ | nano file.txt |
df | ディスク使用量を確認する | df -h |
top/htop | プロセスを表示 | htop |
systemctl | システムサービスを管理する | systemctl status nginx |
history | コマンド履歴の表示 | history | grep ssh |
高度なヒント
コマンドエイリアス
頻繁に使用するコマンドの短いエイリアスを作成できます。
# Temporary alias (only valid for the current session)
alias ll='ls -lah'
alias gs='git status'
# Permanent alias (written to ~/.bashrc)
echo "alias ll='ls -lah'" >> ~/.bashrc
echo "alias update='sudo apt update && sudo apt upgrade -y'" >> ~/.bashrc
# Apply aliases immediately
source ~/.bashrcコマンド履歴
# View command history
history
# Search command history
history | grep "apt install"
# Execute command #42 from history
!42
# Clear history
history -cワイルドカード
# * matches any characters
ls *.txt # All .txt files
cp *.jpg photos/ # Copy all .jpg files
# ? matches a single character
ls file?.txt # Matches file1.txt, fileA.txt, etc.
# [] matches a specified range
ls file[1-3].txt # Matches file1.txt, file2.txt, file3.txtこれらの基本を学べば、日常的な操作のほとんどを Ubuntu コマンド ラインで処理できるようになります。シェル スクリプト、システム管理、ネットワーク構成など、より高度なトピックを引き続き学習できます。練習すれば完璧になるということを忘れないでください。コマンド ラインをマスターするには実践的な経験が最善の方法です。