Linux+ Topics & scenarios to pracice
iptables
Iptables Practice Tasks
Below are 30 tasks/scenarios designed to help you practice various iptables configurations and use cases.
-
Block a Specific IP: Create an iptables rule to block all incoming traffic from IP address
192.168.1.100
. -
SSH Access Restriction: Allow incoming SSH (port 22) connections only from the subnet
10.0.0.0/24
while denying others. -
Block Outgoing SMTP: Block outgoing traffic to port 25 (SMTP) to prevent unauthorized mail relay.
-
Allow Established Connections: Write a rule to allow all established and related incoming connections.
-
Permit Web Traffic: Allow incoming HTTP (port 80) and HTTPS (port 443) traffic.
-
Default DROP Policy: Set the default policy for the INPUT chain to DROP, ensuring only explicitly allowed traffic is accepted.
-
Log Dropped Packets: Create a rule that logs dropped packets with a custom log prefix.
-
Block a Country’s IP Range: Using an ipset (or equivalent method), block traffic from a specified country’s IP range.
-
Block Ping Requests: Drop all ICMP echo-request (ping) packets to prevent ping sweeps.
-
Selective ICMP Handling: Allow outgoing ICMP traffic but drop incoming ICMP echo requests.
-
Port Forwarding: Forward incoming TCP traffic on port 8080 to port 80 using NAT.
-
Block by MAC Address: Write a rule to block all traffic coming from a specific MAC address.
-
Rate Limiting (SYN Flood Protection): Limit the rate of new TCP connection requests from a single IP address to mitigate SYN flood attacks.
-
Drop Suspicious TCP Flag Packets: Create rules to drop packets that use unusual TCP flag combinations (e.g., for detecting Xmas or Null scans).
-
Custom Chain for Logging & Dropping: Create a new chain called
LOG_DROP
that logs and then drops packets. Then, direct matching packets to this chain. -
Save Current Rules: Demonstrate how to save the current iptables configuration to a file.
-
VPN Interface Restriction: Allow incoming traffic only if it comes via a specific VPN interface (e.g.,
tun0
). -
Redirect Traffic to a Different Port: Redirect all incoming traffic on port 8080 to port 80 (using NAT) – similar to port forwarding but with redirection.
-
Loopback Interface Traffic: Ensure that all traffic on the loopback interface (
lo
) is accepted. -
Block a Subnet: Drop all incoming traffic from a particular subnet (e.g.,
192.168.2.0/24
). -
Mark Packets for QoS: Mark packets destined for a specific port (e.g., port 80) for Quality of Service (QoS) handling.
-
Allow FTP Traffic: Allow incoming FTP traffic on port 21.
-
Allow Only Established New Connections: Allow traffic if the connection is already established; reject other new connections by default.
-
Drop Invalid Packets: Write a rule to drop packets that are marked as invalid by the connection tracking system.
-
Selective New Connection Acceptance: Accept new connections from a specific IP (e.g.,
192.168.1.50
) and drop all other new connection attempts. -
Log and Block Telnet Attempts: Log incoming Telnet (port 23) connection attempts before dropping them.
-
Enable NAT for a Local Network: Set up a NAT rule to allow machines on a local network (e.g.,
192.168.1.0/24
) to access the internet via a public interface (e.g.,eth0
). -
Block UDP Traffic from a Specific Port: Drop all UDP packets originating from a specific source port (choose any port, e.g., 12345).
-
Accept DNS Traffic: Allow incoming DNS queries (port 53) over both TCP and UDP.
-
Flush and Reset iptables: Write a set of commands to flush all iptables rules, delete any user-defined chains, and set the default policies to ACCEPT.
-
Block a Specific IP: Define rules to block all traffic from a particular IP address and log any attempts.
-
Allow HTTP/HTTPS Traffic Only: Configure rules that allow incoming traffic on ports 80 and 443 while denying all other incoming connections.
-
SSH Access Restriction: Allow SSH connections only from a specific subnet or trusted IP range.
-
Rate Limiting: Set up rules to limit the rate of incoming connections for a particular service (such as SSH or web traffic).
-
Drop ICMP Requests: Create a rule that drops or limits ICMP (ping) requests.
-
Port Forwarding with NAT: Establish a NAT rule to forward traffic from one port to another.
-
Custom Chain Creation: Develop a custom chain to handle and process specific types of traffic, then integrate that chain into your main rules.
-
Stateful Filtering: Implement rules that filter traffic based on connection states (e.g., NEW, ESTABLISHED, RELATED, INVALID).
-
Default Deny Policy: Write a script that flushes existing rules and sets a default deny policy, while adding exceptions for essential services.
-
Traffic Logging: Create rules that log suspicious or dropped packets to help analyze network activity later.
Topic 1: User & Group Management
- Create a new user account with a specified home directory and default shell.
- Set a password for the new user.
- Configure the new account so that the user must change the password upon first login.
- Create a new group and verify its entry in /etc/group.
- Add the new user to the newly created group.
- Remove an existing user from a specified group.
- Display all user accounts on the system by examining /etc/passwd.
- Display all groups present on the system.
- Modify the default shell for an existing user account.
- Lock a user account to prevent login.
- Unlock a previously locked user account.
- Remove a user account and optionally its home directory.
- Remove a group and verify that its memberships are updated.
- Verify user account details by inspecting /etc/passwd.
- Verify group information by inspecting /etc/group.
- Update a user’s details (e.g., comment field or home directory) using a command like usermod.
- Create a system account for background services.
- Write a script to batch add multiple users to a group.
- Grant sudo access to a specific user by editing the sudoers file.
- Back up the /etc/passwd and /etc/group files for recovery purposes.
Topic 2: Service Management with systemd
- Create a shell script that writes the current date and time to a log file.
- Build a systemd service unit file to execute the log script.
- Save the service unit file in /etc/systemd/system/.
- Reload the systemd daemon to acknowledge the new service.
- Enable the service so that it starts automatically at boot.
- Manually start the service and verify its operation.
- Use systemctl to check the service status and review its logs.
- Develop a systemd timer unit that triggers the service on a set schedule (e.g., hourly).
- Enable and start the timer unit.
- Check the timer’s status and scheduled activations.
- Modify the service unit to run under a specific user and group.
- Configure the service to automatically restart on failure.
- Set a working directory for the service in the unit file.
- Configure custom environment variables for the service.
- Establish a dependency between this service and another systemd unit.
- Create a custom systemd target and assign your service to it.
- Simulate a script failure to observe the restart behavior.
- Disable both the service and its timer.
- Write a separate script that monitors and restarts the service if it stops unexpectedly.
- Document all changes made to the service and timer configuration.
Topic 3: Scheduling Tasks with Cron
- Write a script that backs up a designated directory.
- Schedule the backup script to run daily at midnight using cron.
- Edit the current user’s crontab using the proper command.
- Define environment variables within the crontab file.
- Modify a cron job to redirect its output to a specific log file.
- Schedule a task to run every 15 minutes.
- Create a cron job that runs only on Saturdays and Sundays.
- Delete an existing cron job from the crontab.
- List all current cron jobs for the user.
- Set up a cron job to run at system startup using the @reboot directive.
- Schedule a cron job for another user (with appropriate privileges).
- Configure a cron job to run at a specified date and time.
- Set a cron job to update system packages weekly.
- Create a cron job that removes old files from a temporary directory.
- Schedule a job that monitors disk usage and logs the output.
- Configure a cron job to run with a random delay to avoid simultaneous execution.
- Set up a cron job that sends email notifications upon task completion.
- Verify that a cron job is executing correctly by examining log outputs.
- Modify the timing of an already scheduled cron job.
- Export and back up your crontab configuration to a file.
Topic 4: File Permissions and Ownership
- Create a new directory and a file within it.
- Change a file’s permissions to 644 using both symbolic and numeric methods.
- Adjust a directory’s permissions to 755.
- Use chown to change the file owner to another user.
- Modify a file’s group using chgrp or chown.
- Recursively set permissions for all files in a directory.
- Change permissions on a file using symbolic notation (e.g., u+x).
- Use numeric (octal) notation to modify file permissions.
- Create or modify an executable file to set the setuid bit.
- Create a shared directory and apply the sticky bit.
- Apply an Access Control List to grant a specific user additional permissions on a file.
- Verify the ACL settings on a file using getfacl.
- Remove an ACL entry from a file using setfacl.
- Write a script to change group ownership on multiple files.
- Set and test the default umask for new files and directories.
- Use ls -l to document permissions before and after changes.
- Create a script that resets file permissions to a predefined standard.
- Create a file with no permissions and then restore default permissions.
- Adjust group permissions to allow execution on a file.
- Write a script to back up current permissions and restore them when needed.
Topic 5: Process Management
- Use ps to display all running processes.
- Launch top or htop to monitor system processes in real time.
- Use ps or pgrep to find a process by name and note its PID.
- Gracefully terminate a process using the SIGTERM signal.
- Force terminate a process with SIGKILL if it does not respond to SIGTERM.
- Write a script to monitor a specific process’s CPU usage over time.
- Create a script that tracks a process’s memory consumption.
- Utilize pgrep to find processes matching a specific pattern.
- Use pkill to terminate all processes matching a given name.
- Start a process in the background and verify it is running.
- Bring a background process to the foreground using job control commands.
- Suspend a process (using Ctrl+Z) and then resume it with fg or bg.
- Use pstree to visualize parent-child relationships among processes.
- Run a long‑running process with nohup to ensure it continues after logout.
- Change the niceness value of a running process using renice.
- Use htop to interactively manage and monitor processes.
- Create a script that logs CPU and memory usage of a process over time.
- Set up a mechanism (using cron or systemd) to automatically restart a specific process if it stops.
- Configure a systemd service to manage a long‑running process.
- Document overall process resource usage and create a summary report.
Topic 6: System Logging Configuration
- Configure rsyslog to write specific log messages to a custom log file.
- Develop a new rsyslog configuration file for filtering certain logs.
- Restart the rsyslog service to apply configuration changes.
- Set up a filter in rsyslog to log messages of a specific severity to a separate file.
- Configure rsyslog to forward selected log messages to a remote log server.
- Create a logrotate configuration for your custom log file.
- Use the logger command to generate a test log message and verify its destination.
- Send a test message via logger and check its logging.
- Use journalctl to view logs from the last hour for a particular service.
- Export filtered journal logs to a file for offline analysis.
- Modify the rsyslog configuration to change the default log file location for a facility.
- Activate debug logging in rsyslog and verify increased verbosity.
- Back up the original rsyslog configuration file before making changes.
- Examine the system log files for common error messages and document your findings.
- Configure logrotate to manage log retention and file size limits.
- Adjust file permissions on log files to enhance security.
- Create a script to monitor log file sizes and send alerts when they grow beyond a threshold.
- Write a script to archive and compress logs older than a specified date.
- Set up a centralized logging solution on your VM.
- Record every change made to the logging configuration and maintain a backup copy of config files.
Topic 7: Disk & Partition Management
- Use fdisk -l (or a similar command) to list all available disks and partitions.
- Use fdisk or parted to create a new partition on a virtual disk.
- Format the new partition with a filesystem (e.g., ext4).
- Manually mount the new partition to a designated directory.
- Add an entry in /etc/fstab to mount the partition automatically at boot.
- Initialize a new partition table on a virtual disk.
- Resize an existing partition using parted or another tool.
- Create a new physical volume, add it to a volume group, and create a logical volume.
- Format the logical volume with an appropriate filesystem.
- Mount the logical volume and update /etc/fstab accordingly.
- Create and activate a swap file or partition.
- Use df to display disk space usage for all mounted filesystems.
- Use du to determine the disk usage of specific directories.
- Set up a RAID configuration using mdadm on multiple partitions.
- Test the RAID configuration by simulating a disk failure scenario.
- Use tools like sfdisk to back up the current partition table.
- Practice restoring a partition table from a backup file.
- Modify the label of a partition using a tool like e2label.
- Perform a filesystem integrity check using fsck.
- Develop a script that monitors disk usage and sends alerts when thresholds are exceeded.
Topic 8: Network Configuration
- Use ip addr (or ifconfig) to list network interfaces and their statuses.
- Configure a network interface with a static IP address.
- Update /etc/resolv.conf to change DNS server settings.
- Restart networking services to apply configuration changes.
- Use ping to verify connectivity to an external host.
- Display the current routing table using ip route or route.
- Add a secondary IP address to an existing network interface.
- Reconfigure a network interface to use DHCP for dynamic IP assignment.
- Implement a basic firewall rule with iptables (or nftables) to block a specific port.
- Use netstat or ss to display active network connections.
- Use traceroute (or tracepath) to diagnose network path issues.
- Adjust NetworkManager settings for a network interface if applicable.
- Establish a VPN connection using command‑line tools and configuration files.
- Verify DNS functionality using nslookup or dig.
- Add a custom routing rule for specific traffic.
- Use iftop to observe real‑time network bandwidth usage.
- Write a script that checks and logs network connectivity at intervals.
- Set up a mechanism to automatically switch network configurations upon failure.
- Record every change made to network settings and back up configuration files.
- Develop a script that resets network settings automatically if connectivity is lost.
Topic 9: Package Management
- Refresh the package index using your distro’s package manager (e.g., apt-get update or yum update).
- Perform a full system upgrade.
- Install a new package from the repositories.
- Uninstall an installed package and verify its removal.
- Use a package search command (e.g., apt-cache search or yum search) to find a package.
- List installed packages to confirm a package’s presence.
- Download a package without immediately installing it.
- Install a package from a local .deb (or equivalent) file.
- Resolve and fix broken dependencies using appropriate package manager commands.
- Clear the package cache to free up space.
- Generate a complete list of available packages.
- Add a new repository or PPA (if using Ubuntu/Debian).
- Remove or disable an added repository from the configuration.
- Lock a package version to prevent automatic upgrades.
- Downgrade a package to an earlier version and verify compatibility.
- Apply security updates only and document the process.
- Set up unattended upgrades for automated security patches.
- Display detailed information about a package using the appropriate commands.
- Check for available package updates.
- Document and back up package management logs and history for future reference.
Topic 10: System Resource Monitoring
- Use top to observe real‑time CPU and memory usage.
- Install and run htop to monitor system resources interactively.
- Display current memory usage using the free command.
- Use vmstat to obtain system performance metrics.
- Install and run iostat to monitor disk input/output activity.
- Write a script to log system resource usage (CPU, memory) at regular intervals.
- Install and configure sysstat to use sar for detailed performance tracking.
- Use mpstat to create a CPU usage report and save it to a file.
- Use iftop to monitor real‑time network bandwidth usage.
- Use the uptime command to display load averages.
- Use dstat to display multiple system resource statistics simultaneously.
- Verify swap usage using free or swapon -s.
- Configure monitoring tools to alert when CPU or memory usage exceeds a defined threshold.
- Create a cron job that runs your resource monitoring script at set intervals.
- Redirect the output of your monitoring script to a log file for historical analysis.
- Use available tools (e.g., GNUPlot) to create a graphical report of resource usage.
- Capture and compare resource usage data between two time intervals.
- Write a script to monitor and log resource usage for a specific process.
- Create a summary report of system resource data over a period.
- Outline and implement a comprehensive plan for ongoing system performance monitoring.
SELinux Hands-On Practice Tasks for Linux+ Exam Preparation
Below are 60 practical tasks to help you learn and work with SELinux on a Linux machine. Each task is designed to be performed directly on your system.
- Check SELinux Status: Run the command to display the current SELinux mode and active policy.
- Switch to Permissive Mode Temporarily: Change SELinux mode to permissive using the appropriate command.
- Switch Back to Enforcing Mode: Revert SELinux back to enforcing mode.
- Display File Security Context: Use a command to display the SELinux context of a common file (e.g.,
/etc/passwd
). - Change File Context Using chcon: Create a test file and modify its SELinux context using
chcon
. - Restore File Context to Default: Reset the test file’s context to its default value using the proper restoration command.
- List All SELinux Booleans: Display all available SELinux booleans on your system.
- Temporarily Modify an SELinux Boolean: Change a boolean (for example,
httpd_enable_homedirs
) without making a permanent change. - Permanently Modify an SELinux Boolean: Change an SELinux boolean permanently using the appropriate flag.
- Add a New File Context Rule: Use
semanage fcontext
to add a file context rule for a test directory. - Apply the New File Context Rule: Run the command that applies the new file context rule to the test directory.
- Check Active SELinux Policies: Verify which SELinux policy is active (e.g., targeted or strict).
- List Running Processes with SELinux Contexts: Display all running processes along with their SELinux contexts.
- Inspect a Specific Process Context: Identify a process (e.g.,
sshd
) and check its SELinux context (via/proc/<pid>/attr/current
or similar). - Map a Linux User to an SELinux User: Create a mapping between a Linux user and an SELinux user using the appropriate command.
- List SELinux User Mappings: Display the current SELinux user mappings.
- Generate an SELinux Audit Report: Use the SELinux audit tool to generate a human-readable audit report.
- Filter SELinux Audit Messages: Filter recent SELinux audit log entries using
ausearch
or a similar command. - Generate a Custom Policy Module: Simulate a denial and use
audit2allow
to generate a custom SELinux policy module. - Load the Custom Policy Module: Install the module generated in Task 19 using the SELinux module loader.
- Remove an SELinux Module: Uninstall the custom module using the appropriate command.
- Trigger a Full Filesystem Relabel: Create the necessary file to trigger a full filesystem relabel on the next reboot.
- Create a Test File and Change Its Context: Create a test file (e.g., in
/tmp
) and manually assign a new SELinux context. - Verify the File Context Change: Confirm that the test file’s context has been changed.
- List Installed SELinux Modules: Display a list of all currently installed SELinux modules.
- Adjust a Boolean Affecting a Service: Modify a boolean (e.g.,
httpd_can_network_connect
) that impacts a service, then test the service. - Edit the SELinux Configuration File: Open and modify
/etc/selinux/config
to change SELinux mode settings. - Reboot to Apply Configuration Changes: Reboot the system and verify that the changes in SELinux mode are applied.
- Add a New Port Context: Use
semanage port
to add a context for a new TCP port (choose an available port). - Verify the New Port Context: List port contexts to confirm that your new port has been added.
- Confirm Targeted Policy Usage: Ensure that your system is using the targeted policy by checking the configuration.
- Identify an Unconfined Process: Use process listing commands to find processes running with an unconfined SELinux context.
- Simulate a Denial Scenario: Create a situation (e.g., incorrect file context for a service) that triggers an SELinux denial and inspect the audit log.
- Use audit2allow on Denial Logs: Process the denial logs with
audit2allow
to generate a sample policy rule. - Load the Generated Policy Rule: Install the custom policy module from Task 34 and verify that the denial is resolved.
- Compare System Behavior with SELinux Disabled: Temporarily disable SELinux and note differences in system behavior (be cautious and revert this change).
- Inspect a Web Server Directory: Check the SELinux file contexts in a web server directory (e.g.,
/var/www/html
). - Change a Web Directory Context: Modify the security context of the web server directory using
chcon
and verify the change. - Create a Custom File Labeling Policy: Use
semanage fcontext
to define a custom labeling rule for a new directory. - Apply and Verify the Custom Labeling Policy: Run the restoration command on the directory and confirm the applied labels.
- Recursively List File Contexts: Use a command to list SELinux contexts for all files in a directory (e.g.,
/var/www
). - Simulate a Process Context Modification: If applicable, simulate changing a process’s context (e.g., via startup options) and check the result.
- Quickly Check the Current Mode: Use
getenforce
to display the current SELinux mode. - Toggle and Verify an SELinux Boolean: Change a boolean’s state and then confirm its value with the appropriate query command.
- List Detailed SELinux Policy Modules: Run a command that lists policy modules with detailed information.
- Simulate a Service Denial: Attempt to start a service that SELinux restricts, then review the resulting denial in the audit log.
- Reset a Misconfigured File Context: Create a file with an incorrect context and then use the restoration command to reset it.
- Practice Changing Modes with setenforce: Toggle SELinux modes using
setenforce
and verify each change. - Inspect Network Port Contexts: List all network port contexts to understand which ports are associated with which types.
- Modify and Revert a File Context: Change the context of a non-critical file, then revert it back to the default.
- Verify a Service’s SELinux Context: Check that a service (e.g.,
nginx
orsshd
) is running with the correct context using process listing. - Investigate SELinux Log Errors: Use audit tools to examine SELinux log entries for errors and document your findings.
- Create a Custom Policy Rule for an Application: Simulate an application denial and generate a custom rule using
audit2allow
. - Compare Targeted vs. Strict Policies: Research and document differences between targeted and strict SELinux policies using command outputs and file inspection.
- Explore SELinux Policy Files: Navigate to
/etc/selinux
and review the available policy files and directories. - Backup Current SELinux Modules: Create a backup (or log) of the list of installed SELinux modules.
- Simulate and Resolve a File Access Block: Create a scenario where file access is blocked by SELinux and resolve it using appropriate context commands.
- Test File Access with SELinux Enforced vs. Permissive: Compare file access behavior in enforcing mode versus permissive mode.
- Configure SELinux for a Container: If you have Docker or another container engine, launch a container with SELinux enabled and verify its file contexts.
- Document Your SELinux Changes: Create and maintain a log file documenting all SELinux configuration changes made during these tasks.
Text Manipulation Tool Tasks
tr
- Convert all uppercase letters in a file to lowercase using
tr
. - Convert all lowercase letters in a file to uppercase using
tr
. - Replace all spaces with underscores using
tr
. - Delete all digits from the input using
tr
. - Replace newline characters with commas using
tr
. - Replace all vowels with
*
usingtr
. - Delete all punctuation marks using
tr
. - Replace all occurrences of the letter “a” with “z” using
tr
. - Delete all non‐alphabetic characters using
tr
. - Squeeze multiple spaces into a single space using
tr -s
. - Replace tab characters with four spaces using
tr
. - Delete all instances of the character “e” using
tr
. - Replace all digits with “#” using
tr
. - Remove all control characters using
tr -d
(with an appropriate set). - Translate the set
[abc]
to[xyz]
usingtr
. - Squeeze repeated alphabetic characters using
tr -s
(applied to a set). - Perform ROT13 encoding using
tr
. - Remove all vowels from a file using
tr -d
. - Replace the vowels “aeiou” with “@@@@@” using
tr
. - Replace all punctuation with a space using
tr
. - Convert carriage returns (CR) to line feeds (LF) using
tr
. - Replace a specific symbol (e.g. “$”) with another (e.g. “%”) using
tr
. - Delete all occurrences of the letter “x” using
tr
. - Replace lowercase letters from “m” through “z” with their uppercase equivalents using
tr
. - Remove characters outside the printable ASCII range using
tr -cd
. - Map the characters “ABC” to “123” using
tr
. - Squeeze multiple exclamation marks into one using
tr -s
. - Replace newline characters with a space using
tr
. - Replace underscores with hyphens using
tr
. - Remove all non‐numeric characters using
tr -cd
. - Replace multiple punctuation marks with a single period using
tr
(possibly in two steps). - Change tab characters to a single space using
tr
. - Delete all non‐alphanumeric characters using
tr -cd
. - Remove extra white space by squeezing spaces using
tr -s
. - Convert CRLF to LF by deleting carriage returns using
tr -d
. - Substitute a range of characters with a given symbol using
tr
(e.g. map all a–z to “*”). - Remove vowels and punctuation simultaneously using
tr -d
. - Replace multiple spaces with a single underscore using
tr
(using a pipeline if needed). - Remove all non‐ASCII characters using
tr -cd
. - Convert each line of a file to lowercase using
tr
. - Delete all dash (“-”) characters using
tr -d
. - Remove all occurrences of the letter “b” using
tr -d
. - Delete both digits and punctuation using
tr -d '0-9[:punct:]'
. - Squeeze repeated digits into a single digit using
tr -s
. - Map hexadecimal letters (a–f) to uppercase using
tr
. - Replace newline characters with semicolons using
tr
. - Delete all non‐printable characters using
tr -cd '[:print:]'
. - Translate the set “123456” to “abcdef” using
tr
. - Delete a range of characters (e.g. from “p” to “t”) using
tr -d
. - Map one character set to another to simulate encoding conversion using
tr
.
cut
- Extract the first comma-separated field from each line using
cut -d',' -f1
. - Extract the second tab-delimited field using
cut -d$'\t' -f2
. - Extract characters 1–5 from each line using
cut -c1-5
. - Extract fixed-width columns from a file using
cut -c…
(specify column ranges as needed). - Extract the first 10 characters from each line using
cut -c1-10
. - Extract the first colon-separated field using
cut -d':' -f1
. - Extract the last field of each line (assume a known number of fields) using
cut
. - Extract fields separated by spaces using
cut -d' '
. - Extract bytes 1–3 from each line using
cut -b1-3
. - Extract a specific range of characters using
cut -c
with custom positions. - Extract the first word from each line using
cut -d' ' -f1
. - Extract all fields except the first using
cut --complement
. - Remove the first field from a colon-separated file using
cut --complement -d':' -f1
. - Extract multiple fields (e.g. fields 2 and 3) using
cut -d',' -f2,3
. - Extract a date field from a log file using
cut
with the appropriate delimiter. - Extract and display a specific field for further processing using
cut
. - Use
cut
to extract fields when the delimiter is a semicolon using-d';'
. - Extract a substring from each line using
cut -c
with the proper range. - Extract the first field from a pipe-delimited file using
cut -d'|' -f1
. - Extract the second and third fields using
cut -d',' -f2-3
. - Extract fields from a CSV file (assuming no embedded commas) using
cut -d','
. - Extract fields from a tab-delimited file using
cut -f
(the default delimiter is tab). - Remove a specific field from each line using
cut --complement
with the proper delimiter. - Extract the first 2 bytes of each line using
cut -b1-2
. - Extract a specific range of bytes using
cut -b
with the desired positions. - Extract the username field from
/etc/passwd
usingcut -d':' -f1
. - Extract the home directory from
/etc/passwd
usingcut -d':' -f6
. - Extract the shell field from
/etc/passwd
usingcut -d':' -f7
. - Extract the domain name from an email address field using
cut -d'@' -f2
. - Extract the file extension from filenames using
cut -d'.' -f2
. - Extract the first word from each line (space-delimited) using
cut -d' ' -f1
. - Extract a middle substring from each line using
cut -c5-15
. - Extract the last N characters from each line using a pipeline (e.g. with
rev
andcut
). - Extract a specific field when lines use a complex delimiter (e.g.
cut -d'|' -f2
). - Combine multiple fields into one output using
cut -d',' -f1,3
. - Extract log file columns using
cut -d' '
(with appropriate field numbers). - Extract the third field from a space-delimited log file using
cut -d' ' -f3
. - Extract a fixed column range from each line using
cut -c10-20
. - Extract specific fields from a colon-separated configuration file using
cut -d':'
. - Extract the second field from a colon-separated file using
cut -d':' -f2
. - Remove the first three characters from each line using
cut -c4-
. - Extract fixed-width segments from each line using
cut -c
with proper ranges. - Extract a substring from each line using
cut -c
(choose start–end positions). - Remove a prefix from each line by cutting off a fixed number of characters using
cut -c
. - Extract fields from a semicolon-separated file using
cut -d';'
. - Extract a specific byte range (e.g. 5–10) using
cut -b5-10
. - Extract fields from a pipe-delimited file using
cut -d'|'
. - Output specific columns from a text file using
cut
(choose delimiter and fields). - Extract fields even if some lines have missing delimiters using
cut
. - Extract a field and combine it with sort (demonstrating pipelining) using
cut
piped tosort
.
nl
- Number all lines in a file using
nl file.txt
. - Number non-empty lines only using
nl -b a file.txt
. - Number lines starting from 10 using
nl -v 10 file.txt
. - Number lines with an increment of 5 using
nl -i 5 file.txt
. - Number lines with a custom format using
nl -n rz file.txt
. - Number lines and pad with zeros using
nl -n rz file.txt
. - Skip blank lines while numbering using
nl -b t file.txt
. - Number lines with a custom delimiter using
nl -s ': ' file.txt
. - Use a custom numbering format (e.g. left justified) with
nl
options. - Number lines of a log file using
nl logfile.txt
. - Add line numbers to a script file using
nl script.sh
. - Number lines and (simulate) print a header using
nl file.txt
. - Format numbering with a custom width using
nl -w 3 file.txt
. - Number lines with a colon separator using
nl -s ': ' file.txt
. - Number lines using an alternate numbering style with
nl -n ln file.txt
. - Number lines using a custom starting value using
nl -v 1 file.txt
. - Remove any preexisting numbers by re‐numbering using
nl file.txt
. - Start numbering at 100 using
nl -v 100 file.txt
. - Right justify line numbers using
nl -n r file.txt
. - Left justify line numbers using
nl -n l file.txt
. - Number lines with fixed width using
nl -w 4 file.txt
. - Redirect the numbered output to a new file using
nl file.txt > numbered.txt
. - Number lines read from standard input using
cat file.txt | nl
. - Number lines and write the result to another file using
nl file.txt > output.txt
. - Ignore leading whitespace and number lines using
nl -b p file.txt
. - Insert a delimiter between line numbers and text using
nl -s ' | ' file.txt
. - (Simulate) Number lines with a custom prefix using
nl
with formatting. - Number lines in a file that contains blank lines using
nl -b t file.txt
. - (Simulate) Add a suffix to line numbers using
nl
(combine with other tools). - Combine
grep
andnl
to number only matching lines. - Use
nl
to number lines for diff comparison. - (Simulate) Number paragraphs in a text file using
nl
(may need preprocessing). - (Simulate) Number only lines that contain a specific pattern (pre-filter then nl).
- Number lines with a dash as a separator using
nl -s ' - ' file.txt
. - (Simulate) Add a custom prefix to line numbers using formatting.
- Number lines with a custom column width using
nl -w 5 file.txt
. - Remove extra spaces (preprocess) and then number lines using
nl
. - (Simulate) Number lines with a hash prefix using
nl
(combine with sed). - Start numbering at 100 using
nl -v 100 file.txt
. - Number lines in a multi‐column file using
nl
. - Number lines from piped input using
cat file.txt | nl
. - (Simulate) Number lines in reverse order by combining
tac
andnl
. - Number lines with indentation using appropriate
nl
formatting. - (Simulate) Append trailing line numbers (combine with paste).
- (Simulate) Use a custom fill character (with sed) after numbering.
- Skip comment lines (pre-filter then nl) and number the rest.
- Number lines in a file with leading spaces using
nl -b t file.txt
. - Merge the original file with numbered output using
paste
. - Combine
nl
withsort
to sort the numbered lines. - Use
nl
to number lines and then compare with another file’s numbered output.
od
- Display a file in octal format using
od -o file.txt
. - Display file contents in hexadecimal using
od -x file.txt
. - Display file contents in decimal using
od -d file.txt
. - Display file contents as ASCII characters using
od -c file.txt
. - Display the first 16 bytes of a file using
od -N 16 file.txt
. - Display file contents in “binary” (simulate using hex output) with
od -t x1 file.txt
. - Set the number of bytes per line using
od -w16 file.txt
. - Display file offsets using
od -An file.txt
. - Display file contents in canonical format using
od -c file.txt
. - Display both hex and ASCII using
od -Ax -tx1z file.txt
. - (Simulate) Display in little-endian order using appropriate options.
- Use a custom format specifier with
od -t o2 file.txt
. - Display file contents with address offsets using
od -A x file.txt
. - Start displaying a file from a given offset using
od -j 10 file.txt
. - (Simulate) Display in octal and decimal simultaneously using two commands.
- Set the number of columns using
od -w8 file.txt
. - Group bytes in hex with two-byte groups using
od -t x2 file.txt
. - Skip a specified number of bytes using
od -j 20 file.txt
. - Limit output to 32 bytes using
od -N 32 file.txt
. - Display in octal with canonical representation using
od -c file.txt
. - Use a custom format string with the
-t
option inod
. - Show ASCII alongside hex using
od -tx1z file.txt
. - Dump a binary file in hex using
od -A x -t x1 file.bin
. - Dump a binary file in octal using
od -A o -t o1 file.bin
. - Output file contents in blocks (64 bytes per block) using
od -N 64 file.txt
. - Display only the header bytes (first 8 bytes) using
od -N 8 file.txt
. - Group output in two-byte blocks using
od -t x2 file.txt
. - Show decimal values for each byte using
od -t d1 file.txt
. - (Simulate) Display hex values with a delimiter (preprocess if needed).
- Display octal output with addresses using
od -A x -t o1 file.txt
. - (Combine) Hex and octal output by running two od commands.
- Use a custom format specifier (e.g.
od -t x4 file.txt
). - Display hex output with decimal offsets using
od -A d -t x1 file.txt
. - Display octal output with 8 bytes per line using
od -w8 -t o1 file.txt
. - Display hex output with 16 bytes per line using
od -w16 -t x1 file.txt
. - Combine hex and ASCII output using
od -tx1z file.txt
. - Start output from a specific offset using
od -j 5 file.txt
. - Format output in a structured style using
od
(with appropriate options). - Ensure the output ends with a newline (default behavior).
- Skip an initial header using
od -j 10 file.txt
. - Display hex without addresses using
od -An -t x1 file.txt
. - Group output by 2 bytes using
od -t x2 file.txt
. - (Combine) Hex and octal options as needed.
- Redirect output to another file using
od file.txt > dump.txt
. - Set a custom block size using
od -N 32 file.txt
. - Display in different numeral bases by changing the
-t
option. - Set a custom column width using
od -w8 file.txt
. - Output in a compact format using
od -v file.txt
. - Use custom formatting flags with
od
as needed. - Display file in hex with offset in hex using
od -Ax -tx1 file.txt
.
sed
- Replace the first occurrence of “foo” with “bar” using:
sed 's/foo/bar/' file.txt
- Replace all occurrences of “cat” with “dog” using:
sed 's/cat/dog/g' file.txt
- Delete lines containing “error” using:
sed '/error/d' file.txt
- Insert a line “Header” before lines matching “START” using:
sed '/START/i Header' file.txt
- Append a line “Footer” after lines matching “END” using:
sed '/END/a Footer' file.txt
- Perform an in-place substitution of “old” with “new” using:
sed -i 's/old/new/g' file.txt
- Print only lines containing “success” using:
sed -n '/success/p' file.txt
- Perform multiple substitutions in one command using:
sed -e 's/foo/bar/g' -e 's/baz/qux/g' file.txt
- Replace “apple” with “orange” only on line 3 using:
sed '3s/apple/orange/' file.txt
- Delete all blank lines using:
sed '/^$/d' file.txt
- Replace all digits with “#” using:
sed 's/[0-9]/#/g' file.txt
- Insert “Start of File” at the beginning using:
sed '1i Start of File' file.txt
- Append “End of File” at the end using:
sed '$a End of File' file.txt
- Change the delimiter (using
|
instead of/
) with:sed 's|foo|bar|g' file.txt
- Replace “test” with “exam” ignoring case using GNU sed:
sed 's/test/exam/Ig' file.txt
- Print matching lines with their line numbers using a two‑step command:
sed -n '=' file.txt | sed 'N;s/\n/ /'
- Remove leading and trailing whitespace using:
sed 's/^[ \t]*//;s/[ \t]*$//' file.txt
- Duplicate each line using:
sed 'p' file.txt
- Delete the first line using:
sed '1d' file.txt
- Delete the last line using:
sed '$d' file.txt
- Replace “foobar” with “foobaz” using a captured group with:
sed 's/\(foo\)bar/\1baz/' file.txt
- Perform a global substitution of “old” with “new” using:
sed 's/old/new/g' file.txt
- Insert a blank line after each line using:
sed 'G' file.txt
- Comment out lines containing “DEBUG” using:
sed '/DEBUG/s/^/# /' file.txt
- Uncomment lines starting with “#” using:
sed 's/^# //' file.txt
- Swap the words “first” and “second” using:
sed 's/\(first\)\(.*\)\(second\)/\3\2\1/' file.txt
- Remove duplicate spaces using:
sed 's/ */ /g' file.txt
- Add line numbers to each line using:
sed = file.txt | sed 'N;s/\n/\t/'
- Replace tab characters with four spaces using:
sed 's/\t/ /g' file.txt
- Change the case of “hello” to uppercase using GNU sed:
sed 's/hello/\U&/g' file.txt
- Extract a substring (matching “pattern”) using:
sed -n 's/.*\(pattern\).*/\1/p' file.txt
- Replace punctuation with “~” using:
sed 's/[[:punct:]]/~/g' file.txt
- Insert “Line Start” at line 5 using:
sed '5i Line Start' file.txt
- Remove all digits using:
sed 's/[0-9]//g' file.txt
- Replace multiple spaces with a single space using:
sed 's/ \{2,\}/ /g' file.txt
- Delete lines starting with a digit using:
sed '/^[0-9]/d' file.txt
- Convert a CSV file by replacing commas with semicolons using:
sed 's/,/;/g' file.csv
- Remove leading whitespace using:
sed 's/^[ \t]*//' file.txt
- Delete lines that do not contain “INFO” using:
sed '/INFO/!d' file.txt
- Replace only the second occurrence of “foo” per line (using a more complex script).
- Append a suffix “ – end” to each line using:
sed 's/$/ -- end/' file.txt
- Remove the suffix “.txt” from filenames using:
sed 's/\.txt$//' file.txt
- Add a prefix “START: ” to each line using:
sed 's/^/START: /' file.txt
- Remove the prefix “DEBUG: ” from each line using:
sed 's/^DEBUG: //' file.txt
- Enclose the word “error” in brackets using:
sed 's/error/[error]/g' file.txt
- Replace “begin” with “start” only at the beginning of a line using:
sed 's/^begin/start/' file.txt
- Replace “end” with “finish” only at the end of a line using:
sed 's/end$/finish/' file.txt
- Merge two consecutive lines into one using:
sed 'N;s/\n/ /' file.txt
- Split a long line at each comma (inserting a newline) using:
sed 's/,/,\n/g' file.txt
- Remove all non-alphanumeric characters using:
sed 's/[^a-zA-Z0-9]//g' file.txt
awk
- Print the first field of each line using:
awk '{print $1}' file.txt
- Print the second field using:
awk '{print $2}' file.txt
- Print lines where the third field is greater than 100 using:
awk '$3 > 100' file.txt
- Sum the values in the second column using:
awk '{sum+=$2} END {print sum}' file.txt
- Calculate the average of the first column using:
awk '{sum+=$1; count++} END {print sum/count}' file.txt
- Print the last field of each line using:
awk '{print $NF}' file.txt
- Print only lines matching “success” using:
awk '/success/' file.txt
- Specify a comma as the field separator using:
awk -F, '{print $1}' file.txt
- Print a header then the file contents using:
awk 'BEGIN {print "Header"} {print}' file.txt
- Print line numbers along with each line using:
awk '{print NR, $0}' file.txt
- Count the number of lines using:
awk 'END {print NR}' file.txt
- Print lines where the first field matches a regex using:
awk '$1 ~ /pattern/' file.txt
- Replace the second field with a computed value using:
awk '{$2 = $2 * 2; print}' file.txt
- Reformat a CSV file using:
awk -F, '{print $1 " - " $2}' file.csv
- Print the first and third fields using:
awk '{print $1, $3}' file.txt
- Print fields in reverse order using:
awk '{for(i=NF;i>0;i--) printf $i " "; print ""}' file.txt
- Print fields separated by a colon using:
awk 'BEGIN {OFS=":"} {print $1, $2}' file.txt
- Filter lines based on multiple conditions using:
awk '$1=="foo" && $2>50' file.txt
- Print only non-empty lines using:
awk 'NF' file.txt
- Print the sum and average of a numeric column using:
awk '{sum+=$2; count++} END {print sum, sum/count}' file.txt
- Print numbers with two‑decimal formatting using:
awk '{printf "%.2f\n", $1}' file.txt
- Count occurrences of “error” using:
awk '/error/ {count++} END {print count}' file.txt
- Print lines conditionally using:
awk '{if($1=="foo") print $0}' file.txt
- Process a tab-delimited file using:
awk -F'\t' '{print $1}' file.txt
- Print only unique lines using:
awk '!seen[$0]++' file.txt
- Perform arithmetic on fields using:
awk '{print $1 * $2}' file.txt
- Concatenate the first two fields with a hyphen using:
awk '{print $1 "-" $2}' file.txt
- Print fields with fixed width using:
awk '{printf "%-10s %-10s\n", $1, $2}' file.txt
- Extract a column from a log file using:
awk '{print $3}' file.txt
- (Simulate) Format dates from a file using an appropriate awk script.
- Count words in a file using:
awk '{ total += NF } END { print total }' file.txt
- Print only the header row of a CSV file using:
awk 'NR==1 {print}' file.csv
- Skip the header row and process the rest using:
awk 'NR>1 {print}' file.csv
- Replace commas with spaces using:
awk '{gsub(/,/, " "); print}' file.txt
- Print lines where the number of fields equals 5 using:
awk 'NF==5' file.txt
- Print the first two columns separated by a colon using:
awk '{print $1 ":" $2}' file.txt
- Multiply the first and second fields and print the result using:
awk '{print $1 * $2}' file.txt
- Print a field as a percentage using:
awk '{printf "%.2f%%\n", $1}' file.txt
- Convert a field to uppercase using:
awk '{print toupper($1)}' file.txt
- Convert a field to lowercase using:
awk '{print tolower($1)}' file.txt
- Compute a running total for a column using:
awk '{sum+=$1; print sum}' file.txt
- Print rows where the first field is empty using:
awk '$1==""' file.txt
- Print the maximum value in the first column using:
awk 'BEGIN {max=0} {if($1>max) max=$1} END {print max}' file.txt
- Print the minimum value in the first column using:
awk 'BEGIN {min=999999} {if($1<min) min=$1} END {print min}' file.txt
- Print lines where the second field equals a specific value using:
awk '$2=="value"' file.txt
- Print a formatted report using:
awk '{printf "Field1: %s, Field2: %s\n", $1, $2}' file.txt
- Output data in JSON format (simulated) using:
awk 'BEGIN {print "["} {print "{\"field\":\"" $1 "\"},"} END {print "]"}' file.txt
- Process a file and save the output using:
awk '{print $1}' file.txt > output.txt
- Print the second field from a colon-separated file using:
awk -F: '{print $2}' file.txt
- Process a file and perform multiple actions using:
awk '{print $1; print $2}' file.txt
sort
- Sort a file alphabetically using:
sort file.txt
- Sort a file in reverse order using:
sort -r file.txt
- Sort a file numerically using:
sort -n file.txt
- Sort a file by the second field using:
sort -k2 file.txt
- Sort a CSV file by the third column using:
sort -t',' -k3 file.csv
- Sort lines ignoring case using:
sort -f file.txt
- Sort lines by length (using awk to prepend length) using:
awk '{print length, $0}' file.txt | sort -n | cut -d" " -f2-
- Sort a file and remove duplicate lines using:
sort -u file.txt
- Sort a file with a custom delimiter using:
sort -t':' -k2 file.txt
- Sort a file based on a numeric field using:
sort -t',' -k2n file.txt
- Sort lines then reverse the order using:
sort file.txt | sort -r
- Sort in human-numeric order using:
sort -h file.txt
- Sort a tab-delimited file by the third column using:
sort -t$'\t' -k3 file.txt
- Sort version numbers using:
sort -V file.txt
- Sort by multiple keys using:
sort -k1,1 -k2,2 file.txt
- (Simulate) Sort lines based on the last field by first extracting it.
- Sort lines while preserving the order of duplicates using:
sort -s file.txt
- Sort a file with blank lines at the end using:
sort file.txt
- Sort a file and output only unique lines using:
sort -u file.txt
- Sort a file ignoring leading blanks using:
sort -b file.txt
- Sort in parallel (if supported) using:
sort --parallel=4 file.txt
- Write sorted output to a new file using:
sort file.txt -o sorted.txt
- Check if a file is already sorted using:
sort -c file.txt
- Use a temporary directory with sort using:
sort -T /tmp file.txt
- Sort a file with mixed numeric and alphabetic keys (use appropriate options).
- Sort a file containing dates using:
sort -k1,1M file.txt
- Sort by month name order using:
sort -k1,1M file.txt
- Sort ignoring case and numerically using:
sort -f -n file.txt
- (Simulate) Sort by the last character (requires preprocessing).
- Sort by the first three characters using:
sort -k1.1,1.3 file.txt
- Sort with a custom locale using:
LC_ALL=C sort file.txt
- Perform a stable sort using:
sort -s file.txt
- Sort a file with zero-terminated lines using:
sort -z file.txt
- Reverse sort on a numeric column using:
sort -k2,2nr file.txt
- Sort a file and then add line numbers using:
sort file.txt | nl
- (Simulate) Merge sort using:
sort --merge file.txt
- (Simulate) Sort while ignoring non-printable characters.
- Sort by word count per line (using awk to count words) then sort.
- (Note: Random sort isn’t done with sort; use shuf instead.)
- Sort by multiple comma-separated fields using:
sort -t',' -k1,1 -k2,2 file.txt
- Sort a file containing mixed numeric and string data using:
sort -n file.txt
- Sort ignoring case differences using:
sort -f file.txt
- (Simulate) Sort ignoring punctuation (preprocess if needed).
- Reverse sort and output to another file using:
sort -r file.txt -o rev_sorted.txt
- (Simulate) Sort and then extract the first field using a pipeline.
- (Simulate) Sort using a custom comparator (use available options).
- Enable debug mode in sort using:
sort --debug file.txt
- Sort with human-readable numbers using:
sort -h file.txt
- (Simulate) Sort with a custom dictionary order using available keys.
- Sort based on the first 5 characters using:
sort -k1.1,1.5 file.txt
split
- Split a file into equal-sized parts (100 lines each) using:
split -l 100 file.txt
- Split a file into parts with 100 lines each using:
split -l 100 file.txt
- Split a file into parts with a specified prefix using:
split -l 50 -a 2 file.txt part_
- Split a file by bytes (1 KB per part) using:
split -b 1K file.txt
- Split a large log file into smaller files using:
split -l 1000 logfile.txt
- Split a file and add numeric suffixes using:
split -d file.txt
- Split a file with a custom suffix length (3 characters) using:
split -a 3 file.txt
- Split a file at a specific line number (200 lines per file) using:
split -l 200 file.txt
- Split a file into 1 KB chunks using:
split -b 1K file.txt
- (Simulate) Split a file into parts while preserving the header (requires extra scripting).
- Split a file into a specified number of files using:
split -n 5 file.txt
- Split a file using a numeric suffix using:
split -d file.txt
- Split a file into parts of 500 bytes each using:
split -b 500 file.txt
- Split a file with a custom prefix using:
split -d -a 2 file.txt mypart_
- Split a file and store the parts in a specific directory (change directory first).
- Split a file with verbose output using:
split -v file.txt
- Split a file and then list the created parts using:
split file.txt && ls x*
- Split a file into 5 equal parts using:
split -n 5 file.txt
- Split a file with a numeric suffix length of 3 using: `split -d -a 3 file.tx