Configure
Build your server

Linux tutorial: basic Linux commands in terminal

Linux tutorial: basic Linux commands in terminal 15 August 2023

Linux is a flexible operating system popular among users worldwide. Want to learn how to use Linux and navigate the command line efficiently? Let's dive in.

Linux terminal basics

The Linux Terminal is a text-based interface that enables users to interact with the operating system using command-line instructions. It provides the capability to:

  • Configure the system
  • Manage files and directories
  • Execute scripts
  • Run commands and more.

There are several methods to access the Linux terminal, each varying based on your system and preference:

  • Through Application Search: Useful if the terminal shortcut isn't immediately visible. Typically, there's a search field in the taskbar or application menu. Type "terminal" or "command line" to locate the terminal application.
  • Via the Graphical Interface: Many Linux distributions feature a button or shortcut on the desktop or in the application menu that initiates the Linux terminal. Simply click on it. Common names include "Konsole", "Terminal", and "XTerm".
  • Using Keyboard Shortcuts: Some Linux versions provide shortcuts for swift terminal access. A common combination is pressing Ctrl + Alt + T simultaneously.

How to use the Linux terminal: gaining root access in Linux

Upon launching the terminal, you'll encounter a command prompt where you can input Linux commands and observe their results. While the Linux terminal typically operates under the current user's context, there are instances when you might need elevated privileges (root) to execute specific commands. To run commands with elevated privileges, utilize the sudo command. Simply type sudo [terminal_command_name] and press Enter. To validate this action, the system might prompt you for the password associated with an administrative account. In essence, the Linux Terminal is a robust tool for interacting with the OS. Familiarity with it not only deepens your understanding of the system's capabilities but also enables automation, system customization, and full control over your computer or server.

Basic principles of working in the Linux terminal

  1. Basic Commands: These are essential commands used to:
    1. Create directories (mkdir)
    2. View the contents of directories (ls)
    3. Change the current directory (cd)
    4. Create files (touch)
    5. Move files (mv)
    6. Copy files (cp)
    7. Delete files and directories (rm)
    8. Display file contents (cat).
  2. Access Rights and Privileges: In Linux, both files and directories possess access rights, which specify who can read, write, or execute them. Certain tasks might necessitate administrator rights, for which the sudo command is employed.
  3. Command Line Interface (CLI): This is where users input textual commands to carry out various functions. With the Linux terminal, you can control processes, manage files and directories, and configure the system, among other tasks. To execute these commands, press the Enter key.
  4. Autocompletion and Command History: The Linux terminal offers an autocompletion feature, which enables the automatic filling out of commands after typing just a few initial characters, simplifying the input of lengthy or intricate commands. Furthermore, the terminal keeps a record of previously entered commands. The Up and Down arrow keys allow for easy navigation through this command history.
  5. Path Types - Absolute and Relative: A path indicates a file's or directory's location within the file system. While Linux employs absolute paths starting with '/', it also uses relative paths which describe a location concerning the present directory. For instance, /home/user/Documents is the absolute path to the "Documents" directory, whereas Documents/notes.txt is the relative path to the "notes.txt" file situated in the "Documents" folder.
  6. Command Parameters and Flags: These are supplementary components that can be added to a command in the terminal to modify its behavior or furnish extra information. For instance, the -r flag in the rm command signals the recursive removal of files and directories. Conversely, the -l flag in the ls command shows directory contents as an exhaustive list.

Popular commands for system management in Linux: system commands

  1. Shutdown and Reboot: The shutdown and reboot commands are employed to power down or restart the system, respectively. An example usage is sudo shutdown -h now, which will immediately shut down the system.
  2. Systemctl: This command manages system services in Linux, providing the ability to start, stop, restart, and check the status of these services. For instance, sudo systemctl start [service-name] will begin the specified service.
  3. Network Configuration: The ifconfig (older but still used) and ip commands are essential for retrieving information about network interfaces and setting network parameters, such as the gateway, IP address, and subnet mask.
  4. Process Monitoring - PS: The ps command gives a snapshot of the current processes in the system, including their process identifiers (PIDs), resource usage, and other related data.
  5. Memory Overview - Free: The free command provides insights into the system's memory usage. It presents details about RAM consumption, swap space usage, and total memory capacity.
  6. Real-Time System Monitoring - Top: The top command delivers an interactive monitoring interface, showcasing real-time active processes, memory usage, CPU load, and other vital statistics.
  7. Disk Space Analysis - DF: The df command reveals information about the disk space used and available across various segments of the file system, helping users understand space utilization in each partition.

Commands for installing, updating, and removing programs in Linux

Linux uses a range of package managers to facilitate the installation, upgrading, and removal of software applications. Below are some key commands associated with the DNF and APT package managers:

DNF (Fedora and CentOS):

  • Program Installation:

    Command: sudo dnf install [package_name]
    Example: To install the Firefox web browser, use sudo dnf install firefox.
  • System and Software Update:

    Command: sudo dnf upgrade
    Note: This updates all installed packages to their latest versions.
  • Program Removal:

    Command: sudo dnf remove [package_name]
    Example: To remove the installed Firefox package, use sudo dnf remove firefox.

APT (Debian-based distributions, e.g., Ubuntu):

  • Program Installation:

    Command: sudo apt install [package_name]
    Example: To install the Firefox web browser, use sudo apt install firefox.
  • System and Software Update:
    • Update Package Lists: sudo apt update
    • Update Installed Packages: sudo apt upgrade
    Note: The apt update command refreshes the list of available packages, while apt upgrade updates the installed packages to their latest versions.
  • Program Removal:

    Command: sudo apt remove [package_name]
    Example: To uninstall the Firefox package, use sudo apt remove firefox.

Commands for file and directory management in Linux

Linux provides a suite of commands for managing files and directories. Below are some of the essential commands:

  • find: Search for files and directories based on various criteria, such as name, size, and type.
    Example: find / -name file_name.txt
  • mkdir: Create a new directory.
    Example: mkdir ~/new_directory
  • cat: Display the content of a file.
    Example: cat file_name.txt
  • touch: Create a new file.
    Example: touch ~/new_directory/new_file.txt
  • rm: Delete files and directories. Caution: Deleted files cannot be restored.
    To delete a directory and its contents: rm -r directory_name
    To delete a file: rm file_name.txt
  • rename: Rename files.
    Example: rename old_file_name new_file_name
  • mv: Move or rename files or directories.
    Example: mv ~/source_directory/file_name.txt ~/target_directory/
  • pwd: Display the path of the current directory.
    Example: pwd
  • cd: Change the current directory.
    Example: cd ~/Documents
  • cp: Copy files or directories.
    Example: cp ~/source_directory/file_name.txt ~/destination_directory/
  • ls: List the contents of the current or specified directory.
    Example: ls ~/directory_name

Commands for managing users in Linux

  • sudo: Execute commands with superuser (root) privileges.
    Example: sudo command_name
  • useradd: Create a new user account.
    Example: useradd user_name
  • su: Switch to another user or assume superuser (root) privileges.
    Example: su user_name
  • passwd: Set or change a user's password. After executing this command, you'll be prompted to input a new password for the designated user.
    Example: passwd user_name
  • userdel: Delete a user account, which will also remove their home directory.
    Example: userdel user_name
  • usermod: Modify user account settings, such as their home directory, associated group, and more.
    Example: usermod [options] user_name

Conclusively, the Linux operating system offers an extensive suite of tools for system management. While this guide touches upon the basics suitable for newcomers, there are numerous other commands available for diverse requirements.