Linux Basic Commands Introduction

beginner | 60 min read | 2025.12.02

What You’ll Learn in This Tutorial

  • Navigating and checking directories
  • File and directory operations
  • Displaying file contents
  • Basic file searching

History of Linux and UNIX

Why Learn Linux

Linux runs on approximately 96% of the world’s servers and is used everywhere from smartphones (Android) to supercomputers and IoT devices. Command line skills are essential for engineers.

The Birth of UNIX

The roots of modern Linux commands trace back to 1969. UNIX was developed at Bell Labs by Ken Thompson and Dennis Ritchie.

“UNIX is an operating system made by programmers for programmers” — Brian Kernighan

The Birth of Linux

In 1991, Finnish university student Linus Torvalds developed a free OS kernel compatible with UNIX. His famous post:

“Hello everybody out there using minix. I’m doing a (free) operating system (just a hobby, won’t be big and professional like gnu) for 386(486) AT clones.” — Linus Torvalds, 1991

UNIX Philosophy

The UNIX design philosophy has greatly influenced modern software development:

  1. Combine small programs: Create programs that do one thing well and combine them
  2. Text streams: Use text for communication between programs
  3. Early prototyping: Build something that works first, then improve

“This is the Unix philosophy: Write programs that do one thing and do it well.” — Doug McIlroy

Terminal and Shell

What is a Terminal

A terminal (terminal emulator) is an application that provides a command-line interface.

  • macOS: Terminal.app, iTerm2
  • Windows: Windows Terminal, PowerShell
  • Linux: GNOME Terminal, Konsole

What is a Shell

A shell is a program that interprets user commands and communicates with the OS.

ShellDescription
bashMost common. Default for Linux/macOS (older macOS)
zshDefault since macOS Catalina. bash-compatible
fishFeature-rich and user-friendly
shPOSIX standard shell. Highly portable
# Check your current shell
echo $SHELL

Basic Directory Operations

pwd - Check Current Location

Use pwd (Print Working Directory) to check your current directory.

$ pwd
/home/username

Fun fact: In UNIX, directories are also treated as “files”. A directory is a “special file that contains links to other files”.

ls - List Files

Use ls to list files in a directory.

# Basic listing
$ ls

# Detailed view (permissions, size, etc.)
$ ls -l

# Show hidden files too
$ ls -a

# Detailed + hidden files
$ ls -la

# Human-readable sizes
$ ls -lh

Reading ls -l Output

FieldDescription
-rw-r--r--File type and permissions (owner/group/others)
1Hard link count
userOwner
groupOwner group
4096File size
Jan 1 12:00Modified date
file.txtFilename

File types: - = regular file, d = directory, l = symbolic link

Official Documentation: GNU Coreutils - ls

cd - Change Directory

# Go to specified directory
$ cd /var/log

# Go to home directory
$ cd ~
$ cd

# Go up one directory
$ cd ..

# Go back to previous directory
$ cd -

Types of Paths

TypeDescriptionExample
Absolute pathComplete path starting from root (/)/home/user/docs
Relative pathPath relative to current location./docs or ../other

Special directory notation:

  • . - Current directory
  • .. - Parent directory
  • ~ - Home directory
  • / - Root directory

Creating and Deleting Files/Directories

mkdir - Create Directory

# Create directory
$ mkdir myproject

# Create deep hierarchy at once (-p: parents)
$ mkdir -p myproject/src/components

# Set permissions when creating
$ mkdir -m 755 secure_dir

Best Practice: Always use the -p option when creating multiple levels. It won’t error if the directory already exists.

touch - Create File

touch originally “updates a file’s timestamp”, but creates an empty file if it doesn’t exist.

# Create empty file
$ touch index.html

# Create multiple files at once
$ touch style.css script.js

# Set timestamp to specific date
$ touch -t 202501011200 file.txt

Official Documentation: GNU Coreutils - touch

rm - Delete File/Directory

# Delete file
$ rm file.txt

# Delete directory (including contents)
$ rm -r mydir

# Delete without confirmation (caution!)
$ rm -rf mydir

# Ask before deleting
$ rm -i file.txt

Warning: rm -rf deletes immediately without confirmation. There is no recycle bin. Be especially careful when using it near / or with variable expansion (like rm -rf $DIR/).

Best Practices for Safe Deletion

# 1. Check with ls first
ls /path/to/delete

# 2. Delete with -i option for confirmation
rm -ri /path/to/delete

# 3. Set up alias (add to ~/.bashrc)
alias rm='rm -i'

Copying and Moving Files

cp - Copy

# Copy file
$ cp original.txt copy.txt

# Copy directory (-r: recursive)
$ cp -r mydir mydir_backup

# Copy multiple files to directory
$ cp file1.txt file2.txt destination/

# Preserve timestamp and permissions
$ cp -p original.txt copy.txt

# Ask before overwriting
$ cp -i source.txt dest.txt

Official Documentation: GNU Coreutils - cp

mv - Move/Rename

mv is used for both moving and renaming files.

# Move file
$ mv file.txt /path/to/destination/

# Rename file
$ mv oldname.txt newname.txt

# Move directory
$ mv mydir /path/to/destination/

# Ask before overwriting
$ mv -i source.txt dest.txt

Displaying File Contents

CommandDescriptionUsage
catDisplay entire filecat file.txt
headDisplay first N lines (default 10)head -n 20 file.txt
tailDisplay last N linestail -f log.txt
lessDisplay with pager (scrollable)less file.txt
moreOlder pagermore file.txt

cat Applications

# Display line numbers
$ cat -n file.txt

# Concatenate multiple files
$ cat file1.txt file2.txt > combined.txt

# Compress empty lines
$ cat -s file.txt

less Navigation

less lets you interactively browse files:

  • Space / f - Forward one page
  • b - Back one page
  • /pattern - Search forward
  • ?pattern - Search backward
  • n - Next search result
  • N - Previous search result
  • g - Go to beginning
  • G - Go to end
  • q - Quit

Fun fact: The name less comes from “less is more”. It’s an improved version of the more command.

Log Monitoring with tail -f

# Monitor log in real-time
$ tail -f /var/log/syslog

# Monitor multiple files simultaneously
$ tail -f file1.log file2.log

# Start monitoring from last 100 lines
$ tail -n 100 -f app.log

Permission Basics

Reading Permissions

PositionMeaningExample
1File type- (file), d (directory), l (link)
2-4Owner permissionsrwx (all permissions)
5-7Group permissionsr-x (read + execute)
8-10Others permissionsr-- (read only)
SymbolNumberMeaning
r4read
w2write
x1execute
-0no permission

chmod - Change Permissions

# Specify with numbers (755 = rwxr-xr-x)
$ chmod 755 script.sh

# Specify with symbols
$ chmod u+x script.sh    # Add execute to owner
$ chmod g-w file.txt     # Remove write from group
$ chmod o=r file.txt     # Set others to read only
$ chmod a+r file.txt     # Add read to all

Official Documentation: GNU Coreutils - chmod

Commonly Used Permissions

PermissionUse Case
755Executable scripts
644Regular files
600Sensitive files (SSH keys, etc.)
700Private directories

Practice: Create a Project Structure

Let’s use the commands we learned to create a basic web project structure.

# Create project directory
mkdir -p mywebsite/{css,js,images}

# Create basic files
touch mywebsite/index.html
touch mywebsite/css/style.css
touch mywebsite/js/main.js

# Check structure
ls -R mywebsite

# Use tree for better visualization if available
tree mywebsite

Practical Project Setup

# Node.js project creation example
mkdir -p my-app/{src/{components,utils,styles},tests,docs}
touch my-app/{package.json,README.md,.gitignore}
touch my-app/src/index.js

# Check structure
tree my-app
# my-app
# ├── README.md
# ├── docs
# ├── package.json
# ├── src
# │   ├── components
# │   ├── index.js
# │   ├── styles
# │   └── utils
# └── tests

Common Mistakes and Troubleshooting

Filenames with Spaces

# Wrong: Filename split by spaces
$ rm my file.txt  # Tries to delete "my" and "file.txt"

# Correct: Wrap in quotes
$ rm "my file.txt"
$ rm 'my file.txt'

# Or escape with backslash
$ rm my\ file.txt

Permission Errors

# For Permission denied errors
$ sudo command  # Run with admin privileges

# Check file permissions
$ ls -l file.txt

# If missing execute permission
$ chmod +x script.sh

File Not Found

# Search for file
$ find /path -name "filename"

# Partial match search
$ find . -name "*.txt"

# locate command (uses database, fast)
$ locate filename

Useful Shortcuts and Tips

Terminal Shortcuts

ShortcutDescription
TabAuto-complete commands and paths
Ctrl + CCancel running command
Ctrl + LClear screen (same as clear)
Ctrl + AGo to beginning of line
Ctrl + EGo to end of line
Ctrl + UDelete left of cursor
Ctrl + KDelete right of cursor
Ctrl + RSearch command history
/ Browse command history
!!Re-run last command

Combining Commands

# Pipe (|): Pass output to next command
$ ls -la | grep ".txt"

# Redirect (>): Save output to file
$ ls > filelist.txt

# Append (>>): Append to file
$ echo "new line" >> file.txt

# Command chaining
$ mkdir newdir && cd newdir  # Run next only if successful
$ command1 || command2       # Run next only if failed
$ command1 ; command2        # Run in sequence

Next Steps

Once you’ve mastered basic commands, learn shell scripting to automate your work.

Official Documentation

Cheat Sheets/Tools

← Back to list