Google ColaboratoryでのLinuxコマンド活用ガイド
Colabでのコマンド実行
Colabでは、コマンドの前に!を付けることで、Linuxコマンドを実行できる。
!ls
!pwd
!pip install numpy
注意:Colabのランタイムは一時的である。ランタイムがリセットされると、/content/以下のファイルやインストール済みパッケージは消去される。重要なデータはGoogle Driveに保存する必要がある。
基本的なファイル・ディレクトリ操作
pwd - カレントディレクトリの表示
現在作業しているカレントディレクトリのフルパスを表示する。
!pwd
ls - ファイルとディレクトリの一覧表示
!ls
!ls -l # 詳細情報を表示
!ls -lh # サイズを読みやすい形式で表示
!ls -a # 隠しファイルも表示
cd - ディレクトリの移動
カレントディレクトリを変更する。Colabでは%cdマジックコマンドを使用する(!cdは効果が持続しない)。
%cd /content/drive/MyDrive/
%cd dataset
%cd .. # 親ディレクトリへ移動
mkdir - ディレクトリの作成
!mkdir output
!mkdir -p data/train/images # 親ディレクトリも同時に作成
cat - ファイル内容の表示
!cat config.txt
!cat data/labels.txt
cp - ファイルのコピー
!cp source.txt destination.txt
!cp model.h5 /content/drive/MyDrive/backup/
!cp -r dataset/ backup_dataset/ # ディレクトリごとコピー
mv - ファイルの移動・名前変更
!mv old_name.txt new_name.txt
!mv results.csv /content/drive/MyDrive/
rm - ファイルの削除
削除したファイルは復元できない。
!rm temp.txt
!rm -rf temp_output/ # ディレクトリごと削除
ワイルドカード - 複数ファイルの指定
*は任意の文字列に一致する。
!ls *.csv
!ls data/*.jpg
!rm temp_*.txt
Google Driveの利用
データを永続化するため、Google Driveをマウントする。
# Google Driveのマウント
from google.colab import drive
drive.mount('/content/drive')
# Drive内のファイル確認
!ls /content/drive/MyDrive/
# Driveとのファイルのやり取り
!cp /content/drive/MyDrive/dataset.zip .
!cp results.csv /content/drive/MyDrive/
パッケージ管理
pip - Pythonパッケージの管理
!pip install numpy pandas matplotlib
!pip list # インストール済みパッケージの確認
apt-get - システムライブラリの管理
!apt-get update
!apt-get install -y libgl1-mesa-glx
dpkg - インストール済みパッケージの確認
システムにインストールされているパッケージを確認する。
!dpkg -l # 全パッケージの一覧
!dpkg -l | grep libgl1 # 特定のパッケージを検索
データファイルの確認
wc - 行数のカウント
!wc -l data/labels.txt
!ls *.csv | wc -l # ファイル数のカウント
grep - テキスト検索
!grep 'error' training.log
!grep -i 'warning' output.txt # 大文字小文字を区別しない
!grep -r 'class_A' dataset/ # ディレクトリ内を再帰的に検索
ファイルの圧縮と展開
zip / unzip
# 展開
!unzip dataset.zip
!unzip archive.zip -d target_directory/
# 圧縮
!zip -r results.zip output_files/
tar
# 圧縮
!tar -czvf results.tar.gz output_files/
# 展開
!tar -xzvf dataset.tar.gz
ファイルのダウンロードとネットワーク
wget
!wget https://example.com/dataset.zip
!wget -O custom_name.zip https://example.com/file.zip
curl
!curl -O https://example.com/file.zip
!curl -o output.html https://example.com
gdown - Google Driveからのダウンロード
!pip install gdown
!gdown https://drive.google.com/uc?id=YOUR_FILE_ID
ping - ネットワーク接続の確認
指定したホストとのネットワーク接続を確認する。
!ping -c 4 google.com
!ping -c 4 api.example.com
Git操作
# リポジトリのクローン
!git clone https://github.com/user/repository.git
%cd repository
# 基本操作
!git status
!git add .
!git commit -m "Update from Colab"
!git push origin main
実践例
データセットの準備
# Driveをマウント
from google.colab import drive
drive.mount('/content/drive')
# データセットをダウンロードして展開
!wget https://example.com/dataset.zip
!unzip dataset.zip
# ファイル数を確認(パイプでコマンドを連結)
!ls dataset/images/*.jpg | wc -l
# 作業ディレクトリを作成
!mkdir -p output/models output/logs
訓練結果の保存
# 結果をリスト化(リダイレクトでファイルに保存)
!ls -lh output/ > file_list.txt
# 結果を圧縮してDriveに保存
!tar -czvf results.tar.gz output/
!cp results.tar.gz /content/drive/MyDrive/
環境の確認
# インストール済みパッケージの確認
!pip list
!dpkg -l | grep libgl1
# ネットワーク接続の確認
!ping -c 4 google.com
Gitとの連携
# リポジトリをクローン
!git clone https://github.com/user/ml-project.git
%cd ml-project
# 必要なパッケージをインストール
!pip install -r requirements.txt
# 訓練実行
!python train.py
# 結果をコミット
!git add results/
!git commit -m "Add training results"
!git push origin main