Basic Linux Commands (ls, pwd, cd, mkdir, rm, cp, mv, cat, etc.)

Mastering basic Linux commands is the first step toward becoming proficient in system administration and DevOps. This comprehensive guide covers essential commands for navigation, file management, and daily operations with practical examples and best practices.

Essential Navigation Commands

📂
pwd

Print Working Directory - Shows your current directory location

pwd

Example: pwd outputs /home/username

📁
ls

List Directory Contents - Shows files and directories

ls [options] [directory]

Common Options:

  • ls -l - Detailed list
  • ls -a - Show hidden files
  • ls -la - Detailed list with hidden files
🚪
cd

Change Directory - Move between directories

cd [directory]

Common Uses:

  • cd /path/to/dir - Absolute path
  • cd .. - Go up one level
  • cd ~ - Go to home directory
  • cd - - Go to previous directory

File and Directory Management

📝
mkdir

Make Directory - Create new directories

mkdir [options] directory_name

Examples:

  • mkdir projects - Create single directory
  • mkdir -p projects/{src,doc,bin} - Create nested directories
📄
touch

Create Empty File - Create new files or update timestamps

touch filename

Examples:

  • touch file.txt - Create empty file
  • touch file1.txt file2.txt - Create multiple files
🗑️
rm

Remove Files/Directories - Delete files and directories

rm [options] file/directory

Common Options:

  • rm file.txt - Remove file
  • rm -r directory - Remove directory recursively
  • rm -f file - Force remove without confirmation
Danger Zone: The rm -rf / command can destroy your entire system! Always double-check what you're deleting, especially when using -r (recursive) and -f (force) options.

File Operations Commands

📋
cp

Copy Files/Directories - Duplicate files and directories

cp [options] source destination

Examples:

  • cp file1.txt file2.txt - Copy file
  • cp -r dir1 dir2 - Copy directory recursively
  • cp -v file* backup/ - Verbose copy with wildcard
🚚
mv

Move/Rename Files - Move files or rename them

mv [options] source destination

Examples:

  • mv old.txt new.txt - Rename file
  • mv file.txt /new/location/ - Move file
  • mv *.txt documents/ - Move multiple files
👁️
cat

Concatenate and Display - View file contents

cat [options] filename

Common Uses:

  • cat file.txt - Display file content
  • cat file1.txt file2.txt - Concatenate files
  • cat > newfile.txt - Create file from input

File Viewing and Editing Commands

📖
less

Page Through Files - View files page by page

less filename

Navigation in less:

  • Space - Next page
  • b - Previous page
  • q - Quit
  • /pattern - Search
📊
head & tail

View File Beginnings/Endings - Show first/last lines

head/tail [options] filename

Examples:

  • head -n 10 file.log - First 10 lines
  • tail -f app.log - Follow log in real-time
  • tail -n 20 file.log - Last 20 lines
🔍
grep

Search Text - Find patterns in files

grep [options] pattern file

Examples:

  • grep "error" logfile.txt - Search for "error"
  • grep -r "function" src/ - Recursive search
  • grep -i "warning" file.txt - Case-insensitive
Pro Tip: Use the tab key for auto-completion of file and directory names. Press tab twice to see available options when multiple matches exist.

Practical Command Examples

Common Workflow Examples:

Navigate and Explore:
pwd - Check where you are
ls -la - See everything in current directory
cd ~/Documents - Go to Documents folder

File Management:
mkdir myproject - Create project directory
cd myproject - Enter the directory
touch script.sh README.md - Create project files
cp script.sh backup/ - Backup your script

Content Viewing:
cat README.md - Quick file view
less largefile.log - Page through large file
tail -f /var/log/syslog - Monitor system logs

Quick Reference Cheat Sheet

Navigation

  • pwd - Show current directory
  • ls - List files
  • cd - Change directory
  • cd ~ - Go home
  • cd .. - Go up

File Operations

  • touch - Create file
  • cp - Copy file
  • mv - Move/rename
  • rm - Delete file
  • mkdir - Create directory

Viewing Content

  • cat - Display file
  • less - Page through
  • head - Show beginning
  • tail - Show end
  • grep - Search text

Hands-On Practice Exercise

Try these commands in sequence to practice your skills:

# 1. Check your current location
pwd

# 2. Create a practice directory
mkdir linux_practice
cd linux_practice

# 3. Create some files
touch file1.txt file2.txt document.md
echo "Hello World" > greeting.txt

# 4. List files with details
ls -la

# 5. Create a subdirectory and move files
mkdir texts
mv *.txt texts/

# 6. View file contents
cat texts/greeting.txt

# 7. Copy a file
cp texts/greeting.txt backup_greeting.txt

# 8. Search for specific content
grep "Hello" texts/greeting.txt

# 9. Clean up (optional)
cd ..
rm -rf linux_practice

Key Takeaways

Mastering these basic Linux commands is essential for efficient system navigation and file management. Remember to practice regularly, use tab completion, and always double-check destructive commands like rm. These fundamentals form the foundation for more advanced Linux administration and DevOps tasks.

Next Step: Explore file permissions and ownership commands (chmod, chown, chgrp) to understand Linux security basics.