Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Command Line
CSE_STARTER_GUIDE

Command Line

Learn essential command-line concepts and terminal commands for navigating files, running programs, managing processes, and working efficiently.

Command Line

The command line, also known as the terminal, shell, or console, is a text-based interface for interacting with your computer’s operating system. Instead of using a graphical user interface (GUI) with windows, icons, and a mouse, you type commands to tell the computer what to do. While GUIs are great for everyday tasks, the command line is essential for developers, system administrators, and anyone who wants to work more efficiently with computers.

Why Learn the Command Line?

  1. Efficiency: Many tasks can be done much faster on the command line than with a mouse
  2. Automation: You can write scripts to automate repetitive tasks
  3. Remote Access: The command line is the primary way to access and manage remote servers
  4. Development Tools: Many development tools (like Git, Docker, and build tools) are command-line based
  5. System Administration: Essential for managing operating systems and servers
  6. Power and Flexibility: Gives you more control over your system
  7. Understanding How Computers Work: Helps you understand the underlying workings of your operating system

Basic Concepts

The Shell

The shell is a program that interprets your commands and communicates them to the operating system. Common shells include:

  • Bash (Bourne-Again SHell): The default on most Linux distributions and macOS
  • Zsh (Z Shell): Popular alternative with more features (default on newer macOS versions)
  • Fish (Friendly Interactive SHell): User-friendly with auto-suggestions
  • PowerShell: Microsoft’s modern shell for Windows
  • Command Prompt (cmd): The traditional Windows command-line interpreter

The Prompt

The prompt is what you see when the shell is ready to accept a command. It typically includes:

  • Username: The account you’re logged in as
  • Hostname: The name of the computer
  • Current Directory: The folder you’re currently in
  • Symbol: Usually $ for regular users and # for the root user

Example prompt: username@hostname:~/documents$

Essential Commands

Here are some of the most important commands to learn, categorized by function:

File and Directory Management

CommandDescription
pwdPrint Working Directory - Shows your current location
lsList - Shows files and directories in the current directory
ls -lLong format - Shows detailed information (permissions, owner, size, date)
ls -aShow all files, including hidden ones (starting with .)
cdChange Directory - Move to a different directory
cd ..Move up one directory level
cd ~Move to your home directory
mkdirMake Directory - Create a new directory
rmdirRemove Directory - Delete an empty directory
touchCreate an empty file or update a file’s timestamp
cpCopy - Copy files or directories
mvMove - Move or rename files or directories
rmRemove - Delete files or directories
rm -rRecursive remove - Delete directories and their contents

File Manipulation

CommandDescription
catConcatenate - Display file contents
lessView file contents one page at a time
moreSimilar to less, but older and less feature-rich
headDisplay the beginning of a file
tailDisplay the end of a file
tail -fFollow - Display file contents as they’re written (great for logs)
nanoSimple text editor
vimPowerful text editor (steeper learning curve)
emacsAnother powerful text editor

System Information

CommandDescription
whoamiShow current username
dateDisplay current date and time
calDisplay calendar
dfDisk Free - Show disk space usage
duDisk Usage - Show file/directory space usage
freeShow memory usage
topDisplay running processes (real-time)
htopEnhanced version of top (often needs to be installed)
psProcess Status - List running processes
killTerminate a process

Network Commands

CommandDescription
pingTest network connectivity to a host
ip addrShow IP addresses and network interfaces (Linux)
ifconfigShow network interfaces (older, but still common)
netstatShow network connections
curlTransfer a URL - Make HTTP requests
wgetDownload files from the web
sshSecure Shell - Connect to remote servers
scpSecure Copy - Copy files between computers

Permissions

CommandDescription
chmodChange Mode - Change file permissions
chownChange Owner - Change file ownership
chgrpChange Group - Change file group

Command Line Tips and Tricks

1. Tab Completion

Press the Tab key to auto-complete commands, filenames, and directory names. Press Tab twice to see all possible completions.

2. Command History

  • Use the up and down arrow keys to navigate through previously executed commands
  • Press Ctrl+R to search your command history
  • Type history to see a list of all commands you’ve run

3. Wildcards

Wildcards allow you to match multiple files with a single pattern:

  • *: Matches zero or more characters (e.g., *.txt matches all files ending with .txt)
  • ?: Matches exactly one character (e.g., file?.txt matches file1.txt, file2.txt, etc.)
  • []: Matches any character within the brackets (e.g., file[123].txt matches file1.txt, file2.txt, file3.txt)

4. Input/Output Redirection

Redirect the output of a command to a file or use a file as input:

  • >: Redirect output (overwrites the file)
  • >>: Append output (adds to the end of the file)
  • <: Redirect input from a file

Examples:

ls -l > file_list.txt       # Save file list to a file
cat file.txt >> another.txt  # Append file contents to another file
wc -l < file.txt             # Count lines in a file

5. Pipes

Use the pipe operator | to send the output of one command as input to another command:

ls -l | grep .txt            # List files and filter for .txt files
ps aux | grep nginx          # Find processes related to nginx
df -h | grep /dev/sda1       # Check disk space for a specific partition

6. Aliases

Create shortcuts for long commands:

alias ll='ls -lh'
alias update='sudo apt update && sudo apt upgrade'
alias cls='clear'

7. Background Processes

Run a command in the background by adding & at the end:

./my_long_running_script.sh &

8. Job Control

  • Ctrl+C: Interrupt (stop) the current foreground process
  • Ctrl+Z: Suspend the current foreground process
  • jobs: List suspended jobs
  • fg: Bring a suspended job to the foreground
  • bg: Resume a suspended job in the background

9. Command Chaining

Execute multiple commands in sequence:

  • &&: Run the next command only if the previous one succeeded
  • ||: Run the next command only if the previous one failed
  • ;: Run commands sequentially regardless of success

Examples:

mkdir my_

My Private Notes

Notes are auto-saved locally to this device.