Post

Linux-M2 - Introduction to Linux Commands

Learn how to use common Linux commands. what a shell and shell commands are, and how to use commands to do various tasks in Linux. How to use informational commands to find relevant information about your system, navigation commands to navigate files and directories, and management commands to create, delete, copy, and move files and directories. Learn to sort and view files in useful ways and extract specific lines and fields from your files. Use networking commands to examine your network configuration and evaluate, identify, and retrieve data from URLs. Cover file archiving and compression commands.

Linux-M2 - Introduction to Linux Commands

Overview of Common Linux Shell Commands


1. What is a Shell?

Definition

A shell is a powerful command-line interpreter used in Unix-like operating systems (like Linux and macOS).

  • It acts as both:
    • An interactive command language for users to interact with the OS.
    • A scripting language for automating tasks.

Common Shells in Linux

ShellDescription
Bash (/bin/bash)Bourne Again SHell – Default on most Linux distros
shOriginal Bourne shell
kshKornShell – Enhanced version of sh
tcshExtended C Shell with advanced features
zshZ Shell – Modern, feature-rich
fishFriendly Interactive SHell – beginner-friendly

In this course, we will use Bash, the default shell on most Linux systems.

How to Check Your Current Shell

1
printenv SHELL

Example output:

1
/bin/bash

If not Bash, you can switch by typing:

1
bash

2. Applications of Shell Commands

Shell commands are essential for various tasks such as:

Task CategoryExamples
System Infouname, date, whoami
File Managementcp, mv, rm, touch
Directory Navigationls, cd, pwd, mkdir
Text Displaycat, head, tail, echo
Search & Filtergrep, find
Compressiontar, zip, unzip
Networkingcurl, wget, ping, hostname
Performance Monitoringtop, ps, df

3. Common Shell Commands

A. System Information Commands

CommandDescription
whoamiDisplays current username
idShows user and group IDs
unameDisplays OS name and version
psLists running processes
topReal-time system monitoring tool
dfShows disk space usage
man [command]Displays manual/help for any command
datePrints current date and time

B. File Management Commands

CommandDescription
cp file1 file2Copy files or directories
mv file1 file2Move or rename files
rm fileRemove/delete a file
touch fileCreate an empty file or update timestamp
chmod permissions fileChange file access permissions
wc fileCount lines, words, characters in a file
grep "pattern" fileSearch for text within a file

C. Directory Navigation Commands

CommandDescription
lsList contents of current directory
find . -name "*.txt"Find files matching a pattern
pwdPrint working (current) directory
mkdir dir_nameMake a new directory
cd dir_nameChange to another directory
rmdir dir_nameRemove an empty directory

D. File Content Viewing Commands

CommandDescription
cat file.txtConcatenate and print full file content
more file.txtView file content one page at a time
head file.txtShow first few lines of a file
tail file.txtShow last few lines of a file
echo "text"Print text or variable values to terminal

E. File Compression & Archiving

CommandDescription
tar -cvf archive.tar file1 file2Archive files into .tar
tar -xvf archive.tarExtract from .tar file
zip archive.zip file1 file2Compress files into .zip
unzip archive.zipExtract .zip archive

F. Networking Commands

CommandDescription
hostnameShow system hostname
ping google.comTest network connectivity
ifconfigConfigure or view network interfaces
curl http://example.comTransfer data from a URL
wget http://example.com/file.txtDownload files from the web

4. Running Linux on Windows

You can run Linux commands on a Windows machine using these methods:

MethodDescription
Dual BootInstall Linux alongside Windows on separate partitions
Virtual Machine (VM)Run Linux inside a VM like VirtualBox or VMware
Linux Emulator (e.g., Cygwin)Run Linux tools in a simulated environment
Windows Subsystem for Linux (WSL)Native Linux compatibility layer on Windows 10/11

5. Summary

✅ The role of a shell in Linux as both a command interpreter and scripting tool.
✅ How to check and switch shells using commands like printenv SHELL and bash.
✅ The main applications of shell commands across system info, file management, networking, and automation.
✅ The most common shell commands grouped by function for easy reference.


Quick Reference Table

PurposeCommand
User Infowhoami, id
System Infouname, date, top, df
File Opscp, mv, rm, touch, chmod, grep
Dir Navigationls, cd, pwd, mkdir, rmdir, find
View Contentcat, head, tail, more, echo
Compressiontar, zip, unzip
Networkingcurl, wget, ping, hostname, ifconfig


🔧 Linux Shell Commands Cheat Sheet

This cheat sheet includes common Linux shell commands, their syntax, and examples. Designed for quick reference and learning.


🧑‍💻 Basic Navigation

CommandDescriptionExample
pwdPrint Working Directorypwd/home/user/documents
cd <dir>Change Directorycd /home/user
lsList files in current directoryls
ls -lLong listing formatls -l
ls -aShow hidden filesls -a

🗂️ File & Directory Management

CommandDescriptionExample
touch <file>Create an empty filetouch myfile.txt
mkdir <dir>Make a new directorymkdir myfolder
cp <src> <dest>Copy files/dirscp file.txt backup/
mv <src> <dest>Move or rename filesmv old.txt new.txt
rm <file>Remove/delete a filerm temp.txt
rm -r <dir>Remove a directoryrm -r myfolder
rmdir <dir>Remove an empty directoryrmdir empty_folder

📄 Viewing and Editing Files

CommandDescriptionExample
cat <file>View entire filecat myfile.txt
more <file>View file page by pagemore bigfile.txt
head <file>Show first 10 lineshead myfile.txt
tail <file>Show last 10 linestail myfile.txt
nano <file>Edit file using nanonano notes.txt
echo "<text>" > <file>Write text to a fileecho "Hello" > hello.txt

🔍 Searching and Filtering

CommandDescriptionExample
grep "<pattern>" <file>Search for pattern in filegrep "error" log.txt
find <path> -name "<pattern>"Find files matching namefind . -name "*.log"
wc <file>Count lines, words, characterswc myfile.txt

🧰 System Information

CommandDescriptionExample
whoamiShow current userwhoami
idShow user and group IDsid
uname -aShow OS infouname -a
dateShow current date/timedate
topReal-time process monitortop
df -hDisk space usagedf -h
psList running processesps

📦 Compression & Archiving

CommandDescriptionExample
tar -cvf <archive.tar> <files>Create tar archivetar -cvf backup.tar *.txt
tar -xvf <archive.tar>Extract tar archivetar -xvf backup.tar
zip <archive.zip> <files>Compress fileszip docs.zip *.docx
unzip <archive.zip>Extract zip archiveunzip docs.zip

🌐 Networking

CommandDescriptionExample
hostnameShow system hostnamehostname
ping <host>Test network connectionping google.com
ifconfigShow network interfaces (deprecated)ifconfig
ip addrAlternative to ifconfigip addr show
curl <url>Download from URLcurl http://example.com
wget <url>Download and save filewget http://example.com/file.txt

💾 Package Management

Debian/Ubuntu (.deb)

CommandDescriptionExample
sudo apt updateUpdate package listsudo apt update
sudo apt upgradeUpgrade all packagessudo apt upgrade
sudo apt install <pkg>Install a packagesudo apt install curl

Red Hat/CentOS/Fedora (.rpm)

CommandDescriptionExample
sudo yum updateUpdate all packagessudo yum update
sudo yum install <pkg>Install a packagesudo yum install wget

🖥️ Shell Utilities

CommandDescriptionExample
man <command>View manual/helpman ls
historyShow command historyhistory
clearClear terminal screenclear
exitExit shell sessionexit

Linux Informational Commands Cheat Sheet


🔍 1. User & Identity Info

CommandDescriptionExample
whoamiShow current usernamewhoamijohn
idShow user and group IDsid
id -uShow numeric user IDid -u1001
id -unShow username from UIDid -unjohn

🖥️ 2. Operating System Info

CommandDescriptionExample
unameShow OS nameunameLinux
uname -sShow OS name (same as above)uname -s
uname -rShow kernel release versionuname -r5.15.0-76-generic
uname -vShow kernel versionuname -v
uname -aShow all system infouname -a

💾 3. Disk Usage Info

CommandDescriptionExample
df -h ~Disk usage of home directorydf -h ~
df -hDisk usage of all mounted filesystemsdf -h
du -sh <dir>Show size of a directorydu -sh /home/john

-h makes output human-readable (e.g., GB instead of bytes)


🔄 4. Process Monitoring

CommandDescriptionExample
psShow running processes (current shell only)ps
ps -eShow all running processesps -e
topReal-time process monitortop
top -n 3Show top 3 processestop -n 3
htopEnhanced interactive process viewer (if installed)htop

🖨️ 5. Printing Strings, Variables, and Dates

CommandDescriptionExample
echo "text"Print text to terminalecho "Hello World"
echo $PATHPrint value of an environment variableecho $HOME
datePrint current date/time (default format)date
date +"%j %Y"Print day of year and yeardate +"Day %j of %Y"Day 097 of 2023
date +"%A, %j, %Y"Day name, day of year, yeardate +"%A, %j, %Y"Tuesday, 097, 2023

📚 6. Command Manuals

CommandDescriptionExample
man whoamiView manual for whoamiman whoami
man idView manual for idman id
man dfView manual for dfman df
man psView manual for psman ps
man dateView manual for dateman date
man manLearn how to use the man commandman man

📚 Getting Help for Linux Commands – A Complete Guide

When working with Linux, knowing how to find help is just as important as knowing the commands themselves. Here’s a structured approach to getting assistance when you’re stuck or curious.


🔍 1. Use the Built-in man Command

The man (manual) command is the traditional and most comprehensive way to get help in Linux.

🧾 How to Use It:

1
man command_name

Example:

1
man ls

📘 Man Page Sections:

SectionDescription
NAMEName and brief description of the command
SYNOPSISHow to use the command, including options
DESCRIPTIONDetailed explanation of what the command does
OPTIONSList of available flags or arguments
EXAMPLESCommon usage examples
SEE ALSORelated commands or documentation

📋 View All Available Man Pages:

1
man -k .

This lists all commands with manual pages and a short description.


📝 2. Install and Use the tldr Command

tldr provides simplified, example-based help for common commands — perfect for quick reference.

💾 Install it:

If Node.js is installed:

1
npm install -g tldr

🚀 Usage:

1
tldr ls

You’ll see concise examples like:

1
2
3
4
5
6
7
8
ls
List directory contents.

- List files one per line:
  ls -1

- List all files, including hidden ones:
  ls -a

Ideal for users who want just enough info to get the job done.


💬 3. Search Stack Overflow

Stack Overflow is a powerful community-driven Q&A platform for developers and sysadmins.

🔍 Tips for Searching:

📌 Example query:
"how to delete a file in linux" site:stackoverflow.com


🤖 4. Search Stack Exchange (Unix & Linux)

Unix & Linux Stack Exchange is ideal for deeper technical questions.

🎯 Best For:

  • More detailed explanations than Stack Overflow
  • Complex issues involving system administration, scripting, or obscure commands

🔍 5. Just Google It!

Sometimes, a simple web search is the fastest way to solve a problem.

✅ Tips:

  • Include keywords like Linux, Ubuntu, or bash
  • Add sources like site:wikipedia.org or site:askubuntu.com
  • Avoid blindly trusting old or unverified results

📋 6. Use Course Cheat Sheets

Throughout this course, you’ll encounter cheat sheets that:

  • Condense key commands into easy-to-reference formats
  • Help you review and apply concepts faster
  • Are great for exam prep and real-world use

📄 7. Refer to Wikipedia’s List of Unix Commands

Wikipedia maintains a list of Unix/Linux commands: 🔗 List of Unix Commands (Wikipedia)

Includes:

  • Command name
  • Category (e.g., Filesystem, Process management)
  • Description
  • First appearance (e.g., Version 7 AT&T UNIX)

Great for historical context and discovering lesser-known utilities.


🧠 Summary Table: Ways to Get Help

MethodBest ForExample
manFull, official documentationman ls
tldrQuick examples and cheatsheet-style helptldr cp
Stack OverflowProgramming-related issuesSearch “how to rename file in Linux”
Stack ExchangeAdvanced Unix/Linux topicsVisit Unix & Linux SE
GoogleFast solutions and general knowledgeSearch “Linux sort file”
Cheat SheetsQuick reference and learningProvided in this course
WikipediaOverview and history of commandsList of Unix Commands

📌 Exercise 1 - Informational Commands

In this exercise, you explored several Linux commands that provide valuable system and user information. These commands are essential for understanding your environment, troubleshooting issues, and monitoring system performance.


🔍 Overview of Commands Covered

CommandPurpose
whoamiDisplays the current user
unameShows kernel/OS info
idDisplays user and group IDs
dfShows disk space usage
psLists running processes
topReal-time view of running processes and system load
echoPrints text or variables to the terminal
dateDisplays or sets the system date and time
manDisplays manual pages for commands

🧾 Detailed Breakdown

✅ 1.1 Display Current User

1
whoami
  • Output: theia
  • Tells you which user is currently logged in.

✅ 1.2 Get OS Info with uname

1
uname
  • Outputs: Linux

To get full system info:

1
uname -a

This includes:

  • Kernel name
  • Hostname
  • Kernel release
  • Kernel version
  • Machine hardware name
  • Processor type
  • Operating system

✅ 1.3 View User Identity

1
id
  • Shows:
    • uid: User ID
    • gid: Group ID
    • List of groups the user belongs to

✅ 1.4 Check Disk Space

1
df
  • Shows disk usage in 512-byte blocks

For human-readable format:

1
df -h
  • Shows sizes in KB, MB, GB

✅ 1.5 View Running Processes

1
ps
  • Lists processes owned by the current user

To see all processes:

1
ps -e

✅ 1.6 Real-Time Process Monitoring with top

1
top
  • Gives live updates of:
    • Running processes
    • CPU and memory usage
    • System uptime
    • Load average

To exit: Press q or Ctrl + C
To limit refreshes:

1
top -n 10

Sorting options (while top is running):

  • Shift + M → Sort by memory
  • Shift + P → Sort by CPU usage
  • Shift + N → Sort by PID
  • Shift + T → Sort by runtime

✅ 1.7 Print Messages with echo

1
echo "Welcome to the Linux lab"

Output:

1
Welcome to the Linux lab

Use -e to interpret escape characters:

1
echo -e "Line one\nLine two"

Output:

1
2
Line one
Line two

Common escape characters:

  • \n – New line
  • \t – Tab

✅ 1.8 Show Date and Time

1
date
  • Shows full current date and time

Custom formats:

1
2
date "+%D"     # mm/dd/yy
date "+%Y-%m-%d %T"  # 2025-04-05 14:30:00

Popular format specifiers: | Specifier | Meaning | |———-|———| | %d | Day of month | | %h | Abbreviated month (Jan-Dec) | | %m | Month number | | %Y | 4-digit year | | %T | Time in HH:MM:SS | | %H | Hour (00–23) |


✅ 1.9 View Manual Pages

1
man ls
  • Opens the manual page for the ls command

To list all available man pages:

1
man -k .

Man pages typically include:

  • NAME
  • SYNOPSIS
  • DESCRIPTION
  • OPTIONS
  • EXAMPLES
  • SEE ALSO

📋 Summary Table

TaskCommand
Show current userwhoami
Show OS infouname, uname -a
Show user identityid
Show disk usagedf, df -h
List running processesps, ps -e
Monitor processes livetop, top -n 10
Print textecho, echo -e
Show date/timedate, date "+format"
View command helpman command, man -k .

💡 Tips & Best Practices

  • Use --help with many commands for quick usage examples:
    1
    
    ls --help
    
  • Combine man and tldr for both depth and speed:
    1
    
    tldr df
    
  • Use watch for repeated execution:
    1
    
    watch df -h
    

📚 Practice Exercises with Solutions

Here are the practice exercises along with hints and complete solutions to reinforce your understanding of essential Linux commands.


🔹 1. Get basic information about the operating system

💡 Hint:

Use the uname command for kernel and OS-related information.

✅ Solution:

1
uname -a

This will display detailed system information, including:

  • Kernel name
  • Hostname
  • Kernel release version
  • Machine hardware name
  • Operating system

🔹 2. View all running processes on the system

💡 Hint:

The ps command lists running processes. Use an option to show all processes.

✅ Solution:

1
ps -e

This shows every process currently running on the system, not just those owned by the current user.


🔹 3. Get the table of processes and sort by memory usage

💡 Hint:

Use the top command and a keyboard shortcut to sort by memory.

✅ Solution:

1
top

Once inside top, press:

1
Shift + M

This sorts the list by memory usage, allowing you to quickly identify which processes are using the most RAM.

To exit top, press q.


🔹 4. Display the current time

💡 Hint:

Use the date command with a format specifier to show only the time.

✅ Solution:

1
date "+%T"

Output example:

1
14:25:36

Other useful formats:

  • date "+%H:%M"14:25
  • date "+%r"02:25:36 PM

🔹 5. Using one command, display the messages “Hello!” and “Goodbye!” separated by a new line

💡 Hint:

Use the echo command with the -e option to interpret escape sequences like \n.

✅ Solution:

1
echo -e "Hello!\nGoodbye!"

Output:

1
2
Hello!
Goodbye!

The -e flag enables interpretation of backslash escapes like \n (new line) and \t (tab).


🎯 Bonus Tips

  • Combine echo with redirection to write text into files:
    1
    
    echo "Hello World" > hello.txt
    
  • Use watch to monitor changing output:
    1
    
    watch "ps -e | head -n 10"
    

File and Directory Navigation Commands in Linux


Introduction

This video introduces the essential Linux commands for navigating files and directories. After watching, you will be able to:

✅ Use ls to list directory contents
✅ Navigate between directories using cd
✅ Understand relative vs absolute paths
✅ Search for files using the find command


1. List Files and Directories: ls Command

Purpose

The ls (list) command is used to display the contents of a directory.

Basic Usage

  • To list files in the current directory:

    1
    
    ls
    
  • To list files in a specific directory:

    1
    
    ls Downloads
    

Common Options

OptionDescriptionExample
-lLong format – shows permissions, owner, size, date, and namels -l
-aShow hidden files (files starting with .)ls -a
-laDetailed list including hidden filesls -la

The output from ls -l includes:

  • File type and permissions (drwxr-xr-x)
  • Number of links
  • Owner name
  • Group name
  • Size in bytes
  • Date of last modification
  • File or directory name

2. Show Current Working Directory: pwd

Purpose

The pwd (print working directory) command shows your current location in the file system.

Usage

1
pwd

Example Output:

1
/home/user/Documents

Useful when:

  • You’re unsure which directory you are in.
  • You want to verify your location before running commands.

3. Change Directory: cd Command

Purpose

The cd (change directory) command lets you move between directories.

Basic Syntax

1
cd <directory_path>

Examples

CommandDescription
cd DocumentsMove into the Documents subdirectory
cd ..Move up one level to the parent directory
cd ~ or just cdGo to your home directory
cd /home/user/Documents/NotesMove to an absolute path
cd ./projectsMove to a relative path

Understanding Path Types

TypeDescriptionExample
Absolute PathStarts from root /, always starts with //var/log/syslog
Relative PathRelative to current working directory../images/logo.png

4. Find Files: find Command

Purpose

The find command helps you locate files and directories based on various criteria like name, type, size, etc.

Basic Syntax

1
find <starting_directory> <options>

Common Examples

CommandDescription
find . -name "a.txt"Find all files named a.txt in current directory (.)
find /home/user -name "*.txt"Find all .txt files in user’s home directory
find . -iname "a.txt"Case-insensitive search for a.txt
find . -type d -name "Images"Find directories named Images
find . -type fFind only files in current directory tree

Tip: The . means “start searching here” — it’s useful when you want to limit the scope of your search.


5. Summary of Key Concepts

ls – Listing Contents

  • Lists files and directories.
  • Use options like -l, -a, and -la for more detailed views.

pwd – Knowing Where You Are

  • Displays the full path of the current working directory.

cd – Moving Around

  • Supports both absolute and relative paths.
  • Use ~ to go to your home directory.
  • Use .. to move up one level.

find – Searching Efficiently

  • Powerful tool for locating files and folders.
  • Can use exact names, wildcards, case-insensitive searches, and filters by type.

6. Quick Reference Table

TaskCommand
List files in current directoryls
List with detailsls -l
Show hidden filesls -a
Print current directorypwd
Move into a directorycd <dir>
Move to home directorycd ~ or cd
Move up one levelcd ..
Move to absolute pathcd /path/to/dir
Find files by namefind . -name "filename"
Find files case-insensitivelyfind . -iname "filename"
Find directories onlyfind . -type d -name "dirname"

7. Final Thoughts

Mastering these navigation commands gives you control over your Linux environment. Whether you’re organizing files, troubleshooting issues, or writing scripts, understanding how to navigate the filesystem efficiently is crucial.


File and Directory Management Commands in Linux V2


1. Introduction

✅ Creating and deleting files and directories
✅ Copying and moving files and folders
✅ Managing file permissions (e.g., making a script executable)

These commands are essential for managing your system, organizing data, and working with scripts or configuration files.


2. Creating and Removing Files and Directories

A. mkdir – Make Directory

  • Purpose: Create new directories.
  • Syntax:
    1
    
    mkdir <directory_name>
    
  • Example:
    1
    
    mkdir test
    

Creates a directory named test in the current location.


B. rm – Remove Files or Directories

  • Purpose: Delete files or directories.
  • Syntax:

    1
    
    rm <filename>
    
  • Remove a file:

    1
    
    rm file1.txt
    
  • Remove a directory and its contents:
    1
    
    rm -r folder1
    

⚠️ Warning: Use rm -r carefully — it deletes everything inside the directory recursively.


C. rmdir – Remove Empty Directories

  • Purpose: Delete only empty directories.
  • Syntax:

    1
    
    rmdir <directory_name>
    
  • Example:
    1
    
    rmdir empty_folder
    

If the folder contains files or subdirectories, this command will fail — use rm -r instead.


D. touch – Create Empty Files or Update Timestamps

  • Purpose: Create an empty file or update the last-modified timestamp of an existing one.
  • Syntax:

    1
    
    touch <filename>
    
  • Create multiple files:

    1
    
    touch a.txt b.txt c.txt
    
  • Update timestamp on an existing file:
    1
    
    touch notes.txt
    

Useful for testing, scripting, or resetting file timestamps.


3. Copying and Moving Files and Directories

A. cp – Copy Files or Directories

  • Purpose: Copy files from one location to another.
  • Syntax:

    1
    
    cp <source> <destination>
    
  • Copy a single file:

    1
    
    cp notes.txt Documents/
    
  • Copy and rename a file:

    1
    
    cp notes.txt Documents/backup_notes.txt
    
  • Copy a directory and all contents:
    1
    
    cp -r Documents Docs_copy
    

The -r option enables recursive copying of directories and their contents.


B. mv – Move or Rename Files/Directories

  • Purpose: Move files/dirs or rename them.
  • Syntax:

    1
    
    mv <source> <destination>
    
  • Move a file into a directory:

    1
    
    mv my_script.sh Scripts/
    
  • Rename a file:

    1
    
    mv old_name.txt new_name.txt
    
  • Move multiple directories into another:
    1
    
    mv Scripts Notes Documents/
    

Unlike cp, mv does not duplicate the original — it moves or renames.


4. Managing File Permissions with chmod

A. chmod – Change File Mode (Permissions)

  • Purpose: Set or change read, write, and execute permissions on files.
  • Syntax:
    1
    
    chmod [options] <filename>
    

Common Options

OptionDescription
+rAdd read permission
+wAdd write permission
+xAdd execute permission
-rRemove read permission
-wRemove write permission
-xRemove execute permission

Example: Making a Script Executable

1
chmod +x my_script.sh
  • Before:

    1
    
    -rw-r--r-- 1 user group 0 Jan 1 12:00 my_script.sh
    
  • After:

    1
    
    -rwxr-xr-x 1 user group 0 Jan 1 12:00 my_script.sh
    

Now you can run the script:

1
./my_script.sh

5. Summary of Key Commands

TaskCommandNotes
Create a directorymkdir <dir> 
Remove a filerm <file> 
Remove a directory and contentsrm -r <dir>Use with caution
Remove an empty directoryrmdir <dir>Safe alternative
Create or update a filetouch <file>Sets current timestamp
Copy filescp <src> <dest>Use cp -r for directories
Move or rename filesmv <src> <dest>Also used for renaming
Change file permissionschmod +x <file>Makes a file executable

6. Final Tips

  • Always double-check before using rm -r — it’s irreversible.
  • Use rmdir when removing empty directories to avoid accidental deletion.
  • chmod is essential for running scripts or setting up secure environments.
  • Combine these commands with others like ls, cd, and find for powerful file operations.

📁 Exercise 1 - Navigating Files and Directories

In this exercise, you practiced essential Linux commands for navigating the filesystem, listing directory contents, and understanding file patterns.


📍 1.1 Get the Location of the Present Working Directory

Command:

1
pwd

What It Does:

  • Stands for “Print Working Directory”
  • Shows the full path to the directory where you are currently located

Example output:

1
/home/theia

📋 1.2 List Files and Directories in a Directory

Basic Usage:

1
ls
  • Lists all non-hidden files and directories in the current location
  • If the directory is empty, it returns nothing

View Contents of /bin (Binary Directory):

1
ls /bin
  • Displays system-level commands like ls, cd, cp, etc.
  • You can also look at specific files:
    1
    
    ls /bin/ls
    

Use Wildcards to Match Patterns:

🔍 All files starting with b:

1
ls /bin/b*

🔍 All files ending with r:

1
ls /bin/*r

💡 The * wildcard matches any number of characters


📄 Long Listing Format

See detailed file information:

1
ls -l

Includes:

  • File type and permissions
  • Number of links
  • Owner
  • Group
  • Size in bytes
  • Last modification date/time
  • File or directory name

Common ls Options:

OptionDescription
-aShow hidden files (those starting with .)
-dShow directories only
-hHuman-readable sizes (e.g., KB, MB)
-lLong listing format
-SSort by file size (largest first)
-tSort by modification time (newest first)
-rReverse sort order

Example: List all files (including hidden ones) in long format:

1
ls -la

Example: List all files in /etc with details:

1
ls -la /etc

This helps you explore system-wide configuration files.


✅ Summary Table

TaskCommand
Show current directorypwd
List files in current dirls
List files in /binls /bin
List files matching patternls /bin/b*, ls /bin/*r
Long list formatls -l
Show hidden filesls -a
Long list including hiddenls -la
Human-readable sizesls -lh

💡 Pro Tips

  • Combine options:
    1
    
    ls -lah   # Long list, human-readable, include hidden
    
  • Use tab completion to avoid typing full paths:
    1
    
    ls /bi<TAB>
    

    Becomes:

    1
    
    ls /bin/
    

📁 Exercise 2 - Creating Files and Directories

In this exercise, you practiced using Linux commands to create directories, navigate the filesystem, and create or update files using the touch command.


✅ Summary of Commands Covered

TaskCommand
Create a directorymkdir directory_name
Change working directorycd directory_name
Move up one directory levelcd ..
Go back to home directorycd
Create an empty file or update timestamptouch filename

🧾 Step-by-Step Breakdown

🔹 2.1 Create a Directory with mkdir

1
mkdir scripts
  • Creates a new directory named scripts in your current location

Verify it was created:

1
ls

You should see scripts listed.


🔹 2.2 Change Working Directory with cd

Move into the scripts directory:

1
cd scripts

Check your current location:

1
pwd

Return to your home directory:

1
cd

Move up one level (to the parent directory):

1
cd ..

This is useful when navigating through nested directories.


🔹 2.3 Create an Empty File with touch

Go back to your home directory:

1
cd

Create a new file called myfile.txt:

1
touch myfile.txt

Verify the file exists:

1
ls

If the file already exists, touch updates its last-modified timestamp:

1
touch myfile.txt

To check the updated timestamp:

1
date -r myfile.txt

This shows the last time the file was accessed or modified.


📋 Tips & Tricks

  • You can create multiple directories at once:
    1
    
    mkdir dir1 dir2 dir3
    
  • Make a nested directory structure easily:
    1
    
    mkdir -p project/files/data
    

    The -p option creates parent directories as needed.

  • Use tab completion for faster navigation:
    1
    
    cd scri<TAB>
    

    Completes to:

    1
    
    cd scripts
    

🧠 Why These Commands Matter

These basic tools are essential for managing your workspace in Linux:

  • mkdir: Organize your files by creating structured directories
  • cd: Navigate between folders quickly
  • touch: Create placeholder files or update timestamps for testing or scripting purposes

📁 Exercise 3 - Managing Files and Directories

In this exercise, you practiced essential Linux commands for searching, removing, moving, renaming, and copying files and directories — key skills for managing your system effectively.


✅ Summary of Commands Covered

TaskCommand
Search for filesfind
Remove (delete) filesrm, rm -i
Move or rename filesmv
Copy filescp

🔍 Step-by-Step Breakdown

🔹 3.1 Search for Files Using find

1
find /etc -name '*.txt'
  • Searches the /etc directory and its subdirectories for all .txt files.
  • -name specifies a filename pattern to match.
  • You may see “Permission denied” errors — these are normal due to access restrictions in lab environments.

🔹 3.2 Remove Files with rm

1
rm -i myfile.txt
  • The -i option prompts for confirmation before deletion:
    1
    
    rm: remove regular file 'myfile.txt'? y/n
    

After deletion, verify:

1
ls

⚠️ Warning: Deleted files cannot be recovered easily. Always double-check what you’re deleting.

Use without -i only when sure:

1
rm myfile.txt

Delete multiple files safely using confirmation:

1
rm -i *.txt

🔹 3.3 Move or Rename Files with mv

✏️ Rename a File:

1
mv users.txt user-info.txt

This renames users.txt to user-info.txt in the same directory.

Verify:

1
ls

📦 Move a File to Another Directory:

1
mv user-info.txt /tmp

Moves user-info.txt to the /tmp directory.

Verify:

1
2
ls
ls -l /tmp

💡 If the target file already exists, it will be overwritten silently — always be cautious!


🔹 3.4 Copy Files with cp

📄 Copy from One Location to Another:

1
cp /tmp/user-info.txt user-info.txt

This copies the file back to your current working directory.

Verify:

1
ls

📂 Copy Content of a System File:

1
cp /etc/passwd users.txt

This creates a new file users.txt containing the contents of /etc/passwd.

Check that the copy was successful:

1
2
ls
cat users.txt

You can also view the first few lines:

1
head users.txt

🧰 Pro Tips

  • Use wildcards to move or copy groups of files:
    1
    2
    
    cp *.txt /backup/
    mv *.log logs/
    
  • Combine cp with -r to copy entire directories:
    1
    
    cp -r myfolder/ backup/
    
  • Use rm -r carefully to delete directories and their contents:
    1
    
    rm -r myfolder
    

🧠 Why These Commands Matter

These tools give you full control over your filesystem:

  • find: Locate files across your system
  • rm: Clean up unwanted files
  • mv: Reorganize or rename files
  • cp: Duplicate or archive content

They form the foundation for automating tasks, writing scripts, and managing data efficiently.


📚 Practice Exercises – Managing Files and Directories

Here are the practice exercises with hints and solutions to help reinforce your understanding of Linux commands for navigating, creating, moving, copying, and deleting files and directories.


🔹 1. Display the contents of the /home directory

💡 Hint:

Use the ls command to list directory contents.

✅ Solution:

1
ls /home

This shows all user directories inside /home.


🔹 2. Ensure that you are in your home directory

💡 Hint:

Use the cd command without arguments to return to your home directory.

✅ Solution:

1
cd

You can verify using:

1
pwd

It should output something like:

1
/home/theia

🔹 3. Create a new directory called tmp and verify its creation

💡 Hint:

Use mkdir to create a directory and ls to confirm it exists.

✅ Solution:

1
2
mkdir tmp
ls

You should see tmp listed in the output.


🔹 4. Create a new, empty file named display.sh in the tmp directory, and verify its creation

💡 Hint:

Navigate into the tmp directory and use touch to create an empty file.

✅ Solution:

1
2
3
cd tmp
touch display.sh
ls

You should see display.sh listed.


🔹 5. Create a copy of display.sh, called report.sh, within the same directory

💡 Hint:

Use the cp command to make a copy of a file.

✅ Solution:

1
2
cp display.sh report.sh
ls

Now both display.sh and report.sh should be listed.


🔹 6. Move your copied file, report.sh, up one level in the directory tree to the parent directory. Verify your changes

💡 Hint:

Use mv to move the file and cd .. or ls -l ../ to check.

✅ Solution:

1
2
mv report.sh ..
ls ..

Or:

1
2
cd ..
ls

You should now see report.sh in the parent directory.


🔹 7. Delete the file display.sh

💡 Hint:

Use the rm command to delete a file. Consider using -i for confirmation (optional).

✅ Solution:

1
rm display.sh

To verify:

1
ls

The file display.sh should no longer appear.


🔹 8. List the files in /etc directory in the ascending order of their access time

💡 Hint:

Use the ls command with appropriate options to sort by access time.

✅ Solution:

1
ls -ltu /etc
  • -l = long listing format
  • -t = sort by modification time (but combined with -u, sorts by access time)
  • -u = use access time for sorting

This lists files sorted by when they were last accessed, newest first.


🔹 9. Copy the file /var/log/bootstrap.log to your current directory

💡 Hint:

Use the cp command to copy a file from its source path to the current directory.

✅ Solution:

1
cp /var/log/bootstrap.log .

To verify:

1
ls

You should see bootstrap.log in your current directory.


🧠 Bonus Tips

  • Use tab completion to avoid typing full paths:
    1
    
    cp /va<TAB>/lo<TAB>/boo<TAB> .
    
  • Combine ls options for better readability:
    1
    
    ls -lh
    
  • Use rm -i when unsure:
    1
    
    rm -i *.tmp
    

Great job completing these hands-on exercises! You’re now comfortable with basic file and directory management in Linux.


🔒 Security: Managing File Permissions and Ownership – Summary & Guide

In this reading, you learned how to manage file permissions and ownership in Linux — a crucial skill for protecting sensitive data and ensuring system security.


🎯 Learning Objectives Recap

By the end of this section, you should be able to:

  • Explain file ownership and permissions
  • View file and directory permissions
  • Make files private by changing permission settings

🔐 Why File Permissions Matter

Linux is a multi-user operating system, which means multiple people can access the same machine. Without proper permissions:

  • Anyone could read or modify your files
  • Malicious users might execute harmful scripts
  • Sensitive documents (like tax records or company IP) could be exposed

That’s why permissions and ownership allow fine-grained control over who can read, write, or execute files.


👤 Types of Owners in Linux

There are three categories of users that determine permission levels:

CategoryMeaning
user (u)The owner of the file (usually the creator)
group (g)A group of users assigned to the file
other (o)Everyone else with access to the system

Each category has its own set of permissions:

  • r = Read
  • w = Write
  • x = Execute

👀 Viewing File Permissions

Use:

1
ls -l filename

Example output:

1
-rw-r--r-- 1 theia users 25 Dec 22 17:47 my_new_file
  • -: Indicates it’s a regular file
  • rw-: User (owner) permissions
  • r--: Group permissions
  • r--: Others’ permissions

📘 For directories:

  • d at the start indicates it’s a directory
  • Permissions mean:
    • r: Can list contents (ls)
    • w: Can create or delete files inside
    • x: Can enter the directory (cd)

🔧 Changing File Permissions with chmod

You can change permissions using:

1
chmod [options] filename

Example: Make a file private

1
chmod go-r my_new_file

This removes read permission for group and others.

After running:

1
ls -l my_new_file

You’ll see:

1
-rw------- 1 theia users 24 Dec 22 18:49 my_new_file

Now only the owner can read and write to the file.

⚠️ Only the owner or root can change permissions on a file.


🛠️ Common chmod Syntax Options

CommandAction
chmod u+x file.shAdd execute permission for the user
chmod go-w file.txtRemove write permission for group and others
chmod 755 file.shSet numeric permissions (see below)

Numeric Permission Notation

Each permission level has a number value:

  • r = 4
  • w = 2
  • x = 1

Sum them to get numeric values:

PermissionValue
rwx7
rw-6
r-x5
r--4

Example:

1
chmod 600 secret.txt

Only the owner has read and write access.


📁 Executable Files and Shell Scripts

To make a shell script executable:

  1. Add a shebang line at the top:
    1
    
    #!/bin/bash
    
  2. Give it execute permission:
    1
    
    chmod +x script.sh
    
  3. Run it:
    1
    
    ./script.sh
    

This tells the OS that the file is a program written in Bash (or another interpreter).


✅ Summary Table

ConceptDescription
User (u)Owner of the file
Group (g)Group of users who share ownership
Other (o)All other users
Permissionsr = read, w = write, x = execute
View Permissionsls -l
Change Permissionschmod
Make Privatechmod go-rwx filename
Numeric Modechmod 600 filename
Directoriesr = list, w = modify contents, x = enter

💡 Pro Tips

  • Use chmod 700 folder to keep a directory private
  • Protect sensitive scripts with chmod 600 script.sh
  • Use id to see what groups you belong to
  • Use chown (as root) to change file ownership

You’re now equipped with the knowledge to secure your files, control access, and protect sensitive data in a Linux environment.


🛠️ Exercise 1 - Viewing and Modifying File Access Permissions

In this exercise, you learned how to view and modify file permissions in Linux using the ls -l and chmod commands. Understanding and managing permissions is essential for maintaining system security and controlling access to your files.


📁 1.1 View File Access Permissions

🔧 Steps:

  1. Navigate to your project directory:

    1
    
    cd /home/project
    
  2. Download the required file:

    1
    
    wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-LX0117EN-SkillsNetwork/labs/module%201/usdoi.txt
    
  3. View the file’s permissions:

    1
    
    ls -l usdoi.txt
    

✅ Sample Output:

1
-rw-r--r-- 1 theia theia 8121 May 31 16:45 usdoi.txt

📝 Breakdown of Permission String: -rw-r--r--

PartMeaning
-It’s a regular file (use d for directories)
rw-User (owner) has read and write
r--Group has only read
r--Others have only read

🔐 1.2 Change File Access Permissions with chmod

The chmod command allows you to modify who can read, write, or execute a file.

🔧 Syntax:

1
chmod [who][operator][permission] filename
ComponentValues
whou = user, g = group, o = others, a = all
operator+ = add, - = remove, = = set exactly
permissionr = read, w = write, x = execute

🔍 Example Commands

❌ Remove read permission for all users:

1
chmod -r usdoi.txt

Verify:

1
ls -l usdoi.txt

You’ll see something like:

1
--w------- 1 theia theia 8121 May 31 16:45 usdoi.txt

✅ Restore read permission for all users:

1
chmod +r usdoi.txt

Verify:

1
ls -l usdoi.txt

You should return to:

1
-rw-r--r-- 1 theia theia 8121 May 31 16:45 usdoi.txt

🚫 Remove read permission only for “other”:

1
chmod o-r usdoi.txt

Verify:

1
ls -l usdoi.txt

Now output will be:

1
-rw-r----- 1 theia theia 8121 May 31 16:45 usdoi.txt

Only user and group can read the file now.


🧠 Summary Table

TaskCommand
View file permissionsls -l filename
Remove read from allchmod -r filename
Add read to allchmod +r filename
Remove read from otherschmod o-r filename
Add execute to userchmod u+x filename
Set exact permissions (e.g., rw——-)chmod 600 filename

💡 Pro Tips

  • Use numeric mode for precision:
    1
    2
    
    chmod 644 usdoi.txt    # rw-r--r--
    chmod 700 secret.sh     # rwx------
    
  • Always double-check permissions on sensitive files:
    1
    
    ls -l secret.txt
    
  • Only the file owner or root can change permissions.

Great job completing this exercise! You’re now able to view and manage file permissions — an essential skill for working securely in Linux.


📁 Exercise 2 - Understanding Directory Access Permissions

In this exercise, you explored how directory permissions in Linux work and how they differ from file permissions. You learned that execute (x) permission is required to enter a directory, and write (w) permission is needed to create or delete files inside it.


🔍 Summary of What You Learned

PermissionMeaning for Directories
r (read)Allows listing contents with ls
w (write)Allows adding/removing files
x (execute)Allows entering the directory using cd

⚠️ Even if you have write access, you can’t modify contents without execute permission!


🧾 Step-by-Step Breakdown

🔹 2.1 View Default Directory Access Permissions

Create a new directory:

1
2
cd /home/project
mkdir test

Check its permissions:

1
ls -l

Output example:

1
drwxr-sr-x 2 theia users 4096 May 15 14:06 test
  • d: Indicates it’s a directory
  • rwx: Owner (user) has read, write, and execute
  • r-s: Group has read and setgid (special permission)
  • r-x: Others have read and execute

You verified that:

  • You could enter the directory
  • You could create a subdirectory inside it

Commands used:

1
2
3
cd test
mkdir test2
cd ..

💡 The s in group permissions is a special flag called setgid — it ensures new files inherit the group of the parent directory.


🔹 2.2 Remove User Execute Permissions

Remove execute permission for owner:

1
chmod u-x test

Now try:

1
cd test

❌ Output:

1
bash: cd: test: Permission denied

Even though you still have read and write permissions, you can’t enter the directory without execute permission.

Try listing contents:

1
ls -l test

✅ Works — because you still have read permission.

Try creating a subdirectory:

1
mkdir test/test3

❌ Fails — because write requires execute.


🔁 Restore Permissions

Add back execute:

1
chmod u+x test

Remove write:

1
chmod u-w test

Check current permissions:

1
ls -l

Now try:

1
2
cd test
mkdir test_again

❌ Output:

1
mkdir: cannot create directory ‘test_again’: Permission denied

You can enter the directory (thanks to x), but you can’t modify it (because you don’t have w).


🧠 Key Takeaways

ScenarioRequired Permission
List contents (ls)r
Enter directory (cd)x
Create/delete filesw + x
Rename or move filesw + x in both source and target directories

🧩 Think of x as a key to open a door — you need it before you can do anything useful inside a directory.


✅ Summary Table

CommandEffect
ls -lShow directory permissions
chmod u+x dirAllow user to enter directory
chmod u-w dirPrevent user from modifying directory contents
cd dirRequires execute (x) permission
ls dirRequires read (r) permission
mkdir dir/subdirRequires write (w) + execute (x) permissions on dir

You’ve now mastered how directory permissions control access differently than file permissions — an essential concept for securing and managing your Linux system effectively.


🛠️ Practice Exercises – File and Directory Permissions

Here are the practice exercises with hints and full solutions to help you reinforce your understanding of Linux file and directory permissions.


🔹 1. List the permissions set for the file usdoi.txt

💡 Hint:

Use the ls -l command to view file permissions.

✅ Solution:

1
ls -l usdoi.txt

Example output:

1
-rw-r--r-- 1 theia users 8121 May 31 16:45 usdoi.txt

This shows that:

  • The owner has read and write
  • The group has read
  • Others have read

🔹 2. Revoke the write permission on usdoi.txt for the user, and verify

💡 Hint:

Use chmod u-w to remove write access for the user.

✅ Solution:

1
2
chmod u-w usdoi.txt
ls -l usdoi.txt

New output:

1
-r--r--r-- 1 theia users 8121 May 31 16:45 usdoi.txt

The user (owner) no longer has write permission.


🔹 3. What happens if you try to delete usdoi.txt after revoking write permissions?

💡 Hint:

You don’t need write permission to delete a file — but you do need write permission in the directory it’s in.

✅ Solution:

Try deleting:

1
rm usdoi.txt

✅ It works!
Even though you removed write permission on the file, you can still delete it because:

  • You own the file
  • You have write permission in the directory

🔍 Note: If you didn’t own the file, you could still delete it as long as:

  • You had write permission in the directory
  • You didn’t own the file

🔹 4. Create a new directory called tmp_dir in your home directory

💡 Hint:

Use the mkdir command.

✅ Solution:

1
2
cd ~
mkdir tmp_dir

Now you have a new directory in your home folder.


🔹 5. View the permissions of the newly created directory tmp_dir

💡 Hint:

Use ls -l again to see directory permissions.

✅ Solution:

1
ls -l

Output will include something like:

1
drwxr-xr-x 2 theia users 4096 Apr 5 10:00 tmp_dir

This means:

  • User: read, write, execute
  • Group: read, execute
  • Others: read, execute

🔹 6. Revoke the user write permission for tmp_dir

💡 Hint:

Use chmod u-w on the directory.

✅ Solution:

1
2
chmod u-w tmp_dir
ls -l

New output:

1
dr-xr-xr-x 2 theia users 4096 Apr 5 10:00 tmp_dir

User now only has execute and read — not write.


🔹 7. Check whether you can create a subdirectory sub_dir inside tmp_dir

💡 Hint:

Use mkdir to attempt creating a subdirectory.

✅ Solution 1:

1
mkdir tmp_dir/sub_dir

❌ Output:

1
mkdir: cannot create directory ‘tmp_dir/sub_dir’: Permission denied

✅ Solution 2 Explanation:

Even though you have execute (x) permission on tmp_dir, which allows you to enter the directory, you don’t have write (w) permission — so you can’t modify its contents (like creating or deleting files or directories).


🧠 Summary Table

TaskCommand
View file permissionsls -l filename
Remove write from userchmod u-w filename
Delete a filerm filename (requires write in directory)
Create a directorymkdir dirname
View directory permissionsls -l
Remove write from directorychmod u-w dirname
Create subdirectorymkdir dirname/subdir (fails without w + x)

Great job working through these permission exercises! You’re building strong skills in managing file access control, user permissions, and directory behavior in Linux.


Viewing File Content in Linux


1. Introduction

✅ Displaying full or partial contents of a file
✅ Navigating large files page-by-page
✅ Counting lines, words, and characters

These tools are especially useful when working with log files, scripts, configuration files, and other text-based data.


2. Commands to View File Contents

A. cat – Concatenate and Print Entire File

  • Purpose: Print the entire contents of a file to the terminal.
  • Syntax:

    1
    
    cat <filename>
    
  • Example:
    1
    
    cat numbers.txt
    

Outputs all lines from the file numbers.txt, from line 0 to 99 in this example.

  • Use Cases:
    • Small files that fit on one screen.
    • Combining multiple files into one (e.g., cat file1 file2 > combined.txt)

⚠️ Not ideal for long files — output may scroll off-screen quickly.


B. more – View File One Page at a Time

  • Purpose: Scroll through a file page by page.
  • Syntax:

    1
    
    more <filename>
    
  • Example:

    1
    
    more numbers.txt
    
  • Navigation Keys:
    • Press Spacebar to go to the next page.
    • Press Enter to scroll one line at a time.
    • Press q to quit and return to the command prompt.

Ideal for reading long files without overwhelming the terminal.


C. head – View First Few Lines of a File

  • Purpose: Show the beginning portion of a file.
  • Syntax:

    1
    
    head <filename>
    
  • Default Behavior:

    • Displays first 10 lines of the file.
  • Example:

    1
    
    head numbers.txt
    

    Shows lines 0–9

  • Custom Line Count:
    1
    
    head -n 3 numbers.txt
    

    Shows first 3 lines: 0, 1, 2

Useful for checking headers, logs, or sample data quickly.


D. tail – View Last Few Lines of a File

  • Purpose: Show the end portion of a file.
  • Syntax:

    1
    
    tail <filename>
    
  • Default Behavior:

    • Displays last 10 lines of the file.
  • Example:

    1
    
    tail numbers.txt
    

    Shows lines 90–99

  • Custom Line Count:

    1
    
    tail -n 3 numbers.txt
    

    Shows last 3 lines: 97, 98, 99

  • Real-world Use:
    • Monitoring log files in real-time using:
      1
      
      tail -f /var/log/syslog
      

3. Command to Analyze File Content: wc

A. wc – Word Count

  • Purpose: Count lines, words, and bytes (characters) in a file.
  • Syntax:

    1
    
    wc <filename>
    
  • Example:
    1
    
    wc pets.txt
    

Output:

1
7 7 28 pets.txt
  • Meaning:
    • 7 lines
    • 7 words
    • 28 characters (including newline characters)

The character count is higher than expected because wc counts newline characters (\n) as well.


B. Options for Specific Counts

OptionDescriptionExample
-lCount only lineswc -l pets.txt7 pets.txt
-wCount only wordswc -w pets.txt7 pets.txt
-cCount only characters (including newlines)wc -c pets.txt28 pets.txt

These options let you extract specific metrics without extra parsing.


4. Summary Table

CommandPurposeExampleOutput Sample
catView entire filecat numbers.txtAll lines from 0 to 99
moreView file page-by-pagemore numbers.txtScrollable output
headView first 10 lineshead numbers.txtLines 0–9
head -n XView first X lineshead -n 3 numbers.txtLines 0–2
tailView last 10 linestail numbers.txtLines 90–99
tail -n XView last X linestail -n 3 numbers.txtLines 97–99
wcCount lines, words, charswc pets.txt7 7 28 pets.txt
wc -lCount only lineswc -l pets.txt7 pets.txt
wc -wCount only wordswc -w pets.txt7 pets.txt
wc -cCount only characterswc -c pets.txt28 pets.txt

5. Final Tips

  • Use cat for small files or quick inspection.
  • Use more for navigating larger files interactively.
  • Use head and tail to inspect the start or end of a file efficiently.
  • Use wc to get statistical information about your file’s content.
  • Combine these commands with pipes (|) for advanced analysis:
    1
    
    cat pets.txt | wc -l
    

Useful Commands for Wrangling Text Files in Linux


1. Introduction

✅ Sorting lines alphabetically
✅ Removing duplicates
✅ Searching for patterns
✅ Extracting parts of lines
✅ Merging lines from multiple files

These tools are essential for working with log files, CSV data, configuration files, and more.


2. Sorting File Content: sort Command

Purpose

Sort the lines of a file alphabetically or numerically.

Basic Syntax

1
sort <filename>

Examples

  • Sort file alphabetically:

    1
    
    sort pets.txt
    

    Output:

    1
    2
    3
    4
    5
    6
    7
    
    cat
    cat
    cat
    cat
    cat
    dog
    dog
    
  • Sort in reverse order:

    1
    
    sort -r pets.txt
    

    Output:

    1
    2
    3
    4
    5
    6
    7
    
    dog
    dog
    cat
    cat
    cat
    cat
    cat
    

Tip: Use sort -n for numeric sorting if your file contains numbers.


3. Removing Duplicate Lines: uniq Command

Purpose

Filter out consecutive duplicate lines in a file.

Basic Syntax

1
uniq <filename>

Example

Given this content in pets.txt:

1
2
3
4
cat
dog
dog
cat

Running:

1
uniq pets.txt

Output:

1
2
3
cat
dog
cat

Note: uniq only removes duplicates that appear one after another. To remove all duplicates regardless of position, first use sort, then uniq:

1
sort pets.txt | uniq

4. Searching for Patterns: grep Command

Purpose

Search for lines containing a specific pattern (e.g., word, phrase, regular expression).

Basic Syntax

1
grep "pattern" <filename>

Examples

  • Search for lines containing “ch”:

    1
    
    grep "ch" people.txt
    

    Output:

    1
    2
    
    Dennis Ritchie
    Erwin Schrodinger
    
  • Case-insensitive search:

    1
    
    grep -i "ch" people.txt
    

    Output:

    1
    2
    3
    
    Charles Babbage
    Dennis Ritchie
    Erwin Schrodinger
    

grep supports regular expressions for advanced searches:

1
grep "^C" people.txt   # Find names starting with 'C'

5. Extracting Parts of Lines: cut Command

Purpose

Extract specific characters or fields from each line of a file.

Basic Syntax

1
cut [options] <filename>

A. Character-Based Extraction

  • Extract characters 2 through 9:

    1
    
    cut -c2-9 people.txt
    

    Example Input:

    1
    2
    
    Alan Turing
    Charles Babbage
    

    Output:

    1
    2
    
    lan Turin
    harles B
    

B. Field-Based Extraction

Use -d to define a delimiter and -f to select a field.

  • Extract last names (second field) from a space-separated file:

    1
    
    cut -d' ' -f2 people.txt
    

    Output:

    1
    2
    
    Turing
    Babbage
    

You can also extract multiple fields:

1
cut -d',' -f1,3 csvfile.csv

6. Merging Lines from Multiple Files: paste Command

Purpose

Combine lines from multiple files side by side, similar to columns in a table.

Basic Syntax

1
paste <file1> <file2> <file3>

Example

You have three files:

  • first.txt:

    1
    2
    3
    
    Alan
    Charles
    Dennis
    
  • last.txt:

    1
    2
    3
    
    Turing
    Babbage
    Ritchie
    
  • yob.txt:

    1
    2
    3
    
    1912
    1791
    1941
    

Run:

1
paste first.txt last.txt yob.txt

Output:

1
2
3
Alan    Turing    1912
Charles Babbage   1791
Dennis  Ritchie   1941

By default, paste uses tab as the delimiter.

Custom Delimiter

To use a comma instead:

1
paste -d',' first.txt last.txt yob.txt

Output:

1
2
3
Alan,Turing,1912
Charles,Babbage,1791
Dennis,Ritchie,1941

7. Summary Table

CommandPurposeExample
sortSort lines alphabetically/numericallysort pets.txt
sort -rReverse sortsort -r pets.txt
uniqRemove consecutive duplicate linesuniq pets.txt
grep "pattern"Print lines matching a patterngrep "ch" people.txt
grep -iCase-insensitive searchgrep -i "ch" people.txt
cut -cX-YExtract characters from X to Ycut -c2-9 people.txt
cut -d' ' -f2Extract second field using space as delimitercut -d' ' -f2 people.txt
pasteMerge lines from multiple filespaste first.txt last.txt yob.txt
paste -d','Merge with custom delimiterpaste -d',' first.txt last.txt yob.txt

8. Final Tips

  • Combine commands using pipes (|) for powerful workflows:
    1
    
    sort pets.txt | uniq
    
  • Use grep with wildcards or regex for flexible searching.
  • cut is great for parsing structured text like CSV or logs.
  • paste helps you create reports or combine related datasets.

📄 Exercise 1 - Viewing File Contents with cat, more, and less

In this exercise, you learned how to view and navigate file contents in the terminal using three essential Linux commands:

  • cat – for quick viewing and concatenation
  • more – for basic scrolling through a file
  • less – for advanced navigation (forward and backward)

These tools are especially useful when working with shell scripts, logs, configuration files, and other text-based data.


🔧 Step-by-Step Breakdown

🏁 Start by navigating to your home directory:

1
cd ~

🔍 Check what files exist:

1
ls

You should see a file named entrypoint.sh. .sh is the extension used for shell scripts, which are executable text files that contain Bash commands.


✅ 1.1 View File Content with cat

Command:

1
cat entrypoint.sh

What It Does:

  • Displays the entire contents of the file at once
  • Stops at the end and returns to the command prompt

⚠️ If the file is longer than your terminal window, you’ll only see the last part — it scrolls past too quickly to read everything.

💡 Use Cases:

  • Quick inspection of small files
  • Concatenating multiple files:
    1
    
    cat file1.txt file2.txt > combined.txt
    

✅ 1.2 View File Content with more

Command:

1
more entrypoint.sh

What It Does:

  • Shows one screen of text at a time
  • Press Spacebar to go to the next page
  • Type q to quit

🔍 Useful Info:

The first line:

1
#!/bin/bash

is called a shebang — it tells the system to use /bin/bash to interpret the script.

📌 You’ll learn more about writing shell scripts later in the course.

💡 Use Cases:

  • Reading medium-sized files
  • Viewing logs or configuration files from the command line

✅ 1.3 Scroll Through File Content with less

Command:

1
less entrypoint.sh

What It Does:

  • Displays one screen of content
  • Allows scrolling forward and backward
    • ↑ / ↓ keys: scroll line by line
    • Page Up / Page Down: scroll page by page
  • Press q to exit

🆕 Why less Is Better Than more:

  • You can move up and down, not just down
  • You can search inside the file with /search_term
  • It doesn’t automatically quit at the end

📋 Summary Table

CommandScrolling DirectionInteractiveExits AutomaticallyBest For
catAll at onceNoYesSmall files, scripting
moreForward onlyLimitedYesPaging through files
lessBoth forward/backwardFullNoDetailed inspection, logs

🧠 Tips & Tricks

  • Combine with pipes to view output:
    1
    
    ls -l /etc | less
    
  • Search inside less:
    • Type /pattern then press Enter
    • Press n to find next match
  • Exit early: always press q to quit

Great job completing this exercise! You now know how to choose the best tool depending on the size and complexity of the file you’re viewing.


📄 Exercise 2 - Viewing Text File Contents

Using head and tail to View File Content

In this exercise, you learned how to inspect specific parts of a text file using two powerful Linux commands:

  • head – to view the beginning of a file
  • tail – to view the end of a file

These tools are especially useful for:

  • Reading large log files
  • Monitoring real-time updates (with tail -f)
  • Extracting headers or footers from data files

🧾 Step-by-Step Instructions

🔧 Download and Navigate to Your Project Directory:

1
2
cd /home/project
wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DB0250EN-SkillsNetwork/labs/Bash%20Scripting/usdoi.txt

Verify the file was downloaded:

1
ls

You should see usdoi.txt listed.


✅ 2.1 Display the First N Lines with head

Show the first 10 lines (default):

1
head usdoi.txt

This is helpful when viewing large documents like logs or configuration files where the most important info may be at the top.

Show only the first 3 lines:

1
head -3 usdoi.txt

Example output:

1
When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another...

💡 Use head to quickly preview files without opening the entire contents.


✅ 2.2 Display the Last N Lines with tail

Show the last 10 lines (default):

1
tail usdoi.txt

Useful for checking the end of logs or files where recent changes are appended.

Show only the last 2 lines:

1
tail -2 usdoi.txt

Example output:

1
...and that government of the people, by the people, for the people shall not perish from the earth.

🚀 Pro Tip: Use tail -f to follow a log file in real time:

1
tail -f /var/log/syslog

📋 Summary Table

CommandDescriptionExample
headDisplays the first 10 lines of a filehead filename
head -NDisplays the first N lineshead -3 filename
tailDisplays the last 10 lines of a filetail filename
tail -NDisplays the last N linestail -2 filename
tail -fFollows the end of a file in real timetail -f /var/log/syslog

🧠 Why These Commands Matter

  • Efficiency: Avoid opening huge files just to check the start or end.
  • Monitoring: Track live updates in log files using tail -f.
  • Automation: Use in scripts to extract key information from files.

Great work! You now know how to quickly inspect large text files, which is essential for system administration, scripting, and data analysis.


📊 Exercise 3 - Getting Basic Text File Stats with wc

In this exercise, you learned how to use the wc (word count) command to get basic statistics about a text file — including the number of lines, words, and characters.

This is especially useful when:

  • You’re analyzing large text files
  • You need to verify file contents before processing
  • You’re writing scripts that depend on file size or structure

🔧 Step-by-Step Breakdown

Start by navigating to your project directory and using wc:

1
2
cd /home/project
wc usdoi.txt

Example Output:

1
     21     268    1654 usdoi.txt

The output shows:

  1. Number of lines
  2. Number of words
  3. Number of characters
  4. File name

✅ View Specific Stats

🔢 Count Lines Only:

1
wc -l usdoi.txt

Useful for checking how many entries are in a list or log file.


📝 Count Words Only:

1
wc -w usdoi.txt

Great for content analysis or verifying document length.


🔤 Count Characters Only:

1
wc -c usdoi.txt

Tells you the total byte size of the file — useful for storage and transmission planning.

💡 Note: wc -c counts bytes, not just visible characters — so whitespace and punctuation are included.


📋 Summary Table

CommandOutput
wc filenameLines, words, characters
wc -l filenameNumber of lines
wc -w filenameNumber of words
wc -c filenameNumber of bytes (characters)

🧠 Why wc Is Useful

  • Automation: Use in scripts to validate input data size
  • Analysis: Get quick stats without opening the file
  • Debugging: Check if a file has expected content structure

You’re doing great! With these tools, you can now analyze text files efficiently and extract meaningful insights from their content.

🧹 Exercise 4 - Basic Text Wrangling: Sorting Lines and Dropping Duplicates

In this exercise, you learned how to clean up and organize text data using two powerful Linux utilities:

  • sort – to alphabetically or numerically sort lines
  • uniq – to remove consecutive duplicate lines

These tools are essential for:

  • Data cleaning
  • Log file analysis
  • Preparing input for scripts
  • Removing redundant output

🔧 Step-by-Step Breakdown

✅ 4.1 Sort Lines Alphanumerically with sort

Display the lines of usdoi.txt sorted alphanumerically:

1
sort usdoi.txt

This command rearranges all the lines in alphabetical order (A–Z), making it easier to scan or analyze content.

Sort in reverse order (Z–A):

1
sort -r usdoi.txt

💡 This is useful when you want to see the “end” of an alphabetized list first — like viewing the latest entries in a log.


✅ 4.2 Remove Consecutive Duplicate Lines with uniq

First, download a new file:

1
wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-LX0117EN-SkillsNetwork/labs/module%201/zoo.txt

View the contents:

1
cat zoo.txt

You’ll notice some repeated animal names, especially zebra.

Remove consecutive duplicates:

1
uniq zoo.txt

This removes only lines that appear one after another, so:

1
2
3
4
5
zebra
zebra
lion
tiger
zebra

Becomes:

1
2
3
4
zebra
lion
tiger
zebra

Only the first two zebras are removed as duplicates — the last one stays because it’s not directly after another zebra.


📋 Summary Table

CommandDescription
sort filenameSort lines alphabetically
sort -r filenameSort lines in reverse order
uniq filenameRemove consecutive duplicate lines
sort file | uniqSort then remove all duplicates (if grouped)

🧠 Pro Tips

  • To remove all duplicate lines regardless of order:

    1
    
    sort zoo.txt | uniq
    
  • Count how many times each line appears:

    1
    
    sort zoo.txt | uniq -c
    
  • Show only lines that appear more than once:

    1
    
    sort zoo.txt | uniq -d
    
  • Show only lines that appear exactly once:

    1
    
    sort zoo.txt | uniq -u
    

🛠️ Real-World Use Cases

TaskCommand
Clean up a messy list of emailssort emails.txt | uniq > clean_emails.txt
Count unique IP addresses in logscut -d' ' -f1 access.log | sort | uniq -c
Find most frequently visited URLsawk '{print $7}' access.log | sort | uniq -c | sort -nr | head -n 10

Great job mastering these basic but powerful text processing tools! You’re now equipped to organize, clean, and analyze textual data efficiently in Linux.

🧩 Exercise 5 - Basic Text Wrangling: Extracting Lines and Fields

In this exercise, you learned how to filter lines using patterns with grep and extract specific parts of text using the cut command.

These tools are essential for:

  • Searching through logs
  • Filtering data
  • Processing structured files like CSVs
  • Automating repetitive tasks in scripts

🔍 Step-by-Step Breakdown

✅ 5.1 Extract Lines Matching a Pattern with grep

1
grep people usdoi.txt

This shows only the lines where the word people appears.

1
grep -n people usdoi.txt

Count how many lines contain the pattern:

1
grep -c people usdoi.txt

Ignore case (match both “People” and “people”):

1
grep -i people usdoi.txt

Show lines that do not contain the pattern:

1
grep -v login /etc/passwd

Useful for filtering out system-generated accounts from /etc/passwd.

Match only whole words:

1
grep -w people usdoi.txt

Prevents partial matches like peoples or unpeople.


✅ 5.2 Extract Fields from Lines Using cut

View first two characters of each line in zoo.txt:

1
cut -c -2 zoo.txt

View text starting from the second character:

1
cut -c 2- zoo.txt

These options extract by character position, useful for fixed-width formats.


📥 Work with Delimited Files (e.g., CSV)

Download and view the file:

1
2
wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-LX0117EN-SkillsNetwork/labs/v4_new_content/labs/names_and_numbers.csv
cat names_and_numbers.csv

Sample content:

1
2
3
4
Name,Phone
Alice,555-1234
Bob,555-5678
Charlie,555-9012

Extract just the phone numbers (second field):

1
cut -d "," -f2 names_and_numbers.csv
  • -d "," tells cut to split on commas
  • -f2 tells it to return the second field

Output:

1
2
3
4
Phone
555-1234
555-5678
555-9012

You can also extract multiple fields:

1
cut -d "," -f1,3 names_and_numbers.csv

Returns fields 1 and 3 — useful when skipping unnecessary columns.


📋 Summary Table

TaskCommand
Find lines containing a wordgrep pattern file
Show line numbersgrep -n pattern file
Count matchesgrep -c pattern file
Case-insensitive searchgrep -i pattern file
Invert match (not containing)grep -v pattern file
Match whole word onlygrep -w pattern file
Extract characters by positioncut -c START-END file
Extract fields by delimitercut -d "DELIM" -f FIELD_NUMBERS file

🧠 Why These Tools Matter

ToolUse Case
grepSearch, filter, and count patterns in text
cutExtract specific parts of text based on position or delimiters

They’re often used together in pipelines:

1
grep "New York" contacts.csv | cut -d "," -f2

This finds all entries for New York and extracts their phone numbers.


🧩 Exercise 6 - Basic Text Wrangling: Merging Lines as Fields

In this exercise, you learned how to use the paste command to merge lines from multiple files side-by-side, like combining columns in a spreadsheet.

This is especially useful when:

  • You’re working with related data stored in separate files
  • You want to align rows for analysis or reporting
  • You need to build structured output from flat files

🔧 Step-by-Step Breakdown

✅ Download an additional file:

1
wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-LX0117EN-SkillsNetwork/labs/module%201/zoo_ages.txt

You already have zoo.txt, which contains animal names. Now zoo_ages.txt has corresponding age data.

✅ Merge both files line-by-line using paste:

1
paste zoo.txt zoo_ages.txt

By default, paste uses a Tab (\t) character to separate merged fields.

Example Output:

1
2
3
4
lion	5
tiger	4
zebra	6
elephant	10

This makes it easy to combine related data into a single view.


✅ Change the Delimiter to Comma (,) for CSV-like Output:

1
paste -d "," zoo.txt zoo_ages.txt

Now the output looks like:

1
2
3
4
lion,5
tiger,4
zebra,6
elephant,10

💡 This is useful for creating CSV files or preparing data for scripts and databases.

You can also use other delimiters like space, colon, or semicolon:

1
2
paste -d " " zoo.txt zoo_ages.txt   # Space
paste -d ":" zoo.txt zoo_ages.txt  # Colon

📋 Summary Table

CommandDescription
paste file1 file2Merge two files line-by-line using Tab delimiter
paste -d "," file1 file2Merge using comma as delimiter
paste -s file.txtPaste all lines of a file into one line
paste -d ":" file1 file2 > merged.csvSave merged output to a new file

🧠 Why paste Is Useful

Use CaseExample
Combine logs from different sourcespaste access.log user_agents.log
Build CSV files from parallel datapaste -d "," names.csv ages.csv > people.csv
Align configuration valuesMatch hostnames with IPs
Create input for scriptsGenerate formatted input for another tool

🛠️ Try It Out – Real-World Examples

Combine Names and Ages into One File:

1
paste -d "," zoo.txt zoo_ages.txt > animals.csv

Creates a new file animals.csv that’s ready for import into Excel or a database.

Merge Multiple Files:

1
paste names.txt emails.txt phones.txt

Merges three files — one per column — ideal for building contact lists.


🛠️ Practice Exercises – Text Processing in Linux

These hands-on exercises help reinforce your knowledge of file inspection, searching, and text manipulation using essential Linux commands like wc, grep, head, tail, cut, and more.


🔧 Before You Begin

Make sure you’re in your home directory:

1
2
cd ~
pwd

📝 Practice Exercise Solutions

1. Display the number of lines in /etc/passwd

✅ Solution:

1
wc -l /etc/passwd

This shows how many user accounts exist on the system (each line in /etc/passwd represents a user).


2. Display lines containing “not installed” in /var/log/bootstrap.log

✅ Solution:

1
grep "not installed" /var/log/bootstrap.log

This helps identify packages or services that failed to install during system boot.


3. Find websites with “org” in them from top-sites.txt

First, download the file:

1
wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DB0250EN-SkillsNetwork/labs/Bash%20Scripting/top-sites.txt

✅ Use grep to search for “org”:

1
grep "org" top-sites.txt

This lists all URLs or domains that include “org”.

🔄 Alternative solution using case-insensitive match:

1
grep -i "org" top-sites.txt

Useful if you want to catch both “org” and “ORG”, etc.


4. Print the first 7 lines of top-sites.txt

✅ Solution:

1
head -7 top-sites.txt

This gives a quick preview of the top sites list.


5. Print the last 7 lines of top-sites.txt

✅ Solution:

1
tail -7 top-sites.txt

Handy when you’re interested in newer entries at the end of a list or log file.


6. Print the first three characters of each line from top-sites.txt

✅ Solution:

1
cut -c -3 top-sites.txt

This extracts only the first 3 characters from every line — useful for fixed-width data or codes.


7. Extract and view only the names from names_and_numbers.csv

Assuming the file already exists:

1
cat names_and_numbers.csv

✅ Solution:

1
cut -d "," -f1 names_and_numbers.csv
  • -d "," tells cut to use comma as the delimiter
  • -f1 selects the first field — which is the name column

You can also skip the header if needed:

1
tail -n +2 names_and_numbers.csv | cut -d "," -f1

📋 Summary Table: Commands Used

TaskCommand
Count lines in a filewc -l filename
Search for text in a filegrep "pattern" filename
Print first N lineshead -N filename
Print last N linestail -N filename
Extract character rangecut -c START-END filename
Extract by delimitercut -d "DELIM" -f FIELD_NUMBERS filename

🧠 Why These Skills Matter

You now have the tools to:

  • Analyze logs
  • Filter and extract data
  • Process CSVs and structured files
  • Build automation pipelines using command-line tools

These are foundational skills for scripting, system administration, and data analysis in Linux.


Exercise 7 – Combining Commands with Pipes and Redirection

In this exercise, you’ll learn how to:

  • Combine multiple Linux commands using pipes (|)
  • Redirect command output to files using > and >>
  • Use input redirection with <
  • Build powerful command pipelines that process data step-by-step

These skills are essential for:

  • Automating repetitive tasks
  • Processing logs or large datasets
  • Writing shell scripts
  • Becoming a more efficient Linux user

🔧 What is Piping?

The pipe operator (|) takes the output of one command and feeds it as input to another.

This lets you chain together small tools to build complex operations.

Example:

1
ls -l | grep "Jan" | wc -l

This pipeline:

  1. Lists all files (ls -l)
  2. Filters only those modified in January (grep "Jan")
  3. Counts them (wc -l)

📥 Input and Output Redirection

You can also control where input comes from and where output goes using:

OperatorPurposeExample
>Redirect output to a file (overwrites)ls > files.txt
>>Append output to a fileecho "New line" >> files.txt
<Redirect input from a filesort < names.txt

Example:

1
grep "error" /var/log/syslog > errors.txt

Saves all lines containing “error” from the system log into a new file.


💡 Hands-On Practice

Let’s go through some guided examples.

✅ 1. Chain grep, sort, and uniq to Analyze Log Data

Find unique IP addresses in an access log:

1
grep "Failed password" /var/log/auth.log | awk '{print $9}' | sort | uniq
  • grep: filters for failed login attempts
  • awk: extracts the IP address field
  • sort: prepares for deduplication
  • uniq: removes duplicates

You’ll learn about awk soon — it’s a powerful text processing tool.


✅ 2. Save Command Output to a File

Save your list of running processes to a file:

1
ps -e > process_list.txt

Now view it:

1
cat process_list.txt

✅ 3. Count Words in a File Using a Pipeline

Count how many times each word appears in usdoi.txt:

1
tr ' ' '\n' < usdoi.txt | sort | uniq -c | sort -nr

What this does:

  • tr ' ' '\n': replaces spaces with newlines (puts each word on its own line)
  • sort: sorts words alphabetically
  • uniq -c: counts occurrences
  • sort -nr: sorts numerically in reverse order

✅ 4. View Command Output One Page at a Time

Sometimes output fills your screen. Use less to page through it:

1
history | less

Use ↑ ↓ keys to scroll, and press q to quit.


📋 Summary Table: Pipes & Redirection

TaskCommand
Pipe output to another commandcommand1 | command2
Redirect output to a filecommand > file.txt
Append output to a filecommand >> file.txt
Read input from a filecommand < file.txt

🧠 Why This Matters

With pipes and redirection, you can:

  • Automate workflows with simple, reusable components
  • Process large amounts of data efficiently
  • Build custom scripts that do exactly what you need

🎉 Summary – Great Job Completing the Lab!

You’ve just gained hands-on experience with some of the most essential Linux text processing and file inspection tools. These skills are fundamental for working efficiently in a Linux environment — whether you’re managing logs, analyzing data, or writing shell scripts.


🔍 What You Learned

Here’s a quick recap of the core commands and skills you practiced:

SkillCommand(s) Used
Viewing file contentscat, more, less
Inspecting start/end of fileshead, tail
Count lines, words, characterswc
Sort and deduplicate linessort, uniq
Search for patterns in filesgrep
Extract specific fields or characterscut
Merge files line-by-linepaste

💡 Why This Matters

These tools form the foundation of text-based data manipulation in Linux:

  • They allow you to quickly inspect and analyze large files
  • You can filter, format, and combine data using simple yet powerful utilities
  • Together, they enable pipeline-style scripting (e.g., grep | sort | uniq) — a key part of Linux automation

🧠 Pro Tip: Combine Commands with Pipes!

Now that you know these individual tools, try combining them using the pipe (|) operator:

1
grep "error" /var/log/syslog | wc -l

Counts how many error messages are in the system log.

Or:

1
cat top-sites.txt | tail -10 | grep "org" | sort

Gets the last 10 sites, filters for “org”, and sorts alphabetically.


Networking Commands in Linux


1. Introduction

This video introduces essential Linux networking commands that help you:

✅ View your network configuration
✅ Test connectivity to remote servers
✅ Retrieve data from URLs

These tools are invaluable for troubleshooting, automation, and interacting with web services.


2. hostname – Get or Set the Host Name

Purpose

Displays or sets the hostname of the machine — a unique identifier used on the network.

Basic Usage

1
hostname

Output:

1
my-linux-machine.local
  • .local indicates your system uses zeroconf/local domains (e.g., Bonjour).

Options

OptionDescription
hostname -sShow short hostname (without domain suffix)
hostname -iShow IP address associated with the host

3. ifconfig – Interface Configuration

Purpose

Displays or configures network interfaces such as Ethernet (eth0) or Wi-Fi (wlan0).

⚠️ Note: ifconfig is deprecated in many modern Linux distros; use ip addr instead.

Basic Usage

1
ifconfig

Shows detailed information about all active interfaces including:

  • IP Address (inet)
  • MAC Address (ether)
  • Packets received/transmitted
  • Error/dropped packet counts

Example

1
ifconfig eth0

Shows details only for the Ethernet interface named eth0.


4. ping – Test Network Connectivity

Purpose

Tests whether a host or IP address is reachable by sending ICMP echo requests.

Basic Syntax

1
ping <hostname_or_ip>

Example

1
ping google.com

Output:

1
2
3
PING google.com (142.251.41.78): 56 data bytes
64 bytes from 142.251.41.78: icmp_seq=0 ttl=119 time=10.4 ms
...

Useful Options

OptionDescription
ping -c <count>Send a specific number of packets then stop
ping -i <seconds>Interval between packets (in seconds)

Example:

1
ping -c 5 google.com

Returns 5 ping results and summary statistics:

  • Number of packets transmitted and received
  • Packet loss percentage
  • Round-trip times (min/avg/max/stddev)

5. curl – Transfer Data from or to a URL

Purpose

A powerful command-line tool for transferring data using various protocols like HTTP, HTTPS, FTP, etc.

Basic Syntax

1
curl <url>

Examples

  • Download HTML content from Google:

    1
    
    curl http://www.google.com
    
  • Save output to a file:

    1
    
    curl -o google.html http://www.google.com
    

Common Uses

  • Testing API endpoints
  • Downloading files/scripts
  • Sending HTTP requests with custom headers/data

6. wget – Retrieve Files from Web URLs

Purpose

Downloads files from the web recursively and supports resuming broken downloads.

Basic Syntax

1
wget <url>

Example

Download a test file from W3.org:

1
wget https://www.w3.org/TR/2002/REC-xml-20021104/ISO-Latin-1-encoding.txt

Output Includes

  • Resolving host
  • Connecting to server
  • Sending HTTP request
  • Saving file locally (with original name by default)

Useful Options

OptionDescription
-O <filename>Specify custom output filename
-rRecursive download (mirror websites)
-cResume broken download

Example:

1
wget -O iso.txt https://www.w3.org/TR/2002/REC-xml-20021104/ISO-Latin-1-encoding.txt

7. Summary Table of Networking Commands

CommandPurposeExample
hostnameDisplay or set hostnamehostnamemy-linux-machine.local
hostname -sShow short hostnamehostname -smy-linux-machine
hostname -iShow IP addresshostname -i192.168.1.100
ifconfigShow network interface infoifconfig or ifconfig eth0
pingTest connectivity to a hostping google.com
ping -c 5 google.comPing 5 times and exitping -c 5 google.com
curlTransfer data from or to a URLcurl http://example.com
curl -o file.txt http://example.comSave output to a fileSaves content to file.txt
wgetDownload files from a URLwget http://example.com/file.txt
wget -O custom_name.txt urlDownload and rename filewget -O data.txt http://example.com/data

8. Final Tips

  • Use hostname and ifconfig to quickly check your machine’s identity and network status.
  • Use ping to test connection stability to a website or IP address.
  • Use curl for quick data transfer and testing APIs.
  • Use wget for downloading files, especially when working offline or scripting.

9. Bonus: Modern Alternative to ifconfigip Command

While ifconfig is widely known, it’s being replaced by the more powerful ip command suite:

Taskip Equivalent
Show IP addressesip addr show or ip a
Show routing tableip route show or ip r
Bring up/down an interfacesudo ip link set eth0 up

🌐 A Brief Introduction to Networking – Summary & Highlights

Great job reading through this foundational networking guide! This optional but valuable reading introduced you to core concepts in computer networking, helping you understand how computers communicate, share resources, and connect across networks like the Internet.


🎯 Learning Objectives Recap

After completing this reading, you are now able to:

Describe computer networks, network resources, and network nodes
Explain the roles of hosts, clients, and servers
Understand what packets and pings are
Differentiate between URLs and IP addresses


🧩 Key Concepts Explained

🔹 Computer Networks

  • A computer network is a collection of interconnected devices that can communicate and share resources.
  • Examples:
    • LAN (Local Area Network) – small, localized network (e.g., home or office)
    • WAN (Wide Area Network) – covers large geographical areas (e.g., the internet)
    • The Internet – a global network of networks

💡 The internet is essentially a network of computer networks.


🔁 Hosts, Clients, and Servers

TermDescription
HostAny device on a network with an IP address. Can act as a server or client
ClientRequests services or data from a server
ServerProvides services or data to clients (e.g., web servers, email servers)

💡 Many devices can switch roles — acting as both client and server when needed.


📦 Packets and Pings

What Is a Network Packet?

  • A packet is a formatted unit of data sent over a network.
  • Contains:
    • Control information: source, destination, routing info
    • Payload: actual data being transmitted

What Is ping?

  • A utility used to test connectivity between two hosts
  • Works by sending an “echo request” packet and waiting for a response
  • Helps diagnose connection issues (e.g., “Can I reach Google’s servers?”)

Example command:

1
ping google.com

🌍 URLs and IP Addresses

What Is an IP Address?

  • Stands for Internet Protocol Address
  • A unique identifier assigned to each device connected to a network
  • Used to locate and communicate with other devices
  • Example IPv4 address: 192.168.1.1
  • Example IPv6 address: 2001:0db8:85a3::8a2e:0370:7334

💡 When you use ping, it uses IP addresses to send and receive packets.


What Is a URL?

  • Stands for Uniform Resource Locator
  • Also known as a web address
  • Identifies the location of a resource on the internet and how to access it

URL Format:

1
protocol://hostname/path

Example:

1
https://en.wikipedia.org/wiki/URL

Breakdown:

  • Protocol: https – method to retrieve the resource
  • Hostname: en.wikipedia.org – server where the resource lives
  • Path: /wiki/URL – specific file/resource on the server

📌 URLs make it easier for humans to access resources without needing to remember complex IP addresses.


🧠 Why These Concepts Matter

Understanding these basics helps you:

  • Troubleshoot network issues using commands like ping
  • Understand how computers communicate over the internet
  • Work more effectively with web-based tools and services
  • Prepare for deeper learning about Linux networking commands

✅ Summary Table

ConceptDescription
Computer NetworkInterconnected computers sharing resources
Network ResourceAnything identifiable and accessible via a network (e.g., files, printers)
Network NodeAny device participating in the network (computers, routers, etc.)
HostA device that can be a client or server
ClientRequests data or services from a server
ServerProvides data or services to clients
PacketUnit of data containing control info + payload
PingTests network connectivity by sending echo requests
IP AddressUnique identifier for a device on a network
URLHuman-readable address pointing to a web resource

🧾 Exercise 1 – View Configuration Info About Your Network

In this exercise, you learned how to view your system’s network configuration, including:

  • Hostname
  • IP address
  • Network interface details using hostname and ip

This is essential for understanding how your machine connects to the network and communicates with other systems.


🔍 Overview of What You Learned

TaskCommand
View system hostnamehostname
View system IP addresshostname -i
Show all network interfacesip a or ip addr
Show info about a specific interface (e.g., eth0)ip addr show eth0

✅ Step-by-Step Breakdown

🔹 1.1 Display Your System’s Hostname and IP Address

View the current hostname:

1
hostname

Example output:

1
theia-2c65847f

The hostname helps identify your machine on a network — especially useful in server environments.

View the system’s IP address:

1
hostname -i

Example output:

1
172.17.0.2

💡 This shows the IPv4 address associated with your host.


🔹 1.2 Display Network Interface Configuration

Before running the ip command, you installed the iproute2 package:

Update and install:

1
2
sudo apt update
sudo apt install iproute2

Now you can use the powerful ip command.

Show all network interfaces:

1
ip a

or

1
ip addr

This displays information like:

  • Interface name (lo, eth0, etc.)
  • IP addresses (inet, inet6)
  • MAC address (link/ether)
  • Status (UP, DOWN)

Example output line:

1
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0

Here, 172.17.0.2 is your IPv4 address.

Show configuration for a specific interface (like eth0):

1
ip addr show eth0

📌 eth0 is typically the primary Ethernet interface used to connect to the network.

You’ll see:

  • The device status (UP)
  • Its IPv4 and IPv6 addresses
  • Broadcast and subnet mask info

📋 Summary Table: Useful Commands

PurposeCommand
View system hostnamehostname
View system IP addresshostname -i
List all network interfacesip a or ip addr
View specific interface (e.g., eth0)ip addr show eth0

🧠 Why These Tools Matter

Understanding your network configuration helps with:

  • Troubleshooting connectivity issues
  • Configuring servers
  • Monitoring network usage
  • Writing scripts that depend on network state

The ip command is a modern replacement for older tools like ifconfig, and it offers more flexibility and control.


🛠️ Real-World Use Cases

TaskCommand
Check if network is upip link show eth0
Find your public IP (from terminal)curl ifconfig.me
Monitor interface changesip monitor
Bring an interface up/downsudo ip link set eth0 up / sudo ip link set eth0 down

🧪 Exercise 2 – Test Network Connectivity with ping

In this exercise, you learned how to use the ping command to test whether your system can successfully communicate with another device or website over the network.

This is a fundamental tool for:

  • Checking internet connectivity
  • Diagnosing network issues
  • Testing server availability

🔍 Overview of What You Learned

TaskCommand
Ping a host continuouslyping www.google.com
Ping a host a specific number of timesping -c 5 www.google.com

✅ Step-by-Step Breakdown

🔹 2.1 Test Connectivity to a Host Using ping

Ping Google continuously:

1
ping www.google.com

You’ll see output like:

1
2
3
64 bytes from 142.251.42.78: icmp_seq=1 ttl=115 time=15.3 ms
64 bytes from 142.251.42.78: icmp_seq=2 ttl=115 time=14.9 ms
...

Each line shows:

  • The size of the response
  • The IP address of the responding server
  • Sequence number
  • Time-to-live (TTL)
  • Round-trip time in milliseconds

⚠️ To stop the ping process, press Ctrl + C


Ping a Host a Specific Number of Times

To limit the number of packets sent, use the -c option:

1
ping -c 5 www.google.com

This sends exactly 5 packets, then stops automatically.

Useful for:

  • Scripting and automation
  • Quick tests without manually stopping the command

📋 Summary Table

CommandDescription
ping hostnameTests if a remote host is reachable
ping -c N hostnamePings the host exactly N times
ping -c 5 google.comSends 5 packets to google.com and stops

🧠 Why This Matters

Using ping helps you quickly determine:

  • Whether you have internet access
  • If a remote server is online
  • How fast your connection is (based on response time)
  • If there’s packet loss or high latency

It’s one of the most basic yet powerful tools in any Linux user’s networking toolkit.


🛠️ Real-World Use Cases

ScenarioCommand
Check if you’re onlineping -c 4 google.com
Troubleshoot slow connectionsping google.com (observe response times)
Test local network devicesping 192.168.1.1
Monitor server availabilityping -c 10 server.example.com

📥 Exercise 3 – View or Download Data from a Server

In this exercise, you learned how to retrieve data from remote servers using two powerful command-line tools:

  • curl – for transferring data and viewing content directly in the terminal
  • wget – for downloading files (and even entire websites)

These tools are essential for:

  • Fetching configuration files
  • Downloading software or scripts
  • Interacting with APIs
  • Automating tasks that involve remote data

🔍 Overview of What You Learned

TaskCommand
View file contents from a URLcurl [URL]
Download and save a filecurl -O [URL]
Download a file using wgetwget [URL]

✅ Step-by-Step Breakdown

🔹 3.1 Transfer Data from a Server Using curl

View file contents from a URL:

1
curl https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DB0250EN-SkillsNetwork/labs/Bash%20Scripting/usdoi.txt

This displays the full text of the U.S. Declaration of Independence directly in your terminal.

💡 This is useful when you want to inspect remote files without saving them.


Save the file to your current directory:

1
curl -O https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DB0250EN-SkillsNetwork/labs/Bash%20Scripting/usdoi.txt

Now you have a local copy named usdoi.txt.

📁 The -O option tells curl to save the file using its original filename.


🔹 3.2 Download Files Using wget

First, remove the file if it already exists:

1
rm usdoi.txt

Then download it again using wget:

1
wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DB0250EN-SkillsNetwork/labs/Bash%20Scripting/usdoi.txt

This downloads the file and saves it in your current directory.

⚙️ wget is especially useful because:

  • It works well in scripts
  • Supports recursive downloads (-r)
  • Can continue interrupted downloads (-c)
  • Doesn’t require user interaction once started

📋 Summary Table: curl vs wget

Featurecurlwget
View content in terminal✅ Yes❌ No
Save file with original name✅ With -O✅ Yes
Recursive download❌ No✅ Yes with -r
Resume broken download❌ By default✅ With -c
Works silently❌ Noisy by default✅ With -q
Use caseAPI calls, quick inspectionFile downloads, scripting

🧠 Why These Tools Matter

With curl and wget, you can:

  • Automate downloads in scripts
  • Fetch live data from APIs or web services
  • Download large datasets or software packages
  • Troubleshoot HTTP responses and server connectivity

They’re foundational tools for working with networked resources in Linux.


🛠️ Real-World Examples

🔽 Download a file silently with wget:

1
wget -q https://example.com/file.zip

🖥️ Download and display JSON from an API:

1
curl https://api.github.com/users/octocat

📂 Recursively download an entire website:

1
wget -r https://example.com

📥 Resume a partially downloaded file:

1
wget -c http://example.com/largefile.iso

Exercise 4 – Exploring DNS and Looking Up Domain Information

In this exercise, you’ll learn how to:

  • Understand what DNS is and why it matters
  • Use nslookup and dig to look up domain information
  • Check IP addresses, name servers, and mail servers associated with a domain

These tools are essential for:

  • Troubleshooting domain issues
  • Checking website availability
  • Understanding how domains resolve to IP addresses
  • Debugging email delivery problems using MX records

🌐 What Is DNS?

DNS (Domain Name System) is like the phonebook of the internet.

It maps human-readable domain names (like google.com) to machine-readable IP addresses (like 172.217.174.78), so your computer knows where to find a website or service.

Key DNS Concepts:

TermMeaning
A RecordMaps a domain name to an IPv4 address
AAAA RecordMaps a domain to an IPv6 address
CNAMEAlias record pointing one domain to another
MX RecordSpecifies mail servers for a domain
NS RecordIdentifies authoritative name servers for a domain

🔍 Step-by-Step: Using nslookup

The nslookup command helps you query DNS servers to get domain-related information.

✅ View Basic DNS Info for a Website

1
nslookup google.com

This shows:

  • The IP address of the domain
  • The DNS server used to retrieve the info

✅ Look Up Mail Servers (MX Records)

1
nslookup -type=mx gmail.com

This lists the mail exchange (MX) servers responsible for receiving emails for gmail.com.


✅ Query Name Servers (NS Records)

1
nslookup -type=ns ibm.com

This shows which name servers are responsible for managing the domain’s DNS records.


🔎 Step-by-Step: Using dig

dig (Domain Information Groper) is a more detailed and powerful tool than nslookup. It gives you full control over DNS queries.

✅ View A Record for a Domain

1
dig google.com

Look for the ANSWER SECTION:

1
google.com.     299     IN      A       172.217.174.78

This shows the IPv4 address that google.com resolves to.


✅ Look Up MX Records with dig

1
dig MX gmail.com

Scroll down to the ANSWER SECTION to see which servers handle email for Gmail.


✅ Get All DNS Records for a Domain

1
dig ANY ibm.com

This fetches all available DNS records for ibm.com, including:

  • A / AAAA
  • CNAME
  • MX
  • NS
  • TXT (used for SPF, DKIM, etc.)

⚠️ Some domains may restrict “ANY” queries for security reasons.


📋 Summary Table: Useful DNS Commands

TaskCommand
View basic DNS infonslookup google.com
Look up MX records (for email)nslookup -type=mx gmail.com
Look up name serversnslookup -type=ns ibm.com
View A recorddig google.com
View MX recordsdig MX gmail.com
View all DNS recordsdig ANY ibm.com

🧠 Why This Matters

Understanding DNS helps you:

  • Diagnose domain resolution issues
  • Verify email server settings
  • Configure custom domains
  • Troubleshoot website downtime

Tools like nslookup and dig are invaluable for system administrators, developers, and anyone working with web services.


🛠️ Real-World Examples

ScenarioCommand
Check if a site resolves correctlydig example.com
Find who handles a domain’s emaildig MX example.com
Debug DNS propagation after changesdig @8.8.8.8 example.com (use Google’s public DNS)
Test local DNS cachenslookup example.com (before and after flush)

🛠️ Practice Exercises – Networking in Linux

These exercises will help reinforce your understanding of network-related commands in Linux. You’ll be working with tools like hostname, ping, ip, curl, and wget to inspect network configuration, test connectivity, and transfer data.


🔧 Before You Begin

Make sure you’re in the correct directory:

1
2
cd /home/project
pwd

You should see:

1
/home/project

Now let’s go through each exercise step-by-step.


✅ 1. Display Your Host’s IP Address

💡 Hint:

Use the hostname command with an option that shows the IP address.

✅ Solution:

1
hostname -i

This displays your system’s internal IPv4 address, such as:

1
172.17.0.2

This is useful for checking what IP address your machine is using on the local network.


✅ 2. Get Connectivity Stats on Your Connection to www.google.com

💡 Hint:

Use the ping command with a limited number of packets.

✅ Solution:

1
ping -c 5 www.google.com

This sends 5 ICMP echo requests to Google’s servers and returns stats like:

  • Round-trip time (latency)
  • Packet loss

Example output:

1
2
3
--- www.google.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4005ms
rtt min/avg/max/mdev = 10.2/11.8/13.9/1.2 ms

This helps determine if your system can reach external sites and how fast it does so.


✅ 3. View Info About Your Ethernet Adapter eth0

💡 Hint:

Use the ip command to show details about the network interface.

✅ Solution:

1
ip addr show eth0

This shows:

  • Interface status (UP)
  • IPv4 and IPv6 addresses
  • MAC address
  • Broadcast and subnet info

Look for the line starting with inet — this is your IP address.

Example:

1
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0

This is helpful when troubleshooting or configuring network interfaces.


✅ 4. View the HTML Code for www.google.com’s Landing Page

💡 Hint:

Use curl to fetch and display remote content.

✅ Solution:

1
curl www.google.com

This displays the raw HTML source code of Google’s homepage directly in your terminal.

Tip: If the output looks messy, try saving it to a file instead (next step).


✅ 5. Download the HTML Code for www.google.com’s Landing Page

💡 Hint:

Use wget to download and save the page.

✅ Solution:

1
wget www.google.com

This saves the downloaded HTML as:

1
index.html

Verify the file exists:

1
ls -l

You’ll see something like:

1
-rw-r--r-- 1 user user 12345 Apr 5 10:00 index.html

You now have a local copy of Google’s home page!


📋 Summary Table

TaskCommand
Show host IP addresshostname -i
Test connectivity to Googleping -c 5 www.google.com
View eth0 interface infoip addr show eth0
View HTML of Googlecurl www.google.com
Download Google’s HTMLwget www.google.com

File Archiving and Compression Commands in Linux


1. Introduction

✅ Understand the difference between archiving and compression
✅ Create and extract tarballs (.tar files)
✅ Compress and decompress using gzip and zip
✅ Extract contents from compressed archives

These skills are essential for:

  • Backing up data
  • Transferring large collections of files
  • Managing disk space

2. Key Concepts

A. Archiving

  • Definition: Combines multiple files into a single file without reducing size.
  • Purpose: For portability and backup.
  • Example Format: .tar

B. Compression

  • Definition: Reduces file size by removing redundancy.
  • Purpose: Saves storage space, speeds up transfers, and reduces bandwidth usage.
  • Common Tools:
    • gzip – used with .tar.gz or .tgz
    • zip – used with .zip

3. Using tar – Tape ARchiver

Purpose

Create or extract archive files (called tarballs) that bundle directories and files.

Basic Syntax

1
tar [options] [archive_name.tar] [files_or_directories]

Common Options

OptionMeaning
-cCreate a new archive
-fSpecify filename
-tList contents of an archive
-xExtract files from archive
-zFilter through gzip (for compression/decompression)

A. Creating a Tar Archive

To archive a directory called notes:

1
tar -cf notes.tar notes/

Creates notes.tar, containing all files in the notes directory.


B. Listing Contents of a Tar File

To view what’s inside notes.tar:

1
tar -tf notes.tar

C. Extracting Files from a Tar Archive

To extract files:

1
tar -xf notes.tar

D. Compressing a Tar Archive with GZIP

To compress notes.tar into a smaller notes.tar.gz:

1
tar -czf notes.tar.gz notes/

This bundles and compresses the notes directory into a single compressed archive.


E. Extracting a .tar.gz File

To extract both the archive and its compressed contents:

1
tar -xzf notes.tar.gz

The -z option automatically handles gzip compression.


4. Using zip and unzip

A. What is zip?

  • Combines compression and archiving in one step.
  • Produces .zip files — widely supported across operating systems.

Creating a ZIP Archive

To compress the notes folder into notes.zip:

1
zip -r notes.zip notes/

The -r flag ensures all subdirectories are included.


Listing Contents of a ZIP File

Use unzip to list contents:

1
unzip -l notes.zip

Extracting a ZIP File

To extract the contents:

1
unzip notes.zip

Automatically recreates the original directory structure.


5. Summary Table of Archiving & Compression Commands

TaskCommandDescription
Create tar archivetar -cf archive.tar folder/Bundle files into a single .tar file
List tar contentstar -tf archive.tarView files inside .tar
Extract tar archivetar -xf archive.tarUnpack .tar without compression
Create compressed tar.gztar -czf archive.tar.gz folder/Archive + gzip compression
Extract tar.gz filetar -xzf archive.tar.gzDecompress and unpack .tar.gz
Create zip archivezip -r archive.zip folder/Compress and bundle in .zip format
List zip contentsunzip -l archive.zipShow files inside .zip
Extract zip fileunzip archive.zipUncompress and unpack .zip

6. Comparison: tar vs zip

Featuretarzip
Primary UseArchiving (with optional compression)Archiving + built-in compression
Compression ToolUsually combined with gzip (tar.gz)Built-in compression
Cross-Platform SupportLimited on WindowsWidely supported
Recursive by DefaultYesRequires -r for folders
Preserves PermissionsYes (on Unix/Linux)No (on Windows)

7. Example Directory Structure

Suppose you have this directory:

1
2
3
4
5
6
7
notes/
├── math/
│   ├── week1.txt
│   └── week2.txt
└── physics/
    ├── week1.txt
    └── week2.txt

You can compress it into:

  • notes.tar (just archived)
  • notes.tar.gz (archived + compressed)
  • notes.zip (compressed archive)

And later extract it back to the same structure using the appropriate command.


8. Final Tips

  • Use tar when preserving permissions and working in Linux/Unix environments.
  • Use zip for cross-platform compatibility (e.g., sharing with Windows users).
  • Always double-check your file extensions:
    • .tar → just archived
    • .tar.gz or .tgz → archived + compressed
    • .zip → compressed archive
  • Combine tar with gzip for efficient backups.
  • Use unzip and tar -tzf to preview contents before extraction.

📦 Exercise 1 - File and Folder Archiving and Compression

In this exercise, you learned how to package, compress, and extract files using the Linux command line tools:

  • tar – for creating .tar archives
  • zip – for compressing into .zip format
  • unzip – for extracting .zip files

These are essential skills for:

  • Backing up data
  • Transferring multiple files as a single package
  • Saving disk space with compression
  • Managing software distributions or logs

🔧 Step-by-Step Breakdown

🔹 1.1 Create and Manage File Archives with tar

Create a .tar archive of the /bin directory:

1
tar -cvf bin.tar /bin
  • -c = create a new archive
  • -v = show progress (verbose)
  • -f = specify filename (bin.tar)

This creates a large file called bin.tar, which contains all files from the /bin directory.


List contents of the .tar archive:

1
tar -tvf bin.tar

This shows a detailed list of files inside the archive — useful for verifying contents before extraction.


Extract files from the .tar archive:

1
tar -xvf bin.tar
  • -x = extract
  • -v = show progress
  • -f = specify filename

After extraction, you’ll see a folder named bin in your current directory.

Tip: Use ls -l to confirm it was extracted correctly.


🔹 1.2 Package and Compress Archive Files with zip

Create a .zip file of all .conf files in /etc:

1
zip config.zip /etc/*.conf

This bundles all configuration files ending in .conf into one compressed file: config.zip.


Create a compressed ZIP archive of an entire directory:

1
zip -ry bin.zip /bin
  • -r = recursively include all files and subdirectories
  • -y = store symbolic links as such (instead of following them)

This compresses the entire /bin directory into a portable bin.zip file.


🔹 1.3 Extract, List, or Test ZIP Archives with unzip

List contents of a .zip file:

1
unzip -l config.zip

This shows what’s inside the archive without extracting anything — helpful for previewing.


Extract all files from a .zip archive:

1
unzip -o bin.zip
  • -o = overwrite existing files (useful if you run the command more than once)
  • Automatically creates a bin directory containing the extracted files

📋 Summary Table

TaskCommand
Create tar archivetar -cvf archive.tar directory/
List tar contentstar -tvf archive.tar
Extract tar archivetar -xvf archive.tar
Zip specific fileszip archive.zip file1 file2
Zip entire directoryzip -r archive.zip directory/
List zip contentsunzip -l archive.zip
Extract zip archiveunzip -o archive.zip

🧠 Why This Matters

Understanding archiving and compression helps you:

  • Bundle and share multiple files easily
  • Save storage space
  • Backup important directories
  • Transfer data securely between systems

tar is commonly used in Linux environments for packaging, while zip offers cross-platform compatibility with Windows and macOS.


💡 Pro Tips

  • Combine tar and gzip for compressed archives:
    1
    
    tar -czvf archive.tar.gz directory/
    
  • Use wildcards with zip:
    1
    
    zip logfiles.zip *.log
    
  • Exclude files when zipping:
    1
    
    zip -r project.zip project/ -x "*.git*"
    

🎉 Module Summary & Highlights – Great Work!

You’ve successfully completed a comprehensive module that covers essential Linux command-line skills, from navigating the system and managing files to working with networks, processes, and archives.

Here’s a clean, organized summary of what you’ve learned — your Linux command cheat sheet for future reference.


🔧 Shell & Terminal Basics

TaskCommand
Start shellDefault shell is Bash
Display current userwhoami
Show user ID infoid
Get OS infouname -a
List directory contentsls, ls -l
Change directoriescd directory_name
Show current pathpwd
Find filesfind /path -name "filename"

📁 File and Directory Management

TaskCommand
Create filetouch filename
Make directorymkdir dirname
Copy file/dircp source destination
Move or renamemv old new
Remove filerm filename
Remove empty dirrmdir dirname
View file contentcat filename
View first N lineshead -N filename
View last N linestail -N filename
Count lines/words/charswc, wc -l filename
Sort linessort filename
Remove duplicatesuniq filename
Search in filesgrep "pattern" filename
Extract fieldscut -d "," -f2 filename
Merge files line-by-linepaste file1 file2

💾 Archiving and Compression

TaskCommand
Create .tar archivetar -cvf archive.tar folder/
List .tar contentstar -tvf archive.tar
Extract .tar archivetar -xvf archive.tar
Compress into .zipzip -r archive.zip folder/
List .zip contentsunzip -l archive.zip
Extract .zip archiveunzip archive.zip
Combine tar + gziptar -czvf archive.tar.gz folder/

🌐 Networking Tools

TaskCommand
View hostnamehostname
View IP addresshostname -i
Inspect network interfacesip addr or ip a
Test connectivityping www.google.com
Transfer datacurl https://example.com/file.txt
Download fileswget https://example.com/file.txt
Look up DNS recordsnslookup example.com or dig example.com

🖥️ System Monitoring & Info

TaskCommand
Disk spacedf -h
Running processesps -e, top
Current date/timedate
Print text/valuesecho "message"
Read manual pagesman command

🔒 File Permissions and Ownership

TaskCommand
View permissionsls -l
Change permissionschmod u+rwx filename
Change ownershipchown user:group filename (requires root)

🛠️ Text Processing & Data Wrangling

TaskCommand
Sort linessort filename
Remove duplicate linessort file | uniq
Extract patternsgrep "search" file
Cut out columnscut -d "," -f1 file.csv
Merge files side-by-sidepaste file1 file2

🧠 Why This Matters

You now have a solid foundation in:

  • Navigating and managing files
  • Searching and processing text
  • Understanding and controlling file permissions
  • Working with networked resources
  • Archiving and compressing files

These are core skills used daily by:

  • System administrators
  • Developers
  • Data engineers
  • DevOps engineers
  • Security analysts

📄 Module 2 Cheat Sheet – Introduction to Linux Commands

This cheat sheet is your go-to reference for the most commonly used Linux commands in system navigation, file management, text processing, networking, and more.


Getting Information

TaskCommand
Show current userwhoami
Display user/group ID infoid
Show OS and kernel infouname -a
View command manualman top
List all available man pagesman -k .
Get help on a commandcurl --help
Show current date/timedate

🧭 Navigating and Working with Directories

TaskCommand
List files by date (newest first)ls -lrt
Find .sh files in directory treefind -name "*.sh"
Show current working directorypwd
Create a new directorymkdir new_folder
Move up one levelcd ../
Go to home directorycd ~ or just cd
Remove empty directoryrmdir temp_directory -v

🔍 Monitoring System Performance

TaskCommand
List running processesps
List all processesps -e
View real-time system statstop
Check disk space usagedf
Show disk usage of directoriesdu (not listed but useful)

📁 Creating, Copying, Moving, and Deleting Files

TaskCommand
Create an empty filetouch a_new_file.txt
Copy a filecp file.txt new_path/new_name.txt
Rename or move a filemv this_file.txt that_path/that_file.txt
Delete a filerm this_old_file.txt -v

🔐 Working with File Permissions

TaskCommand
Make file executable for everyonechmod +x my_script.sh
Give owner execute permissionchmod u+x my_file.txt
Remove read from group & otherschmod go-r filename
Change file ownershipchown user:group file (as root)

📖 Displaying File and String Contents

TaskCommand
View full file contentscat my_shell_script.sh
View file page-by-pagemore ReadMe.txt
View first N lineshead -10 data_table.csv
View last N linestail -10 data_table.csv
Print string or variableecho "I am $USERNAME"

🧹 Basic Text Wrangling

Sorting and Deduplication

TaskCommand
Sort file alphabeticallysort text_file.txt
Sort in reverse ordersort -r text_file.txt
Remove duplicate linesuniq list_with_duplicates.txt

Counting Lines/Words/Characters

TaskCommand
Count lines in a filewc -l table_of_data.csv
Count words in a filewc -w my_essay.txt
Count characters in a filewc -m some_document.txt

Searching with grep

TaskCommand
Search case-insensitively for “hello”grep -iw hello a_bunch_of_hellos.txt
List files containing “hello”grep -l hello *.txt

Merging Files with paste

TaskCommand
Merge files side-by-sidepaste first_name.txt last_name.txt phone_number.txt
Use comma as delimiterpaste -d "," first_name.txt last_name.txt phone_number.txt

Extracting Data with cut

TaskCommand
Extract first field (CSV)cut -d "," -f 1 names.csv
Extract bytes 2–5 from each linecut -b 2-5 my_text_file.txt
Extract from byte 10 onwardcut -b 10- my_text_file.txt

📦 Compression and Archiving

TaskCommand
Create tar archivetar -cvf my_archive.tar file1 file2
Compress with zipzip my_zipped_files.zip file1 file2
Compress directory with zipzip -r my_zipped_folders.zip dir1 dir2
Extract zip fileunzip my_zipped_file.zip
Extract zip to specific folderunzip my_zipped_file.zip -d extract_to_this_directory

🌐 Networking Commands

TaskCommand
Show hostnamehostname
Test network connectivityping www.google.com
Configure/view network interfacesip
Download content from URLcurl <url>
Download and save filewget <url>

🧠 Pro Tips

  • Combine commands using pipes:
    1
    
    grep "error" /var/log/syslog | wc -l
    
  • Use wildcards to match patterns:
    1
    
    ls *.txt
    
  • Always double-check what you’re about to delete:
    1
    
    rm -i *.tmp
    

You’re now equipped with a powerful set of tools to work confidently in the Linux environment. Whether you’re managing servers, writing scripts, or analyzing logs — this cheat sheet will be your best companion.


This post is licensed under CC BY 4.0 by the author.