ファイル操作コマンド
ファイル操作コマンド
Section titled “ファイル操作コマンド”Linuxでのファイル操作に使用する基本的なコマンドを詳しく解説します。
ls(リスト表示)
Section titled “ls(リスト表示)”基本的な使い方
Section titled “基本的な使い方”# 現在のディレクトリのファイル一覧ls
# 詳細情報を表示(-l: ロングフォーマット)ls -l
# すべてのファイルを表示(-a: 隠しファイルも含む)ls -a
# 人間が読みやすい形式でサイズを表示(-h: human-readable)ls -lh
# 更新日時でソート(-t: 時間順)ls -lt
# サイズでソート(-S: サイズ順)ls -lS
# 再帰的に表示(-R: 再帰的)ls -R# 最近更新されたファイルを確認ls -lt | head -10
# 大きなファイルを確認ls -lhS | head -10
# 特定の拡張子のファイルのみ表示ls *.txtls *.{txt,md}cd(ディレクトリ移動)
Section titled “cd(ディレクトリ移動)”基本的な使い方
Section titled “基本的な使い方”# ホームディレクトリに移動cd ~# またはcd
# 親ディレクトリに移動cd ..
# 前のディレクトリに戻るcd -
# 絶対パスで移動cd /var/log
# 相対パスで移動cd ../logs# よく使うディレクトリへのショートカット(エイリアス設定)alias ll='ls -lah'alias ..='cd ..'alias ...='cd ../..'
# プロジェクトディレクトリに素早く移動cd ~/projects/my-projectcp(コピー)
Section titled “cp(コピー)”基本的な使い方
Section titled “基本的な使い方”# ファイルをコピーcp source.txt dest.txt
# ディレクトリを再帰的にコピー(-r: 再帰的)cp -r source_dir dest_dir
# 詳細を表示しながらコピー(-v: verbose)cp -v source.txt dest.txt
# 上書き前に確認(-i: interactive)cp -i source.txt dest.txt
# タイムスタンプとパーミッションを保持(-p: preserve)cp -p source.txt dest.txt# バックアップを作成cp important.txt important.txt.bak
# 複数のファイルをコピーcp file1.txt file2.txt file3.txt /backup/
# ワイルドカードを使用cp *.txt /backup/mv(移動・リネーム)
Section titled “mv(移動・リネーム)”基本的な使い方
Section titled “基本的な使い方”# ファイルを移動mv source.txt /tmp/
# ファイルをリネームmv old_name.txt new_name.txt
# ディレクトリを移動mv source_dir /tmp/
# 複数のファイルを移動mv file1.txt file2.txt /tmp/# ファイル名を変更mv log.txt log_$(date +%Y%m%d).txt
# バックアップディレクトリに移動mv *.log /backup/logs/rm(削除)
Section titled “rm(削除)”基本的な使い方
Section titled “基本的な使い方”# ファイルを削除rm file.txt
# 複数のファイルを削除rm file1.txt file2.txt file3.txt
# ディレクトリを削除(-r: 再帰的)rm -r directory
# 確認なしで削除(-f: force)rm -f file.txt
# 確認しながら削除(-i: interactive)rm -i file.txt
# ディレクトリを強制削除rm -rf directory# 危険なコマンド(絶対に実行しない)rm -rf / # システム全体を削除してしまう
# 安全な削除方法rm -i *.txt # 確認しながら削除cat(ファイル内容表示)
Section titled “cat(ファイル内容表示)”基本的な使い方
Section titled “基本的な使い方”# ファイルの内容を表示cat file.txt
# 複数のファイルを連結して表示cat file1.txt file2.txt
# 行番号を表示(-n: number)cat -n file.txt
# 空白行に番号を付けない(-b: number-nonblank)cat -b file.txt# ファイルの内容を確認cat /etc/hosts
# ファイルの内容を別のファイルに書き込むcat file.txt > output.txt
# 複数のファイルを結合cat file1.txt file2.txt > combined.txtless / more(ページャー)
Section titled “less / more(ページャー)”lessの使い方
Section titled “lessの使い方”# ファイルをページ単位で表示less file.txt
# 操作方法:# - スペース: 次のページ# - b: 前のページ# - /検索語: 検索# - q: 終了moreの使い方
Section titled “moreの使い方”# ファイルをページ単位で表示(lessより機能が少ない)more file.txt
# 操作方法:# - スペース: 次のページ# - Enter: 次の行# - q: 終了head / tail(ファイルの先頭・末尾表示)
Section titled “head / tail(ファイルの先頭・末尾表示)”headの使い方
Section titled “headの使い方”# ファイルの最初の10行を表示head file.txt
# 最初の20行を表示head -n 20 file.txt
# 最初の100バイトを表示head -c 100 file.txttailの使い方
Section titled “tailの使い方”# ファイルの最後の10行を表示tail file.txt
# 最後の20行を表示tail -n 20 file.txt
# リアルタイムでログを監視(-f: follow)tail -f /var/log/app.log
# 複数のファイルを監視tail -f /var/log/app.log /var/log/error.log# ログファイルの最新エラーを確認tail -n 100 /var/log/app.log | grep ERROR
# ログファイルをリアルタイムで監視tail -f /var/log/nginx/access.logfind(ファイル検索)
Section titled “find(ファイル検索)”基本的な使い方
Section titled “基本的な使い方”# ファイル名で検索find /home -name "*.txt"
# タイプで検索(-type f: ファイル、-type d: ディレクトリ)find /home -type f -name "*.txt"
# サイズで検索find /home -size +100M # 100MB以上のファイルfind /home -size -1M # 1MB未満のファイル
# 更新日時で検索find /home -mtime -7 # 7日以内に更新されたファイルfind /home -mtime +30 # 30日以上前に更新されたファイル
# パーミッションで検索find /home -perm 644 # パーミッションが644のファイル# 大きなファイルを検索find /home -type f -size +100M
# 古いファイルを検索して削除(注意が必要)find /tmp -type f -mtime +7 -delete
# 検索結果に対してコマンドを実行find /home -name "*.log" -exec rm {} \;
# 検索結果をファイルに保存find /home -name "*.txt" > found_files.txtgrep(テキスト検索)
Section titled “grep(テキスト検索)”基本的な使い方
Section titled “基本的な使い方”# ファイル内で文字列を検索grep "search_term" file.txt
# 大文字小文字を区別しない(-i: ignore-case)grep -i "search_term" file.txt
# 行番号を表示(-n: line-number)grep -n "search_term" file.txt
# 一致する行の前後を表示(-A: after、-B: before、-C: context)grep -A 5 "search_term" file.txt # 一致行の後5行grep -B 5 "search_term" file.txt # 一致行の前5行grep -C 5 "search_term" file.txt # 一致行の前後5行
# 一致しない行を表示(-v: invert-match)grep -v "search_term" file.txt
# 正規表現を使用(-E: extended-regexp)grep -E "pattern1|pattern2" file.txt# ログファイルからエラーを検索grep -i error /var/log/app.log
# 複数のファイルから検索grep "search_term" *.txt
# 再帰的に検索(-r: recursive)grep -r "search_term" /home/user/
# エラーログを確認grep -E "ERROR|FATAL" /var/log/app.log | tail -20ファイル操作コマンドのポイント:
- ls: ファイル一覧の表示、詳細情報、ソート
- cd: ディレクトリ移動、ショートカット
- cp: ファイル・ディレクトリのコピー、バックアップ
- mv: ファイルの移動・リネーム
- rm: ファイル・ディレクトリの削除(注意が必要)
- cat: ファイル内容の表示
- less/more: ページ単位での表示
- head/tail: ファイルの先頭・末尾表示、ログ監視
- find: ファイル検索、条件指定
- grep: テキスト検索、ログ分析
適切にファイル操作コマンドを使用することで、効率的にファイル管理ができます。