Skip to content

Basic Linux Commands for Development

As a developer, understanding essential Linux commands is crucial. Whether you're managing files, setting up Python environments, or working with directories, these commands will help you get started.


File and Directory Management

1. List Files and Directories

ls
Options: - ls -l: Long format listing - ls -a: Show hidden files

2. Create a Directory

mkdir directory_name

3. Remove a Directory

rmdir directory_name
For non-empty directories:
rm -r directory_name

4. Navigate Between Directories

cd directory_name
To go back:
cd ..

5. View Current Directory

pwd

6. Copy Files or Directories

cp source destination
For directories:
cp -r source destination

7. Move or Rename Files

mv source destination

8. Delete Files

rm file_name

Working with Python and Pip

1. Check Python Version

python3 --version

2. Install Python Package Manager (pip)

For Debian/Ubuntu:

sudo apt install python3-pip
For RedHat/CentOS:
sudo yum install python3-pip

3. Install a Python Package

pip install package_name

4. List Installed Python Packages

pip list

5. Uninstall a Python Package

pip uninstall package_name

Virtual Environments

Using virtualenv (or its successor, venv, built into Python 3.3+) is highly recommended for Python development. Here’s why:


1. Isolation of Dependencies

A virtual environment isolates your project's dependencies from the global Python environment. This ensures: - Dependencies installed for one project don’t interfere with others. - Your global Python environment remains clean and unaffected.


2. Reproducible Environments

With virtual environments, you can create a consistent environment across different systems: - Use a requirements.txt file to list exact package versions. - Team members can recreate the same environment by running pip install -r requirements.txt.


3. Easier Collaboration

When you share your project, others can: - Use the virtual environment to work with the same dependencies. - Avoid conflicts with their existing Python setup.


4. Avoid Version Conflicts

You may have projects requiring different versions of the same package or even Python itself: - Example: One project uses Django 2.2, while another uses Django 4.0. - Virtual environments let you manage these independently.


5. Security

Installing packages globally can inadvertently affect system-wide configurations or conflict with other software. Virtual environments prevent this by sandboxing your project.


6. Simplified Deployment

For production environments: - Virtual environments provide a clear structure of dependencies. - Tools like Docker often rely on requirements.txt from a virtual environment.


7. Lightweight and Easy to Use

virtualenv or venv is straightforward to set up and doesn't require much overhead. A few commands get you started:

# Create a virtual environment
python3 -m venv env

# Activate it
source env/bin/activate

# Install dependencies
pip install package_name

# Deactivate when done
deactivate



#### 1. Install `virtualenv`
```bash
pip install virtualenv

2. Create a Virtual Environment

virtualenv env_name

3. Activate the Virtual Environment

source env_name/bin/activate

4. Deactivate the Virtual Environment

deactivate

File Permissions and Ownership

1. Change File Permissions

chmod permissions file_name
Example: Make a file executable:
chmod +x script.sh

2. Change File Ownership

sudo chown user:group file_name

Package Management

1. Update Package Lists

sudo apt update

2. Upgrade Installed Packages

sudo apt upgrade

3. Install a Package

sudo apt install package_name

4. Remove a Package

sudo apt remove package_name

Wrapping Up

These commands are foundational for Linux-based development environments. Whether you're organizing files, managing Python projects, or working with packages, mastering these commands will make your workflow more efficient. Practice them, and you'll become more comfortable with Linux in no time!