Loading
Loading
Mastering these commands makes you comfortable on any Linux server or Docker container.
pwd # Where am I now?
ls # Current directory contents
ls -la # All files with details including hidden
ls -lh # With human-readable sizes (KB, MB)
cd ~ # Go to home directory
cd /etc # Absolute path
cd .. # One level up
cd - # Return to previous directory
mkdir my-project # Create a directory
mkdir -p a/b/c # Create nested directories
touch main.py # Create empty file
cp file.py backup.py # Copy
mv old.py new.py # Move or rename
rm file.py # Delete file
rm -rf folder/ # Delete entire folder (be careful!)
cat requirements.txt # Show file contents
head -20 log.txt # First 20 lines
tail -50 log.txt # Last 50 lines
tail -f app.log # Follow log in real time
# grep — search in files
grep "error" app.log # search for "error"
grep -r "import" src/ # search all files
grep -n "def train" model.py # with line numbers
grep -i "cuda" requirements.txt # case-insensitive
# find — find files
find . -name "*.py" # all Python files
find . -name "*.log" -mtime -1 # log files from last day
find /tmp -size +100M # files larger than 100MB
ps aux # show all processes
ps aux | grep python # Python processes only
top # live resource monitor
htop # better than top (needs install)
kill 1234 # stop process by ID
kill -9 1234 # force stop
pkill python # stop all Python processes
# GPU monitoring
nvidia-smi # GPU status
watch -n 1 nvidia-smi # update every second
# Package management
sudo apt update && sudo apt upgrade
sudo apt install python3-pip
pip install anthropic fastapi
# Environment variables
export ANTHROPIC_API_KEY="sk-ant-..."
printenv # show all variables
# Chain commands together
ps aux | grep python | wc -l # count Python processes
cat app.log | grep "error" | tail -10 # last 10 errors
ls -la | sort -k5 -rn # sort by size
# أوامر الطرفية الأساسية — Terminal Commands Demo
# محاكاة أوامر Linux باستخدام Python
import os
import subprocess
import sys
import time
from pathlib import Path
from dataclasses import dataclass
@dataclass
class Command:
cmd: str
description: str
def simulate_cmd(cmd: str, desc: str, output: str = ""):
"""محاكاة تنفيذ أمر terminal"""
print(f"\n$ {cmd}")
if desc:
print(f" # {desc}")
if output:
for line in output.strip().split("\n")[:5]:
print(f" {line}")
# ─── 1. التنقل في نظام الملفات ───────────────────────────
print("🐧 أوامر الطرفية الأساسية")
print("="*55)
print("\n📂 1. التنقل في نظام الملفات")
print("─"*55)
home = Path.home()
simulate_cmd("pwd", "المجلد الحالي", str(home))
simulate_cmd("ls -la ~", "عرض الملفات مع التفاصيل",
"drwxr-xr-x ahmed users .\n-rw-r--r-- ahmed users .bashrc\n-rw------- ahmed users .env")
simulate_cmd("mkdir -p ai-project/src ai-project/data",
"إنشاء هيكل مشروع")
# تنفيذ فعلي
tmp_dir = Path("/tmp/linux-demo")
for d in ["src", "data", "models", "logs"]:
(tmp_dir / d).mkdir(parents=True, exist_ok=True)
print(f" ✅ تم إنشاء هيكل في {tmp_dir}")
# ─── 2. العمليات على الملفات ─────────────────────────────
print("\n📄 2. العمليات على الملفات")
print("─"*55)
# إنشاء ملفات تجريبية
(tmp_dir / "requirements.txt").write_text(
"anthropic==0.34.0\nfastapi==0.112.0\nuvicorn\npydantic\n"
)
(tmp_dir / "app.log").write_text(
"INFO: Server started\nERROR: Connection failed\nINFO: Retry...\nERROR: Timeout\nINFO: Connected\n"
)
simulate_cmd("cat requirements.txt", "عرض محتوى الملف")
print(" anthropic==0.34.0")
print(" fastapi==0.112.0")
print(" uvicorn")
simulate_cmd("tail -3 app.log", "آخر 3 أسطر من الـ log")
lines = (tmp_dir / "app.log").read_text().strip().split("\n")
for line in lines[-3:]:
print(f" {line}")
# ─── 3. البحث والتصفية ────────────────────────────────────
print("\n🔍 3. البحث والتصفية (grep/find)")
print("─"*55)
log_content = (tmp_dir / "app.log").read_text()
error_lines = [l for l in log_content.split("\n") if "ERROR" in l]
simulate_cmd("grep 'ERROR' app.log", "البحث عن الأخطاء")
for line in error_lines:
print(f" {line}")
# find محاكاة
py_files = list(tmp_dir.rglob("*.txt"))
simulate_cmd("find . -name '*.txt'", f"البحث عن ملفات txt ({len(py_files)} ملف)")
for f in py_files:
print(f" ./{f.name}")
# ─── 4. إدارة العمليات ────────────────────────────────────
print("\n⚙️ 4. إدارة العمليات")
print("─"*55)
simulate_cmd("ps aux | grep python", "عمليات Python الحالية",
f"ahmed {os.getpid()} 0.5 python3 current_script.py")
simulate_cmd("top -bn1 | head -5", "مراقبة الموارد",
"CPU: 15.2% | MEM: 8.1GB/16GB | Load: 0.45")
# ─── 5. Pipes والتحويل ────────────────────────────────────
print("\n🔗 5. Pipes — توصيل الأوامر ببعضها")
print("─"*55)
# محاكاة: cat app.log | grep ERROR | wc -l
error_count = len(error_lines)
simulate_cmd(
"cat app.log | grep 'ERROR' | wc -l",
f"عدد الأخطاء في الـ log: {error_count}",
)
print(f" {error_count}")
simulate_cmd(
"cat requirements.txt | sort | head -3",
"فرز المتطلبات وعرض أول 3",
)
for pkg in sorted(["anthropic", "fastapi", "uvicorn", "pydantic"])[:3]:
print(f" {pkg}")
# ─── تنظيف ────────────────────────────────────────────────
import shutil
shutil.rmtree(tmp_dir, ignore_errors=True)
print(f"\n✅ تم تنظيف الملفات المؤقتة")
print("\n💡 نصيحة: احفظ هذه الأوامر في ملف cheatsheet.txt!")