LPIC-1 Module 3: GNU and Unix Commands – A Complete Study Guide with Quiz & Commands

Published On: 13 July 2025

Objective

GNU and Unix commands are the lifeblood of everyday Linux system administration. From manipulating files to managing processes and networking, mastering these commands makes you faster, more efficient, and exam-ready for LPIC-1. This blog focuses on using command-line tools for file operations, text processing, stream redirection, permissions, compression, and more. If you want to work with Linux systems professionally, knowing these commands is non-negotiable.

Key Topics Covered

Working with Files and Directories

Meaning: In Linux, mastering file and directory management is fundamental for navigating and administering any Linux system. Whether you are copying a file, removing an unwanted directory, or locating a specific document buried deep in your filesystem, knowing the right commands saves time and avoids errors. File and directory operations are essential skills tested in LPIC-1 and are also required daily by any Linux user. From using ls to list files to more powerful commands like find for deep searches, these commands help in organizing and manipulating data effectively.

Commands Involved:

ls -l                     # Lists files in long format with permissions, owner, size, and time
ls -la                    # Includes hidden files (starting with .)
cp file1 file2           # Copies file1 to file2
cp -r dir1 dir2          # Copies directories recursively
mv old.txt new.txt       # Renames or moves a file
rm file.txt              # Removes (deletes) file.txt
rm -i file.txt           # Interactive removal with confirmation
rm -rf directory/        # Recursively removes directory and contents (DANGEROUS)
find / -name "filename"  # Searches from root for files named 'filename'
find /home -size +100M   # Finds files larger than 100MB in /home

Command Breakdown:

  • ls -l: Lists directory contents in long format showing file permissions, size, and date
  • cp: Copies files or directories from one location to another
  • mv: Moves or renames files and directories
  • rm: Deletes files and directories (be cautious, this is irreversible without backups)
  • find: Searches for files and directories based on name, type, size, and more

Safety Tips:

  • Always use rm -i for important files to get confirmation prompts
  • Never run rm -rf / or similar commands without absolute certainty
  • There is no built-in "undo" for rm operations - deleted files are gone unless you have backups

Quiz Question:
What command is used to recursively find all .log files under /var/log?

A) cp /var/log/*.log /backup
B) find /var/log -name "*.log"
C) ls -l /var/log/*.log
D) grep "*.log" /var/log

Answer: B - find /var/log -name "*.log" searches recursively for files matching the pattern. Option A copies files, C only lists files in the current directory, and D searches file contents.

Viewing and Editing File Contents

Meaning: Viewing and editing file contents is a core task in Linux system management. Viewing commands let you read files quickly without changes, which is helpful for examining logs, configs, or output. Editing commands allow you to make permanent changes to files such as configuration or script files. Mastering these commands is key for troubleshooting and system administration.

Commands Involved:

cat file.txt              # Outputs entire content of file.txt to the terminal
less file.txt             # Allows scrolling through file.txt one screen at a time
more file.txt             # Similar to less but with fewer features
head file.txt             # Shows first 10 lines (default)
head -n 20 file.txt       # Shows first 20 lines of file.txt
tail file.txt             # Shows last 10 lines (default)
tail -n 20 file.txt       # Displays last 20 lines of file.txt
tail -f /var/log/syslog   # Follows log file in real-time (live monitoring)
nano file.txt             # Opens file.txt in nano editor (simple, user-friendly)
vim file.txt              # Opens file.txt in vim editor (advanced, modal editor)

Command Breakdown:

  • cat: Displays the full content of a file; useful for quick viewing or piping output
  • less: Allows paginated viewing with navigation controls, ideal for large files
  • head: Shows the beginning lines of a file; great for previewing content
  • tail: Shows the ending lines of a file; often used for monitoring logs in real time
  • nano: Simple terminal text editor suitable for quick edits
  • vim: Powerful and widely used text editor with extensive features

Quiz Question:
What command would you use to view the last 20 lines of a log file?

A) head -n 20 /var/log/messages
B) tail -n 20 /var/log/messages
C) less /var/log/messages
D) cat /var/log/messages

Answer: B - tail -n 20 /var/log/messages shows the last 20 lines. Option A shows first 20 lines, C shows entire file with pagination, and D shows entire file at once.

Using Pipes, Filters, and Redirection

Meaning: In Linux, pipes, filters and redirection are fundamental concepts for managing command input and output. Pipes connect the output of one command directly into the input of another, allowing complex data processing workflows. Filters are commands like grep, awk, or sort that process and transform data streams. Redirection (>, >>, <) lets you send output to files or read input from files instead of the terminal. Mastering these helps in efficiently handling data and automating tasks.

Commands Involved:

ls -l | grep "Jan"              # Pipes output of ls -l to grep to filter lines containing 'Jan'
cat file.txt | sort             # Sends file.txt content through sort command
grep "error" /var/log/messages  # Filters lines containing 'error' from a file
ls > filelist.txt               # Redirects output of ls to filelist.txt (overwrite)
echo "New line" >> filelist.txt # Appends "New line" to filelist.txt
sort < unsorted.txt             # Takes input from unsorted.txt and sorts it
sort < input.txt > output.txt   # Input and output redirection combined
command 2> error.log            # Redirects error messages (stderr) to file
command &> all_output.log       # Redirects both stdout and stderr to file

Advanced Examples:

# Count unique IP addresses in access log
cut -d' ' -f1 access.log | sort | uniq -c | sort -nr

# Find and count specific patterns
grep -c "ERROR" /var/log/syslog

# Process data with multiple pipes
cat data.txt | grep "pattern" | sort | uniq | wc -l

Symbol Meanings:

  • | (pipe): Passes output of one command as input to another
  • grep: Filters input for lines matching a pattern
  • sort: Sorts lines of text input
  • >: Redirects output to a file, overwriting existing content
  • >>: Appends output to a file
  • <: Redirects input from a file
  • 2>: Redirects error messages (stderr) to a file
  • &>: Redirects both output and errors to a file

Quiz Question:
Which symbol is used to send the output of one command as input to another?

A) >
B) <
C) |
D) &

Answer: C - The pipe symbol | passes output of one command as input to another. Option A redirects to file, B redirects from file, and D runs command in background.

Managing Permissions and Ownership

Meaning: In Linux systems, every file and directory has permissions and ownership attributes that control access. Permissions define what actions such as read, write, execute users can perform, while ownership specifies the user and group that own the file. Properly managing permissions and ownership is essential for system security and multi-user environments to prevent unauthorized access or modifications.

Commands Involved:

ls -l filename               # Lists file details including permissions and ownership
chmod 755 filename           # Changes file permissions using octal notation (rwxr-xr-x)
chmod u+x script.sh          # Adds execute permission for owner (user)
chmod g-w file.txt           # Removes write permission for group
chown user:group filename    # Changes file owner and group simultaneously
chown user filename          # Changes only the owner
chgrp group filename         # Changes only the group
stat filename                # Displays detailed file status, including permissions
umask                        # Shows current default permission mask
umask 022                    # Sets default permission mask to 022

Permission System Explanation:

  • Read (r): Permission to view file contents or list directory contents
  • Write (w): Permission to modify file contents or create/delete files in directory
  • Execute (x): Permission to run file as program or access directory

Numeric Permissions:

  • 7 (rwx): Read, write, and execute
  • 6 (rw-): Read and write
  • 5 (r-x): Read and execute
  • 4 (r--): Read only

Umask Clarification:
The umask value is subtracted from default permissions:

  • Default directory permissions: 777 - umask = actual permissions
  • Default file permissions: 666 - umask = actual permissions
  • umask 022: Creates directories with 755 (rwxr-xr-x) and files with 644 (rw-r--r--)

Command Breakdown:

  • ls -l: Lists files with permissions, owner, and group
  • chmod: Modifies file permissions using symbolic or numeric modes
  • chown: Changes the owner and/or group of a file
  • stat: Provides detailed file information
  • umask: Sets default permissions for new files

Quiz Question:
Which command changes the ownership of a file to user "john" and group "developers"?

A) chmod john:developers filename
B) chown john:developers filename
C) stat john:developers filename
D) ls -l john:developers filename

Answer: B - chown john:developers filename changes both owner and group. Option A changes permissions, C displays file info, and D lists file details.

Locating Files and Directories

Meaning: In Linux, knowing how to find files and directories is important for system navigation, administration, and troubleshooting. The system provides several commands to search and locate files based on name, type or other attributes efficiently. Mastering these commands helps users quickly find the data or configuration files they need.

Commands Involved:

find /path -name "filename"         # Searches for files named "filename" starting from /path
find /home -name "*.txt"            # Finds all .txt files in /home directory
find /var -size +10M                # Finds files larger than 10MB in /var
find /etc -type f -name "*.conf"    # Finds regular files with .conf extension
find /tmp -mtime -7                 # Finds files modified in last 7 days
locate filename                     # Quickly finds files by name using prebuilt database
which command                       # Shows full path of an executable command
whereis command                     # Displays locations of binary, source, and manual files
updatedb                           # Updates the database used by 'locate' command

Advanced Find Examples:

# Find and execute commands on results
find /home -name "*.log" -exec rm {} \;

# Find files by permissions
find /home -perm 777

# Find files by user
find /home -user john

# Find empty files
find /tmp -empty

Command Breakdown:

  • find: Recursively searches directories based on various criteria like name, size, type, etc.
  • locate: Uses a regularly updated database to quickly find files by name (faster than find but less up-to-date)
  • which: Finds the path of executables in the user's PATH
  • whereis: Provides locations for binaries, source code, and man pages related to a command
  • updatedb: Updates the database used by locate

Quiz Question:
Which command would you use to search for a file named "httpd.conf" starting from the root directory?

A) locate httpd.conf
B) find / -name "httpd.conf"
C) which httpd.conf
D) whereis httpd.conf

Answer: B - find / -name "httpd.conf" searches from root directory for the specific file. Option A searches database, C finds executables in PATH, and D finds binary/source/man locations.

Archiving and Compressing Files

Meaning: Archiving and compressing files are common tasks in Linux to reduce file size and combine multiple files into a single archive for easier storage, transfer, and backup. Archiving means bundling files together without reducing their size, while compression reduces file size by encoding the data efficiently. Together, these processes save disk space and streamline file management.

Commands Involved:

# Tar (Tape Archive) Commands
tar -cvf archive.tar /path/to/files     # Creates an archive (c=create, v=verbose, f=file)
tar -xvf archive.tar                    # Extracts files from archive (x=extract)
tar -czvf archive.tar.gz /path/to/files # Creates gzip compressed archive (z=gzip)
tar -xzvf archive.tar.gz                # Extracts gzip compressed archive
tar -cjvf archive.tar.bz2 /path/to/files # Creates bzip2 compressed archive (j=bzip2)
tar -xjvf archive.tar.bz2               # Extracts bzip2 compressed archive

# Compression Commands
gzip file.txt                           # Compresses file.txt to file.txt.gz
gunzip file.txt.gz                      # Decompresses file.txt.gz back to file.txt
bzip2 file.txt                          # Compresses file.txt to file.txt.bz2 (better compression)
bunzip2 file.txt.bz2                    # Decompresses bzip2 file
xz file.txt                             # Compresses file.txt to file.txt.xz (highest compression)
unxz file.txt.xz                        # Decompresses xz file

# Zip Commands
zip -r archive.zip /path/to/files       # Creates zip archive recursively (-r for directories)
unzip archive.zip                       # Extracts files from zip archive
unzip -l archive.zip                    # Lists contents without extracting

Compression Comparison:

  • gzip: Fast compression, widely supported
  • bzip2: Better compression ratio than gzip, slower
  • xz: Best compression ratio, slowest
  • zip: Cross-platform compatibility, moderate compression

Command Breakdown:

  • tar: Utility to create and extract archives (tarballs)
  • gzip/gunzip: Compression and decompression using the gzip algorithm
  • bzip2/bunzip2: Better compression using bzip2 algorithm
  • xz/unxz: Highest compression using xz algorithm
  • zip/unzip: Alternative compression and extraction utility widely used across platforms

Quiz Question:
Which command will create a gzip compressed archive named backup.tar.gz of the /etc directory?

A) tar -cvf backup.tar.gz /etc
B) gzip -c /etc > backup.tar.gz
C) tar -czvf backup.tar.gz /etc
D) zip -r backup.tar.gz /etc

Answer: C - tar -czvf backup.tar.gz /etc creates a gzip compressed tar archive. Option A creates uncompressed tar, B won't work with directories, and D creates zip format.

Managing Text Streams

Meaning: Managing text streams is essential in Linux for processing, transforming, and extracting information from text data. Text streams refer to continuous flow of text data, which can be manipulated using various command-line utilities. Mastery of text stream management allows system administrators and users to efficiently handle logs, configuration files, and outputs of commands.

Commands Involved:

# Basic Text Display
cat file.txt                        # Displays the contents of a file
head file.txt                       # Shows the first 10 lines (default)
head -n 20 file.txt                 # Shows the first 20 lines
tail file.txt                       # Shows the last 10 lines (default)
tail -n 20 file.txt                 # Shows the last 20 lines
less file.txt                       # Allows scrolling through a file interactively

# Text Processing
grep 'pattern' file.txt             # Searches for a pattern in a file
grep -i 'pattern' file.txt          # Case-insensitive search
grep -n 'pattern' file.txt          # Shows line numbers with matches
grep -c 'pattern' file.txt          # Counts matching lines
cut -d',' -f1 file.csv              # Extracts specific fields using delimiter
cut -c1-10 file.txt                 # Extracts characters 1-10 from each line
sort file.txt                       # Sorts the lines in a file alphabetically
sort -n numbers.txt                 # Sorts numerically
sort -r file.txt                    # Sorts in reverse order
uniq file.txt                       # Removes duplicate adjacent lines (file must be sorted)
uniq -c file.txt                    # Counts occurrences of each unique line
wc -l file.txt                      # Counts the number of lines
wc -w file.txt                      # Counts the number of words
wc -c file.txt                      # Counts the number of characters
tee output.txt                      # Reads from stdin, writes to stdout and file
tr 'a-z' 'A-Z' < file.txt          # Translates lowercase to uppercase
sed 's/old/new/g' file.txt          # Replaces all instances of 'old' with 'new'
awk '{print $1}' file.txt           # Prints the first field of each line

Important Note about uniq: The uniq command only removes duplicate adjacent lines. This is why input is typically sorted first:

# Correct usage:
sort file.txt | uniq

# Count unique lines:
sort file.txt | uniq -c | sort -nr

Advanced Text Processing Examples:

# Extract usernames from /etc/passwd
cut -d':' -f1 /etc/passwd

# Count unique IP addresses in log
awk '{print $1}' access.log | sort | uniq -c | sort -nr

# Find lines containing "error" (case insensitive)
grep -i "error" /var/log/syslog

# Replace text in file and save to new file
sed 's/old_text/new_text/g' input.txt > output.txt

Command Breakdown:

  • cat: Displays or concatenates files
  • head/tail: Preview start or end of files
  • less: Provides interactive viewing
  • grep: Searches for matching text patterns
  • cut: Extracts columns or fields from text
  • sort/uniq: Organize and filter text lines
  • wc: Counts lines, words, or characters
  • tee: Duplicates output to file and screen simultaneously
  • tr: Translates or transforms characters
  • sed: Stream editor for filtering and transforming text
  • awk: Pattern scanning and processing language

Quiz Question:
Which command will display the last 20 lines of a file named logfile.txt?

A) head -20 logfile.txt
B) tail -20 logfile.txt
C) less logfile.txt
D) grep -20 logfile.txt

Answer: B - tail -20 logfile.txt shows the last 20 lines. Option A shows first 20 lines, C shows entire file with pagination, and D is invalid syntax.

Real-World Use Cases

1. Daily File Operations

System administrators frequently perform file management tasks like copying configuration files for backups, renaming log files to keep them organized, and cleaning up old or unnecessary files in directories. These operations ensure that system files are well-maintained and prevent clutter, which could affect system performance or troubleshooting.

Example:

# Backup configuration before changes
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup

# Clean up old log files
find /var/log -name "*.log" -mtime +30 -exec rm {} \;

2. Log Analysis

Analyzing system logs is vital for monitoring system health and troubleshooting issues. Tools like grep help filter out specific errors or events, tail allows viewing the latest entries in real-time, and awk extracts structured data from logs for reporting or alerting purposes, enabling admins to quickly identify problems.

Example:

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

# Find all error messages from today
grep "$(date '+%b %d')" /var/log/syslog | grep -i error

# Count different types of HTTP responses
awk '{print $9}' access.log | sort | uniq -c | sort -nr

3. Automation Scripts

Shell scripts automate repetitive tasks by using redirection and pipes to connect multiple commands. Utilities like sed help modify text on the fly, making scripts more powerful and adaptable. These scripts save time and reduce human error by automating system administration duties.

Example:

# Script to backup and rotate logs
#!/bin/bash
DATE=$(date +%Y%m%d)
tar -czf /backup/logs_$DATE.tar.gz /var/log/
find /backup -name "logs_*.tar.gz" -mtime +7 -delete

4. User Audits

To manage users effectively, admins analyze user data by extracting relevant fields using cut, sorting data with sort, and removing duplicates with uniq. This helps generate clean, organized reports on user activity, permissions, or login records to maintain system security and compliance.

Example:

# Generate user report
cut -d':' -f1,3,5 /etc/passwd | sort -t':' -k2 -n > user_report.txt

# Find users with specific shell
grep "/bin/bash$" /etc/passwd | cut -d':' -f1

5. Disk Cleanup

Disk space management is critical to prevent system issues. Commands like find locate large or old files based on size or modification date, while xargs executes deletion or archiving commands on those files. This automated cleanup helps keep the filesystem optimized and prevents storage from filling up.

Example:

# Find large files
find /home -size +100M -ls

# Find and archive old files
find /tmp -type f -mtime +7 | xargs tar -czf /backup/old_tmp_files.tar.gz

# Clean up empty directories
find /tmp -type d -empty -delete

Must-Know Commands & Configuration

File Operations

  • ls -l - Lists files in long format with permissions, owner, size, and modification time
  • ls -la - Includes hidden files (starting with .)
  • cp file1 file2 - Copies file1 to file2
  • mv old.txt new.txt - Renames old.txt to new.txt or moves it
  • rm -i file.txt - Interactive removal with confirmation
  • find / -name "filename" - Searches from root for files named 'filename'
  • find /home -size +100M - Finds files larger than 100MB

File Content

  • cat file.txt - Outputs the entire content of file.txt to the terminal
  • less file.txt - Allows scrolling through file.txt one screen at a time
  • head -n 10 file.txt - Shows the first 10 lines of file.txt
  • tail -n 10 file.txt - Displays the last 10 lines of file.txt
  • tail -f /var/log/syslog - Follows log file in real-time

Permissions

  • chmod 755 script.sh - Sets permissions: rwx for owner, rx for group and others
  • chmod u+x script.sh - Adds execute permission for owner
  • chown user:group file.txt - Changes the ownership of file.txt to 'user' and group to 'group'
  • umask 022 - Sets default permissions (directories: 755, files: 644)

Text Processing

  • grep "pattern" file.txt - Searches for 'pattern' in file.txt
  • grep -i "pattern" file.txt - Case-insensitive search
  • cut -d':' -f1 /etc/passwd - Extracts the first field (username) using ':' delimiter
  • sort file.txt - Sorts lines in file.txt alphabetically
  • sort file.txt | uniq - Removes duplicate lines (file must be pre-sorted)
  • wc -l file.txt - Counts number of lines in file.txt
  • tr 'a-z' 'A-Z' < file.txt - Translates all lowercase letters to uppercase
  • sed 's/foo/bar/g' file.txt - Replaces all instances of 'foo' with 'bar' in file.txt
  • awk '{print $1}' file.txt - Prints the first field (column) of each line in file.txt

Archiving and Compression

  • tar -czf archive.tar.gz dir - Creates a compressed tarball of directory 'dir' using gzip
  • tar -xzf archive.tar.gz - Extracts gzip compressed tarball
  • gzip file.txt - Compresses file.txt to file.txt.gz
  • bzip2 file.txt - Compresses file.txt to file.txt.bz2 using bzip2
  • xz file.txt - Compresses file.txt to file.txt.xz using xz

System Information

  • which bash - Shows full path of 'bash' binary
  • whereis bash - Shows locations of binary, source, and man page of 'bash'
  • locate file.txt - Quickly finds path of file.txt using an indexed database
  • updatedb - Updates the locate database

Advanced Operations

  • tee output.txt - Reads from standard input and writes to output.txt and terminal
  • xargs rm - Executes rm on the items piped into xargs

Practice Quiz

Q1: Which command shows the first 20 lines of a file?
A) less -n 20
B) tail -n 20
C) head -n 20
D) cat -20

Answer: C - head -n 20 shows the first 20 lines. Option A is wrong syntax, B shows last 20 lines, and D is invalid.

Q2: How do you search for all .conf files in /etc?
A) grep .conf /etc
B) find /etc -name "*.conf"
C) ls /etc/*.conf
D) cat /etc | grep .conf

Answer: B - find /etc -name "*.conf" searches recursively. Option A searches file contents, C only works if files are in current directory, and D is invalid approach.

Q3: Which command changes file ownership?
A) chmod
B) chown
C) mv
D) useradd

Answer: B - chown changes ownership. Option A changes permissions, C moves files, and D adds users.

Q4: What does tar -czf archive.tar.gz mydir do?
A) Compresses files using bzip2
B) Creates a gzip compressed archive of mydir
C) Creates uncompressed tar file
D) Extracts mydir from archive

Answer: B - Creates gzip compressed archive. Option A would use -j for bzip2, C would omit -z, and D would use -x.

Q5: Which command removes duplicate lines from a sorted file?
A) cut
B) uniq
C) sort
D) tr

Answer: B - uniq removes duplicate adjacent lines. Option A extracts fields, C sorts lines, and D translates characters.

Q6: What is the result of umask 022?
A) Sets file permissions to 022
B) Creates files with 644 permissions and directories with 755
C) Removes all permissions
D) Sets execute permission for all users

Answer: B - umask 022 creates files with 644 and directories with 755 permissions.

Q7: Which command follows a log file in real-time?
A) head -f logfile
B) tail -f logfile
C) cat -f logfile
D) less -f logfile

Answer: B - tail -f follows file in real-time. Other options don't have -f functionality or don't work this way.

Common Mistakes to Avoid

Using rm without caution:

  • Always double-check the path, especially with -r or -f flags
  • Use rm -i for interactive confirmation on important files
  • Remember: there's no built-in "undo" for rm operations
  • Never run rm -rf / or similar dangerous commands

Incorrect file redirection:

  • Know when to use > (overwrite), >> (append), and 2> (error redirection)
  • Understand the difference between > and | (redirection vs. piping)
  • Be careful not to redirect output to the same file you're reading from

Misusing chmod or chown:

  • Incorrect permissions can create security vulnerabilities or break system functionality
  • Understand numeric (755) vs. symbolic (u+x) permission modes
  • Don't give unnecessary permissions (especially execute on data files)

Forgetting to sort before using uniq:

  • uniq only removes adjacent duplicates
  • Always use sort | uniq for removing all duplicates

Not understanding find syntax:

  • Use quotes around patterns: find /path -name "*.txt"
  • Remember the difference between -name (filename) and -path (full path)

Conclusion

The GNU and Unix command line tools are the most important skills you will ever learn as a Linux user or administrator. They allow you to control your system, automate tasks and work efficiently across various Linux distributions. Mastering these commands requires practice and real-world application. Start with basic file operations, then progressively work through text processing, permissions, and advanced combinations. The key is to understand not just what each command does, but how they work together to solve complex problems. Take the LPIC-1 GNU and Unix Commands Quiz Now. Stay sharp. Stay certified.