# System Commands in Linux

#### Chapter 4: System Commands in Linux

**Overview**

In this chapter, we will delve into various system commands in Linux that are essential for managing system processes, resources, and configurations. These commands provide users with insights into system performance and allow for effective system administration.

#### Key Commands and Examples

| Command    | Most Used Arguments                     | Examples                                                            |
| ---------- | --------------------------------------- | ------------------------------------------------------------------- |
| `top`      | `-u` (user), `-p` (PID)                 | <p><code>top</code><br><code>top -u username</code></p>             |
| `ps`       | `-e` (all), `-f` (full), `-u` (user)    | <p><code>ps -ef</code><br>\`ps aux</p>                              |
| `kill`     | `-9` (force)                            | <p><code>kill PID</code><br><code>kill -9 PID</code></p>            |
| `df`       | `-h` (human-readable)                   | <p><code>df -h</code><br><code>df -i</code></p>                     |
| `free`     | `-m` (megabytes), `-h` (human-readable) | <p><code>free -m</code><br><code>free -h</code></p>                 |
| `uname`    | `-a` (all)                              | `uname -a`                                                          |
| `uptime`   |                                         | `uptime`                                                            |
| `shutdown` | `-h` (halt), `-r` (reboot)              | <p><code>shutdown -h now</code><br><code>shutdown -r +10</code></p> |
| `reboot`   |                                         | `reboot`                                                            |

#### Command Descriptions and Use Cases

1. **`top`**: Displays real-time information about system processes and resource usage.
   * **Use Case**: Monitoring system performance and identifying resource-hogging processes.
2. **`ps`**: Shows the current running processes.
   * **Use Case**: Checking the status of processes or identifying processes by their user or name.
3. **`kill`**: Terminates a process by its process ID (PID).
   * **Use Case**: Stopping a misbehaving application or process.
4. **`df`**: Displays disk space usage for file systems.
   * **Use Case**: Checking available and used disk space on mounted file systems.
5. **`free`**: Displays memory usage statistics.
   * **Use Case**: Monitoring RAM usage and available memory on the system.
6. **`uname`**: Displays system information.
   * **Use Case**: Checking kernel version, system architecture, and operating system details.
7. **`uptime`**: Shows how long the system has been running along with user load averages.
   * **Use Case**: Determining system stability and performance over time.
8. **`shutdown`**: Shuts down or reboots the system.
   * **Use Case**: Safely shutting down the system for maintenance or rebooting for updates.
9. **`reboot`**: Restarts the system.
   * **Use Case**: Quickly rebooting the system after applying updates or changes.

#### Interview Questions and Answers

1. **Q: What does the `top` command do in Linux?**
   * **A**: The `top` command provides a dynamic, real-time view of running processes and their resource usage, allowing users to monitor system performance.
2. **Q: How can you find the PID of a running process?**
   * **A**: You can find the PID of a running process using the `ps` command, such as `ps -ef` or `ps aux`, and then searching for the process name.
3. **Q: What is the difference between `kill` and `kill -9`?**
   * **A**: The `kill` command sends a termination signal to a process, while `kill -9` sends a SIGKILL signal that forces the process to terminate immediately without cleanup.
4. **Q: How can you check disk space usage on your system?**
   * **A**: You can check disk space usage with the `df -h` command, which provides human-readable output of disk usage for all mounted filesystems.
5. **Q: What information does the `free` command provide?**
   * **A**: The `free` command displays information about total, used, free, shared, buffer/cache, and available memory in the system.
6. **Q: What is the purpose of the `shutdown` command?**
   * **A**: The `shutdown` command is used to safely turn off or restart the system, allowing users to perform necessary cleanup and prevent data loss.
7. **Q: How would you check how long the system has been running?**
   * **A**: You can check how long the system has been running by using the `uptime` command, which displays the current time, how long the system has been up, and the number of users.

#### Conclusion

This chapter has covered essential system commands in Linux that are critical for managing system processes and resources. Mastery of these commands will enable learners to perform basic system administration tasks efficiently and prepare them for more advanced topics in subsequent chapters.
