
Introduction: How to Use Shell Commands in Google Colab 2026
Knowing how to use shell commands in Google Colab unlocks the full power of Linux inside your notebook. From installing packages and managing files to checking GPUs, cloning Git repos, or running scripts, Colab supports nearly everything a real terminal can do.
This guide walks you through shell basics, automation tricks, real examples, best practices, and common errors.
Run Shell Commands in Google Colab Using the ! Operator
Google Colab executes Linux commands using the ! prefix.
Examples:
!ls
!pwd
!mkdir test_folder
!rm -r test_folder
These commands run directly in Colab’s temporary Linux environment and behave like standard terminal operations.
Run Multiple Commands Using %%bash
For scripts or sequences of commands, use the %%bash magic:
%%bash
echo "Compiling program"
mkdir build
cd build
echo "Done!"
This is ideal for long workflows, loops, functions, and shell automation.
Install System Packages With apt-get
You can install Linux packages exactly as you would on Ubuntu:
!sudo apt-get update
!sudo apt-get install ffmpeg -y
Use this to install development tools, compilers, libraries, and system utilities.
Manage Files and Folders Using Shell Commands
Here are the most useful day-to-day commands:
| Task | Command |
|---|---|
| List files | !ls -lah |
| Create folder | !mkdir folder |
| Copy files | !cp file1 file2 |
| Move files | !mv old new |
| Delete files | !rm file, !rm -r folder |
| View file content | !cat filename |
Example:
!ls /content
How to Use Shell Commands in Google Colab 2026
Install Python Packages Using Shell Commands
Colab supports both !pip and %pip:
!pip install numpy
%pip install pandas
Important: %pip installs packages into the active runtime.!pip can install into the wrong environment, so %pip is recommended.
Internal link: You may also like How to Debug Python Code in Google Colab on darekdari.com.
Clone GitHub Repositories in Google Colab
Clone any repo using:
!git clone https://github.com/username/repo.git
Update the repo later with:
!git pull
This is widely used in machine learning and dataset preparation.
Check System Information With Shell Commands
Colab allows you to check hardware specs easily:
!nvidia-smi
!lscpu
!free -h
!df -h
Example to check GPU model and VRAM:
!nvidia-smi --query-gpu=name,memory.total --format=csv
Create and Run Bash Scripts Inside Colab
To build and run .sh scripts:
%%bash
echo 'echo "Hello from script"' > script.sh
chmod +x script.sh
Execute it:
!./script.sh
Great for automating training loops or preprocessing pipelines.
Mix Python and Shell Commands in One Notebook
Send Python variables to shell:
filename = "data.txt"
!echo "Processing $filename"
Send shell output back to Python:
!echo 42 > number.txt
with open("number.txt") as f:
print(int(f.read()))
This makes hybrid automation extremely powerful.
Troubleshooting and Common Mistakes
“bash: command not found”
The package isn’t installed. Install with:
!sudo apt-get install packagename
“Permission denied”
Add permissions:
!chmod +x script.sh
“No such file or directory.”
Paths changed. Re-check with:
!pwd
!ls
Package installed but Python cannot import it
Use %pip install, not !pip install.
Files disappearing after refresh
Save everything to Google Drive instead of /content.
Internal link: Learn How to Mount Google Drive in Colab on darekdari.com.
Best Practices for Using Shell Commands in Colab
- Prefer
%pipand%aptfor reliable installations - Save important files to Drive
- Use
%%bashfor long shell scripts - Check GPU availability before training
- Keep directories simple
- Comment your commands for clarity
- Avoid commands that alter core system files
Alternatives to Shell Commands
- Python’s
subprocessmodule - Jupyter magic commands
- Google Drive sidebar file manager
- GitHub Codespaces
- Google Cloud Shell for advanced workflows
Frequently Asked Questions
1. How do I run shell commands in Google Colab?
Add ! before any command, or use %%bash for multiple commands.
2. Can I run bash scripts in Colab?
Yes — create a .sh file and run it normally.
3. How do I install packages like ffmpeg in Colab?
Use:
!sudo apt-get install ffmpeg
4. Why do files disappear after I restart Colab?
The environment resets. Save to Drive to avoid losing work.
5. Can I mix Python code and shell commands together?
Yes — Colab supports variable injection both ways.
6. How do I check if a GPU is available?
Run:
!nvidia-smi
7. What’s the difference between !pip and %pip?%pip installs correctly into the active runtime.
8. Can I use Git inside Google Colab?
Absolutely — clone repos with !git clone.
9. How do I navigate directories with shell commands?
Use !ls to inspect and avoid path mistakes.
10. Can I run advanced Linux commands in Colab?
Yes, but root-level commands may be restricted.
11. How do I delete files in Colab?
Use !rm for files and !rm -r for folders.
12. How do I fix pip packages not being detected?
Restart the runtime after installing with %pip install.
Conclusion
Understanding how to use shell commands in Google Colab gives you full control over file management, system tools, installations, and automation. Whether you’re training models, processing data, or building scripts, shell commands make your workflow faster and more professional.
For more practical Colab tutorials, visit darekdari.com.

0 Comments