The terminal is the heart of Linux, and mastering basic commands is your gateway to becoming proficient with the system. While graphical interfaces are convenient, the command line offers unmatched power, flexibility, and speed once you know the fundamentals.
Before diving into commands, let's understand why the terminal is so important:
- Speed: Many tasks are faster via command line than clicking through menus
- Keyboard Shortcut: Ctrl + Alt + T
(most distributions)
When you open a terminal, you'll see something like:
username@hostname:~$
Let's break this down:
username
: Your current user accounthostname
: Your computer's name~
: Current directory (~ represents your home directory)$
: Indicates you're a regular user (# means root/admin)The pwd
(Print Working Directory) command shows your current location:
pwd
Example Output:
/home/username
The ls
(List) command shows files and directories in your current location:
ls # Basic listing
ls -l # Detailed listing (long format)
ls -la # Include hidden files (starting with .)
ls -lh # Human-readable file sizes
ls /path/to/directory # List specific directory
Practical Examples:
ls -la ~/Documents # List all files in Documents folder
ls -lh *.txt # List only .txt files with sizes
ls -lt # Sort by modification time (newest first)
The cd
(Change Directory) command lets you navigate between folders:
cd /home/username/Documents # Go to specific path
cd Documents # Go to Documents (relative path)
cd .. # Go up one directory
cd ../.. # Go up two directories
cd ~ # Go to home directory
cd - # Go to previous directory
cd # Go to home directory (shortcut)
Navigation Shortcuts:
.
= Current directory..
= Parent directory~
= Home directory/
= Root directory-
= Previous directorymkdir new_folder # Create single directory
mkdir -p path/to/nested/dirs # Create nested directories
mkdir dir1 dir2 dir3 # Create multiple directories
touch newfile.txt # Create empty file
touch file1.txt file2.txt # Create multiple files
cp file.txt backup.txt # Copy file
cp file.txt ~/Documents/ # Copy to different directory
cp -r folder/ backup_folder/ # Copy directory recursively
cp *.txt ~/backup/ # Copy all .txt files
mv oldname.txt newname.txt # Rename file
mv file.txt ~/Documents/ # Move file to directory
mv folder/ ~/backup/ # Move directory
rm file.txt # Remove file
rm -r directory/ # Remove directory recursively
rm -f file.txt # Force remove (no confirmation)
rm -rf directory/ # Force remove directory
rm *.tmp # Remove all .tmp files
⚠️ Warning: rm
permanently deletes files. There's no recycle bin!
cat file.txt # Display entire file
cat file1.txt file2.txt # Display multiple files
cat > newfile.txt # Create file with content (Ctrl+D to save)
less largefile.txt # View file page by page
more largefile.txt # Similar to less (older version)
Navigation in less:
Space
or Page Down
: Next pageb
or Page Up
: Previous pageq
: Quit/search_term
: Search forward?search_term
: Search backwardhead file.txt # Show first 10 lines
head -n 5 file.txt # Show first 5 lines
tail file.txt # Show last 10 lines
tail -n 20 file.txt # Show last 20 lines
tail -f logfile.txt # Follow file changes (great for logs)
grep "search_term" file.txt # Search for text in file
grep -i "search_term" file.txt # Case-insensitive search
grep -r "search_term" directory/ # Search recursively in directory
grep -n "search_term" file.txt # Show line numbers
grep "^start" file.txt # Lines starting with "start"
grep "end$" file.txt # Lines ending with "end"
wc file.txt # Count lines, words, characters
wc -l file.txt # Count only lines
wc -w file.txt # Count only words
wc -c file.txt # Count only characters
date # Current date and time
date +"%Y-%m-%d %H:%M:%S" # Custom format
whoami # Current username
uname -a # All system information
uname -r # Kernel version
uname -m # Machine hardware
df -h # Show disk space (human-readable)
du -h directory/ # Show directory size
du -sh * # Show size of all items in current directory
man ls # Show manual for ls command
man -k search_term # Search for commands related to term
Navigation in man pages:
Space
: Next pageb
: Previous page/search
: Search forwardq
: Quitls --help # Quick help for ls command
grep --help # Quick help for grep command
history # Show command history
history | grep "search" # Search command history
!123 # Run command number 123 from history
!! # Run last command
!grep # Run last command starting with "grep"
- Ctrl + C
: Cancel current command
Ctrl + D
: Exit terminal/End of inputCtrl + L
: Clear screen (same as clear
command)Ctrl + A
: Move cursor to beginning of lineCtrl + E
: Move cursor to end of lineCtrl + U
: Delete from cursor to beginning of lineCtrl + K
: Delete from cursor to end of lineTab
: Auto-complete commands and filenamesUp Arrow
: Previous commandDown Arrow
: Next command# Navigate to your home directory
cd ~
# List all files including hidden ones
ls -la
# Create a practice directory
mkdir linux_practice
# Enter the directory
cd linux_practice
# Create some test files
touch file1.txt file2.txt file3.txt
# Create a subdirectory
mkdir subdirectory
# Copy a file to the subdirectory
cp file1.txt subdirectory/
# List the contents
ls -la subdirectory/
# Create a file with content
echo "Hello, Linux!" > greeting.txt
# Display the content
cat greeting.txt
# Append more content
echo "This is my first Linux file." >> greeting.txt
# Display the updated content
cat greeting.txt
# Count lines and words
wc greeting.txt
# Create a file with multiple lines
cat > fruits.txt << EOF
apple
banana
cherry
date
elderberry
fig
grape
EOF
# Search for fruits containing 'a'
grep "a" fruits.txt
# Count total lines
wc -l fruits.txt
# Show only the first 3 fruits
head -n 3 fruits.txt
ls -la | grep "\.txt" # List only .txt files
cat file.txt | grep "search" | wc -l # Count matching lines
history | tail -n 10 # Show last 10 commands
ls -la | sort -k 5 -n # Sort files by size
Always use Tab to auto-complete commands and file names. It saves time and prevents typos.
Use the Up arrow to recall previous commands. Learn to search history with Ctrl + R
.
Always double-check before using rm
, especially with -r
or -f
flags.
When working in a directory, use relative paths (like ./file.txt
) instead of full paths when possible.
Linux error messages are usually helpful. Read them carefully to understand what went wrong.
Begin with read-only commands like ls
, cat
, pwd
before moving to commands that modify files.
1. Case Sensitivity: Linux is case-sensitive. File.txt
and file.txt
are different files.
2. Spaces in Names: Avoid spaces in file names, or use quotes: "my file.txt"
3. Running as Root: Don't use sudo
unless necessary. Regular user permissions are safer.
4. Not Using Tab Completion: Typing full paths manually increases error chances.
5. Ignoring File Extensions: Unlike Windows, Linux doesn't rely on extensions, but they help with organization.
Now that you've mastered basic terminal navigation and file operations, you're ready to explore:
- File permissions and ownership
- The terminal is a powerful tool that becomes intuitive with practice
pwd
, ls
, cd
, cp
, mv
, rm
, cat
, grep
man command
) are your best friend for learning# Navigation
pwd # Show current directory
ls -la # List all files with details
cd directory # Change to directory
cd .. # Go up one level
cd ~ # Go to home directory
# File Operations
mkdir name # Create directory
touch file # Create empty file
cp src dest # Copy file/directory
mv src dest # Move/rename file/directory
rm file # Remove file
rm -r dir # Remove directory
# Viewing Files
cat file # Display file content
less file # View file page by page
head file # Show first 10 lines
tail file # Show last 10 lines
grep text file # Search for text in file
# System Info
whoami # Current user
date # Current date/time
df -h # Disk space
man command # Manual for command
Remember: The terminal might seem intimidating at first, but it's just another interface to your computer. With these fundamental commands, you're well on your way to becoming proficient with Linux. Practice regularly, don't be afraid to explore, and always remember that the Linux community is there to help when you need it!
---
This is Part 2 of our comprehensive Linux mastery series.
Previous: Why Linux? A Beginner's Introduction - Start your Linux journey with the fundamentals
Next: Linux File System Hierarchy - Understand how directories are organized in Linux
Beginner Foundation:
Next Steps: Continue building your foundation with file system navigation and management!
---
Ready to dive deeper? Next, we'll explore the Linux file system structure and understand how directories are organized in the Linux hierarchy.