KAHIBARO
Discord Login Register

3.3. Command Line Basics

Why the Command Line Matters for Backend Developers

As a backend developer you will spend a lot of time in a terminal. You will:

The command line gives you speed, scripting power, and remote access. You do not need to become a Linux guru now, but you must be comfortable with core commands and how to move around.

This chapter focuses on the basics that you will use almost every day.

Rule: For backend work, always know where you are in the filesystem, what files exist there, and what command you are running. A wrong command in the wrong directory can damage your project or system.

Throughout the examples, $ represents the shell prompt. You do not type the $ itself, only what comes after it.

Example:

bash
$ pwd        # You type: pwd  (then press Enter)

Opening a Terminal

How you open a terminal depends on your operating system.

In code examples, Linux, macOS, and WSL behave almost the same. PowerShell syntax sometimes differs, but you can usually adapt easily. If possible, use WSL so you get a Linux environment, which matches most backend servers.

The Prompt and Running Commands

A shell displays a prompt to show it is ready for a command. It often includes your user name, machine name, and current directory:

bash
user@machine:~/projects/myapp$

You then type a command, possibly with options and arguments:

bash
$ command-name [options] [arguments]

For example:

bash
$ ls -l /etc

Rule: Commands are case sensitive on Linux and macOS. LS is not the same as ls.

You can run built-in commands or programs installed on your system. Press Enter to execute.

Getting Help

You never need to memorize everything. Use built‑in help.

`--help` option

Many commands support:

bash
$ command --help

Examples:

bash
$ ls --help
$ mkdir --help

This usually prints a short usage summary.

`man` pages

On Linux and macOS, you often have manual pages:

bash
$ man ls

Use:

If man pages are missing in containers or minimal systems, rely on --help or online documentation.

`help` for shell built‑ins

Some commands are built into the shell, for example cd in bash:

bash
$ help cd

Understanding the Filesystem: Paths

A filesystem is organized like a tree. You navigate it with paths.

Absolute vs relative paths

Common special entries:

PathMeaning
.Current directory
..Parent directory
~Your home directory (Linux/macOS, WSL)

Examples:

bash
$ cd ~/projects          # Go into "projects" in your home directory
$ cd ..                  # Go to parent directory
$ ls ./src               # List contents of "src" inside current directory

Where Am I? `pwd`

To know your current directory, use pwd (print working directory):

bash
$ pwd
/home/user/projects/myapp

You will use pwd a lot while learning.

Example workflow:

bash
$ pwd
/home/user
$ cd projects
$ pwd
/home/user/projects

Listing Files: `ls`

To see what files and folders are in the current directory:

bash
$ ls

You can also specify a path:

bash
$ ls /etc

Useful options:

CommandDescription
lsList names only
ls -lLong format with permissions and dates
ls -aInclude hidden files (names start with .)
ls -la or ls -alLong format plus hidden files

Example:

bash
$ ls -la
drwxr-xr-x  8 user user  4096 Aug 25 10:00 .
drwxr-xr-x 10 user user  4096 Aug 25 09:00 ..
-rw-r--r--  1 user user   220 Aug 25 10:00 .bash_logout
-rw-r--r--  1 user user  3771 Aug 25 10:00 .bashrc
drwxr-xr-x  3 user user  4096 Aug 25 10:10 myapp

Moving Around: `cd`

cd changes your current directory.

Basic usage:

bash
$ cd /absolute/path      # Use an absolute path
$ cd relative/path       # Use a relative path
$ cd                     # Go to your home directory
$ cd ~                   # Also go to home
$ cd ..                  # Go up one directory

Example navigation:

bash
$ pwd
/home/user
$ cd projects
$ pwd
/home/user/projects
$ cd myapp/src
$ pwd
/home/user/projects/myapp/src
$ cd ..
$ pwd
/home/user/projects/myapp

Creating Directories and Files: `mkdir` and `touch`

`mkdir` to create directories

bash
$ mkdir myproject
$ ls
myproject

To create nested directories in one go, use -p:

bash
$ mkdir -p myproject/app/templates

Without -p, mkdir fails if parent directories do not exist.

`touch` to create empty files

bash
$ cd myproject
$ touch main.py
$ ls
main.py

If the file already exists, touch updates its last modified time but does not change its content.

Example project setup:

bash
$ mkdir -p myapi/app
$ cd myapi/app
$ touch __init__.py main.py models.py
$ ls
__init__.py  main.py  models.py

Viewing File Contents: `cat`, `less`, `head`, `tail`

Backend developers often inspect configuration files and logs from the terminal.

`cat` to print entire file

bash
$ cat main.py
print("Hello, backend!")

For small files, cat is fine. For large files, it can flood your terminal.

`less` to scroll

less lets you scroll through long files:

bash
$ less access.log

Controls:

`head` and `tail`

To see only the first or last lines:

bash
$ head my.log        # First 10 lines
$ tail my.log        # Last 10 lines
$ head -n 20 my.log  # First 20 lines
$ tail -n 50 my.log  # Last 50 lines

tail -f is very useful for logs. It follows the file as it is written:

bash
$ tail -f myapp.log

This shows new log entries as your backend runs.

Creating and Editing Files

You will need a text editor that runs in a terminal or in your GUI.

Simple file creation with redirection

You can quickly create small files using cat and redirection:

bash
$ cat > hello.py
print("Hello from the command line")
# Press Ctrl + D to finish input

Now:

bash
$ python hello.py
Hello from the command line

Terminal text editors

Common terminal editors:

Example with nano:

bash
$ nano main.py

Type your code, then:

In many cases you will use a GUI editor or IDE (covered in another chapter), but knowing nano or vim is very helpful on remote servers.

Copying, Moving, and Renaming Files: `cp` and `mv`

Copying: `cp`

cp copies files or directories.

bash
$ cp source.txt dest.txt

Copy into a directory:

bash
$ cp config.example.ini config.ini
$ cp main.py backup_main.py

To copy directories, use -r (recursive):

bash
$ cp -r src src_backup

Be careful, cp can overwrite files without warning. Use -i (interactive) to be asked before overwriting:

bash
$ cp -i config.ini /etc/myapp/config.ini

Moving and renaming: `mv`

mv moves or renames files and directories.

Rename:

bash
$ mv main_old.py main.py

Move a file into a directory:

bash
$ mv main.py src/

Move and rename in one command:

bash
$ mv src/main.py app/server.py

You can also use -i for safety:

bash
$ mv -i config.ini /etc/myapp/config.ini

Deleting Files and Directories: `rm` and `rmdir`

Deleting is powerful and often final.

Rule: Check the path twice before running rm. A small mistake can delete important files. There is no “undo” in the shell.

Deleting files: `rm`

bash
$ rm file.txt

To be asked before deleting each file, use:

bash
$ rm -i file.txt

Deleting directories

You can remove an empty directory with:

bash
$ rmdir emptydir

To remove a directory and everything in it, use:

bash
$ rm -r mydir

This is dangerous. Add -i to confirm each delete:

bash
$ rm -ri mydir

Never run commands like:

bash
rm -rf /
rm -rf *

unless you are absolutely sure what you are doing. They can erase huge parts of your filesystem.

For backend projects, a safe pattern is to only delete inside your project directory, not system paths.

Searching Files and Text: `find` and `grep`

You will often search for a file or a piece of configuration.

`find` to locate files and directories

Basic example, find all .py files under current directory:

bash
$ find . -name "*.py"
./main.py
./app/models.py
./tests/test_main.py

Explanation:

Find directories named migrations:

bash
$ find . -type d -name "migrations"

`grep` to search inside files

Search for a word or phrase in files:

bash
$ grep "DATABASE_URL" config.py

Search recursively in all .py files:

bash
$ grep -R "DATABASE_URL" .
$ grep -R "DATABASE_URL" . --include="*.py"

Useful options:

OptionMeaning
-iCase insensitive search
-nShow line numbers
-RSearch directories recursively

Example:

bash
$ grep -Rin "DEBUG" .
./settings.py:10:DEBUG = True

Running Programs: `python`, `pip`, `git`, etc.

The command line is how you run tools used in backend development.

Examples:

bash
$ python main.py
$ python -m pytest           # Run tests with pytest
$ pip install fastapi
$ git status
$ git commit -m "Add new endpoint"
$ uvicorn main:app --reload  # Run a FastAPI app during development

Each of these tools comes with its own options and subcommands. You will learn them gradually.

Use --help for each:

bash
$ git --help
$ pip --help
$ uvicorn --help

Pipes and Redirection: Combining Commands

The shell lets you connect commands so that the output of one becomes the input of another. This is very powerful.

Standard output and redirection

To send output to a file instead of the screen:

Examples:

bash
$ ls > files.txt             # Save ls output to files.txt, overwrite
$ ls >> files.txt            # Append ls output to files.txt
$ cat config.py > backup_config.py

If you open files.txt, you will see the list of files.

Pipes `|`

The pipe symbol | passes the output of one command into another.

Example, see only Python files from ls:

bash
$ ls | grep ".py"
main.py
models.py

Tail the log file and show only lines containing "ERROR":

bash
$ tail -f myapp.log | grep "ERROR"

Pipes are essential for quickly inspecting logs and filtering information during debugging.

Environment Variables Basics

Environment variables are key-value pairs that affect how processes behave. They are often used for configuration and secrets, such as database URLs and API keys.

To print an environment variable:

bash
$ echo $HOME
/home/user
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:...

To set an environment variable for a single command:

bash
$ DEBUG=true python main.py

To set one in the current shell session (bash style):

bash
$ export DEBUG=true
$ echo $DEBUG
true

Later chapters have a dedicated discussion of environment variables, but you already see them often in backend examples, for example:

bash
$ export DATABASE_URL="postgresql://user:pass@localhost:5432/mydb"
$ uvicorn main:app --reload

Managing Processes: `ps`, `kill`, and `Ctrl + C`

When you run a long command, such as a web server, it may keep running until you stop it.

Stopping a foreground process

Most of the time, you can stop the running command with:

Example:

bash
$ uvicorn main:app --reload
INFO:     Uvicorn running on http://127.0.0.1:8000
# Press Ctrl + C to stop

Listing processes: `ps`

To see processes related to your shell:

bash
$ ps
  PID TTY          TIME CMD
1234 pts/0    00:00:00 bash
1300 pts/0    00:00:00 python
1310 pts/0    00:00:00 ps

PID is the process ID.

To see more details:

bash
$ ps aux | grep python

Killing a process: `kill`

If a process is stuck and does not respond to Ctrl + C, you can use kill with its PID.

bash
$ kill 1300        # Politely ask process 1300 to stop
$ kill -9 1300     # Forcefully kill process 1300 (use only if needed)

Command History and Reuse

The shell keeps a history of commands you entered. This saves time.

Example of Ctrl + R:

  1. Press Ctrl + R
  2. Type uvicorn
  3. You see: (reverse-i-search)uvicorn': uvicorn main:app --reload`
  4. Press Enter to run it again or use arrows to edit.

You can also list the history:

bash
$ history
  1  ls
  2  cd projects
  3  uvicorn main:app --reload

Then run a command by its number:

bash
$ !3

This runs the third command from history.

File Permissions Basics

On Linux/macOS, each file has permissions that control who can read, write, or execute it.

View permissions with ls -l:

bash
$ ls -l main.py
-rw-r--r-- 1 user user 120 Aug 25 10:30 main.py

The first part -rw-r--r-- shows:

They are grouped for:

  1. Owner
  2. Group
  3. Others

Basic patterns you will see:

PatternMeaning
-rw-r--r--Regular file, owner can edit
-rwxr-xr-xExecutable file or script
drwxr-xr-xDirectory

To make a script executable:

bash
$ chmod +x script.sh
$ ls -l script.sh
-rwxr-xr-x 1 user user ... script.sh

You will use permissions more deeply when deploying to servers, but recognizing them now is helpful.

Practical Example: Creating a Simple Project via CLI

Let us combine several commands into a short, realistic workflow.

You want to start a new backend project called todo_api.

bash
# 1. Go to home directory
$ cd ~
# 2. Create a projects directory if it does not exist
$ mkdir -p projects
$ cd projects
# 3. Create a project folder
$ mkdir todo_api
$ cd todo_api
# 4. Create basic structure
$ mkdir app
$ touch app/__init__.py app/main.py
# 5. List structure
$ ls
app
$ ls app
__init__.py  main.py
# 6. Edit main.py with nano (or your editor)
$ nano app/main.py

Inside app/main.py, you might write:

python
def hello():
    return "Hello, TODO API"
if __name__ == "__main__":
    print(hello())

Then run it:

bash
$ python app/main.py
Hello, TODO API

You have just created a simple project using only command line tools.

Good Habits for Using the Command Line

Some simple habits will save you pain.

Rule: Before running a destructive command like rm, mv, or cp, always:

  1. Run pwd to verify where you are.
  2. Run ls to see what is there.
  3. Double check the full path in your command.

Additional tips:

Summary

In this chapter you learned:

These command line basics are enough to start doing real backend work, such as creating projects, running Python scripts, working with Git, and later managing services and servers. In the following chapters you will build on this foundation.

Views: 9

Comments

Please login to add a comment.

Don't have an account? Register now!