List files in long format showing permissions, owner, group, size, and modification date:
List all files including hidden files:
Change to a directory relative to the current directory:
Change to the previous directory:
Print the current directory path:
Print the current directory path without symbolic links:
Create a directory with read, write, and execute permissions for the owner:
Create a directory with parent directories as needed:
Remove a file without prompting:
Remove a directory and its contents recursively:
Copy a file, preserving mode, ownership, and timestamps:
Copy a directory recursively:
Move a file to a new location:
Rename a file:
Create a new empty file:
Update the access and modification times of an existing file to the current time:
Changing Permissions for a Script
This Command: makes the script "script.sh" executable, allowing users to run it as a program.
Example 2: Restricting Access to a Configuration File
This Command: sets strict permissions on the "config.ini" file, allowing only the owner to read and write to it, while denying access to others.
Example 1: Changing Ownership of a Directory
This Command: changes the ownership of the "/var/www/html" directory and all its contents recursively to the user "user1" and the group "group1".
Example 2: Transferring Ownership of a File
This Command: changes the ownership of the file "file.txt" to "user2" and the group "group2".
Example 1: Changing Group Ownership of a Directory
This Command: changes the group ownership of the "/var/www/project" directory and all its contents to the "developers" group recursively.
Example 2: Assigning Group Ownership to a File
This Command: assigns the group ownership of the file "file.txt" to the "admins" group.
Example 1: Setting Default Permissions for Files
This Command: sets the default permissions for newly created files to allow full access for the owner and read/write permissions for the group and others.
Example 2: Configuring Default Permissions for Directories
This Command: sets the default permissions for newly created directories to allow full access for the owner and read-only access for the group and others.
Example 1: Displaying File Contents
This Command: displays the contents of the file "filename.txt" on the terminal.
Example 2: Concatenating Files
This Command: concatenates the contents of "file1.txt" and "file2.txt" into a new file named "combined_file.txt".
Example 1: Searching for a Pattern in a File
This Command: searches for the word "error" in the file "log.txt" and displays all lines containing that pattern.
Example 2: Searching Recursively in Directory
This Command: recursively searches for the pattern "pattern" in all files within the specified directory.
Example 1: Replace Text in a File
This Command: replaces occurrences of "old_text" with "new_text" in the file "input.txt" and writes the result to "output.txt".
Example 2: Deleting Lines Matching a Pattern
This Command: deletes all lines containing the pattern "pattern" from "file.txt" and saves the result to "newfile.txt".
Example 1: Extracting Specific Columns from a CSV File
This Command: prints the first and third columns of each line in the CSV file "data.csv", with columns separated by a comma.
Example 2: Calculating Total Sales from a Sales Report
This Command: calculates the total sales by summing up the values in the second column of a sales report file "sales_report.txt".
Example 1: Displaying First 10 Lines of a File
This Command: displays the first 10 lines of the file "file.txt".
Example 2: Displaying First N Lines of a File
This Command: displays the first 20 lines of the file "file.txt".
Example 1: Displaying Last 10 Lines of a File
This Command: displays the last 10 lines of the file "file.txt".
Example 2: Displaying Last N Lines of a File
This Command: displays the last 5 lines of the file "file.txt".
Example 1: Print detailed system information:
This command is useful when you need comprehensive system information, including the kernel version, hardware architecture, and operating system details. It's commonly used for system diagnostics and troubleshooting.
Example 2: Display the machine hardware name
This command specifically retrieves the machine hardware name, which can be helpful in scripting or when you need to identify the hardware architecture for compatibility purposes.
Example 1: Display system uptime and load averages
Usage: Administrators often use this command to quickly check the system's uptime and current load averages. It helps in monitoring system performance and determining if any resource constraints exist.
Example 2: Show system boot time and number of users logged in
Usage: This command provides a more concise view, particularly for checking when the system was last rebooted and how many users are currently logged in.
Example 1: Print the hostname of the system
Usage: Retrieving the hostname is helpful when you need to identify the system within a network or when setting up network configurations.
Example 2: Set the hostname of the system
Usage: Administrators might use this command to change the hostname of a system, which can be necessary during system setup or when reconfiguring network settings.
Example 1: Display memory usage in megabytes
Usage: This command provides a detailed breakdown of memory usage, helping administrators identify memory-intensive processes or potential memory leaks.
Example 2: Show memory usage in gigabytes and update every second
Usage: By continuously monitoring memory usage in real-time, administrators can detect sudden spikes or unusual patterns, enabling proactive troubleshooting and optimization.
Example 1: Display disk space usage for all mounted filesystems
Usage: This command is crucial for monitoring disk space utilization across all mounted filesystems, helping administrators identify potential disk space constraints and plan for storage expansion if necessary.
Example 2: Show disk space usage only for a specific filesystem
Usage: When focusing on a specific filesystem, such as a data partition or a mounted network share, this command provides targeted information for capacity planning and resource allocation.
Example 1: Report a snapshot of all processes
Usage: This command provides a snapshot of all running processes, along with detailed information such as process ID, CPU and memory usage, and execution status.
Example 2: Report a snapshot of specific processes
Usage: Using grep with ps allows filtering processes based on specific criteria, such as process name or user.
Example 1: Terminate a process by PID
Usage: This command sends the default signal (SIGTERM) to the specified process ID (PID), causing the process to terminate gracefully.
Example 2: Forcefully terminate a process by PID
Usage: The -9 option sends the SIGKILL signal to the specified process ID, forcefully terminating the process without giving it a chance to clean up.
Example 1: Terminate all processes with a specific name
Usage: This command sends the default SIGTERM signal to all processes with the specified name, prompting them to terminate gracefully.
Example 2: Forcefully terminate all processes with a specific name
Usage: The -9 option sends the SIGKILL signal to all processes with the specified name, forcefully terminating them without allowing them to clean up.
Example 1: Terminate processes based on name
Usage: This command sends the default SIGTERM signal to all processes matching the specified name, allowing for efficient termination of multiple processes.
Example 2: Forcefully terminate processes based on name
Usage: The -9 option sends the SIGKILL signal to all processes matching the specified name, forcefully terminating them without allowing them to clean up.
Example 1: List process IDs based on name
Usage: This command lists the process IDs of all processes matching the specified name, providing a convenient way to identify processes for further actions.
Example 2: List processes with full details based on name
Usage: The -l option provides the list of processes along with their full details matching the specified name.
Example 1: Display an interactive process viewer
Usage: htop is an interactive process viewer that provides a more user-friendly interface compared to top. It allows users to interactively monitor and manage processes with ease.
Example 2: Sort processes by memory usage
Usage: This command sorts processes by memory usage, helping users identify memory-intensive processes easily.
Example 1: Display dynamic process information
Usage: The top command displays dynamic information about Linux processes, including CPU and memory usage, and allows users to interactively monitor and manage processes.
Example 2: Monitor processes by sorting by CPU usage
Usage: This command sorts processes by CPU usage, allowing users to identify resource-intensive processes easily.
Example 1: Install packages
Usage: This command installs the specified package and its dependencies using the apt package manager in Debian/Ubuntu-based systems.
Example 2: Install packages with yum
Usage: This command installs the specified package and its dependencies using the yum package manager in RHEL/CentOS-based systems.
Example 1: Update package lists with apt-get
Usage: This command updates the local package lists from the repositories in Debian/Ubuntu-based systems.
Example 2: Update package lists with yum
Usage: This command updates the local package lists from the repositories in RHEL/CentOS-based systems.
Example 1: Upgrade installed packages with apt-get
Usage: This command upgrades all installed packages to their latest versions in Debian/Ubuntu-based systems.
Example 2: Upgrade installed packages with yum
Usage: This command upgrades all installed packages to their latest versions in RHEL/CentOS-based systems.
Example 1: Remove packages with apt-get
Usage: This command removes the specified package from the system in Debian/Ubuntu-based systems.
Example 2: Remove packages with yum
Usage: This command removes the specified package from the system in RHEL/CentOS-based systems.
Example 1: Troubleshooting network interface issues with ifconfig
Usage: This command displays detailed information about the network interface 'eth0', including its IP address, MAC address, and network statistics. It can help troubleshoot connectivity or configuration issues.
Example 2: Configuring a new IP address with ip
Usage: This command assigns the IP address '192.168.1.100' with a subnet mask of '24' to the network interface 'eth0'. It's useful for configuring new network parameters dynamically.
Example 1: Troubleshooting network connectivity with ping
Usage: This command sends ICMP echo requests to the specified domain 'google.com' to check network connectivity. It's useful for diagnosing network reachability and latency issues.
Example 2: Continuously pinging a host for stability testing
Usage: This command sends 1000 ICMP echo requests with an interval of 0.2 seconds to the host 'host.example.com' for stability testing. It helps identify intermittent network issues.
Example 1: Diagnosing network path issues with traceroute
Usage: This command traces the route taken by packets to the destination 'example.com' and displays each hop along the way. It's helpful for identifying network bottlenecks and routing issues.
Example 2: Using tracepath to discover MTU along the route
Usage: This command traces the route to 'example.com' and determines the maximum transmission unit (MTU) along the path. It's useful for troubleshooting MTU-related network problems.
Example 1: Displaying active network connections
Usage: This command prints a list of all active TCP and UDP connections, along with listening ports and associated process IDs. It's useful for monitoring network activity and identifying open ports.
Example 2: Showing network interface statistics
Usage: This command displays statistics for each network interface, including packets transmitted and received, errors, and collisions. It's helpful for analyzing network performance and troubleshooting interface issues.
Example 1: Listing all sockets
Usage: This command displays a comprehensive list of all sockets, including TCP, UDP, and UNIX domain sockets. It provides detailed information about each socket, including state and associated processes.
Example 2: Showing listening TCP sockets with port numbers
Usage: This command filters the output to display only listening TCP sockets along with their port numbers. It's useful for monitoring incoming connections and identifying open ports.
Example 1: Create a simple TCP listener with nc
Usage: This command listens for incoming TCP connections on port 1234. It's useful for setting up simple network services or debugging network communication.
Example 2: Transfer files over the network using nc
Usage: This pair of commands sets up a simple file transfer over TCP. One instance listens on port 1234 and saves incoming data to 'file.txt', while the other connects to the destination IP and sends the contents of 'file.txt'.
Example 1: Allow SSH connections with iptables
Usage: This command adds a rule to allow incoming SSH connections on port 22. It's helpful for configuring basic firewall rules to permit specific types of traffic.
Example 2: Drop all incoming traffic from a specific IP address
Usage: This command adds a rule to drop all incoming traffic from the specified IP address 'bad_ip'. It's useful for blocking malicious or unwanted traffic.
Example 1: Allow HTTP traffic in the default zone
Usage: This command adds a rule to allow incoming HTTP traffic in the default 'public' zone. It's useful for configuring firewall rules in a more dynamic and user-friendly manner compared to iptables.
Example 2: Block all incoming traffic from a specific IP address range
Usage: This command adds a rich rule to block all incoming traffic from the specified IPv4 address range '10.0.0.0/24' in the 'public' zone. It demonstrates the flexibility of firewalld in defining more complex filtering rules.
Example 1: Add a new user with useradd
Usage: This command creates a new user account named 'username' with a home directory and sets the default shell to '/bin/bash'.
Example 2: Delete a user account with userdel
Usage: This command removes the user account 'username' along with their home directory and mail spool.
Example 1: Change the password for a user
Usage: This command prompts the user to enter a new password for the account 'username'. It provides a simple way to update user passwords.
Example 2: Force a password change at next login
Usage: This command expires the password for the user 'username', forcing them to change their password the next time they log in.
Example 1: Switch to the root user
Usage: This command switches to the root user by prompting for the root password. It's useful for performing administrative tasks that require root privileges.
Example 2: Switch to another user without changing the environment
Usage: This command switches to the user 'username' while preserving their environment settings. It's helpful for troubleshooting user-specific issues.
Example 1: Execute a command with elevated privileges
Usage: This command updates the package list using apt with elevated privileges. It's commonly used for system administration tasks that require root permissions.
Example 2: Execute a command as another user
Usage: This command executes 'command' as the user 'username' with elevated privileges. It's useful for running specific commands with different user privileges.
Example 1: Monitor system log changes in real-time
Usage: This command displays the last 10 lines of the syslog file and then continuously updates the display as new log entries are added. It's useful for troubleshooting issues and monitoring system activity.
Example 2: View only log entries related to SSH
Usage: This command filters the syslog output to show only log entries related to the SSH daemon (sshd). It helps in monitoring SSH activity and troubleshooting SSH-related issues.
Example 1: Display all journal entries
Usage: This command displays all journal entries, including boot messages, system events, and service logs. It's the default way to view systemd journal messages.
Example 2: Filter journal entries by a specific unit
Usage: This command filters journal entries to show only those related to the SSH daemon (sshd.service). It's useful for troubleshooting specific service issues.
Example 1: Start a systemd service
Usage: This command starts the specified systemd service. It's used to initiate a service manually or as part of a system startup process.
Example 2: Enable a systemd service to start on boot
Usage: This command configures the specified systemd service to start automatically during system boot. It creates symlinks to the service unit files.
Example 1: Start a System V init script
Usage: This command starts the specified System V init script. It's commonly used in systems that use SysVinit as the init system.
Example 2: Stop a System V init script
Usage: This command stops the specified System V init script. It halts the execution of the script and terminates the associated service.
Example 1: Monitoring system resource usage with htop
Usage: This command launches htop, an interactive process viewer that provides real-time monitoring of system resources such as CPU, memory, and disk usage. It's beneficial for DevOps professionals to identify resource-hungry processes and troubleshoot performance issues.
Example 2: Sorting processes by memory usage
Usage: This command starts htop and sorts processes based on memory usage, helping DevOps teams identify memory-intensive processes quickly and optimize system resources.
Example 1: Monitoring system resource usage with top
Usage: This command displays Linux processes in real-time, providing information on CPU, memory, and swap usage. DevOps engineers can use top to identify resource bottlenecks and optimize system performance.
Example 2: Sorting processes by CPU usage
Usage: This command launches top and sorts processes based on CPU usage, enabling DevOps teams to pinpoint CPU-intensive tasks and troubleshoot performance issues.
Example 1: Listing all processes with ps
Usage: This command generates a snapshot of all running processes, providing detailed information such as process ID, CPU and memory usage, and execution status. DevOps professionals can use ps to identify resource-intensive processes and manage system resources efficiently.
Example 2: Filtering processes by name
Usage: This command filters processes by name using grep, allowing DevOps teams to narrow down process listings and focus on specific tasks or troubleshooting activities.
Example 1: Display system-wide virtual memory statistics
Usage: This command provides an overview of system-wide virtual memory statistics including processes, memory, paging, block I/O, traps, and CPU activity. DevOps engineers can use vmstat to identify memory and CPU usage patterns, and detect potential bottlenecks in the system.
Example 2: Monitor memory usage at regular intervals
Usage: This command displays virtual memory statistics every 5 seconds, providing real-time insights into system performance and resource utilization. DevOps professionals can use this feature to monitor memory usage trends and identify abnormalities over time.
Example 1: Display CPU and I/O statistics for all devices
Usage: This command generates a report of CPU and I/O statistics, including utilization, throughput, and latency, for all devices in the system. DevOps teams can use iostat to monitor disk performance, identify I/O bottlenecks, and optimize storage configurations.
Example 2: Monitor disk I/O activity in real-time
Usage: This command displays disk I/O statistics every 5 seconds, allowing DevOps engineers to monitor disk activity patterns and detect performance issues as they occur. It provides valuable insights into disk utilization and helps optimize storage resources.
Example 1: Collect CPU utilization data for the last 24 hours
Usage: This command retrieves CPU utilization data from the system activity files for the previous day and presents it in a readable format. DevOps teams can analyze historical CPU usage patterns and optimize system performance accordingly.
Example 2: Monitor memory and swap space usage at regular intervals
Usage: This command reports memory and swap space utilization every 5 seconds, enabling DevOps professionals to track memory usage trends and identify potential memory-related issues in real-time.
Example 1: Start nmon and display CPU and memory statistics
Usage: This command starts nmon and displays real-time performance data including CPU utilization, memory usage, disk I/O, and network activity. DevOps professionals can use nmon to monitor system health and identify resource bottlenecks.
Example 2: Generate nmon data for further analysis
Usage: This command runs nmon in data capture mode, saving performance data to a file (-f) every 10 seconds (-s) for a duration of 120 intervals (-c). DevOps teams can analyze the recorded data to gain insights into system behavior over time.
Example 1: Create a tar archive of a directory
Usage: This command creates a tar archive named 'archive.tar' containing all files and directories within 'directory'. The options (-cvf) display the progress, verbosely list files processed, and specify the filename.
Example 2: Extract files from a tar archive
Usage: This command extracts files from the tar archive 'archive.tar'. The options (-xvf) display the progress, verbosely list files extracted, and extract files from the archive.
Example 1: Compress a file with gzip
Usage: This command compresses the file 'filename' using gzip compression algorithm. It replaces the original file with the compressed version, appending '.gz' to the filename.
Example 2: Decompress a gzip-compressed file
Usage: This command decompresses the gzip-compressed file 'filename.gz'. The option (-d) instructs gzip to decompress the file, restoring the original uncompressed file.
Example 1: Compress a file with bzip2
Usage: This command compresses the file 'filename' using the bzip2 compression algorithm. It replaces the original file with the compressed version, appending '.bz2' to the filename.
Example 2: Decompress a bzip2-compressed file
Usage: This command decompresses the bzip2-compressed file 'filename.bz2'. The option (-d) instructs bzip2 to decompress the file, restoring the original uncompressed file.
Example 1: Compress a file with xz
Usage: This command compresses the file 'filename' using the xz compression algorithm. It replaces the original file with the compressed version, appending '.xz' to the filename.
Example 2: Decompress an xz-compressed file
Usage: This command decompresses the xz-compressed file 'filename.xz'. The option (-d) instructs xz to decompress the file, restoring the original uncompressed file.
Example 1: Synchronize files and directories locally
Usage: This command synchronizes files and directories from 'source_directory' to 'destination_directory'. The options (-av) maintain file permissions, enable verbose output, and recursively synchronize directories.
Example 2: Synchronize files and directories over SSH
Usage: This command synchronizes files and directories from 'source_directory' to 'destination_directory' on a remote host using SSH. The options (-avz) maintain permissions, enable compression, and provide verbose output, while (-e ssh) specifies the SSH protocol.
Example 1: Create a disk image with dd
Usage: This command creates a disk image 'disk_image.img' from the contents of '/dev/sda'. The 'if' option specifies the input file (source), and 'of' specifies the output file (destination).
Example 2: Copy data between disks with dd
Usage: This command copies data from '/dev/sda' to '/dev/sdb' with a block size of 4 megabytes and synchronizes I/O during copying. It's useful for disk cloning and data migration.