Essential Linux Commands

  1. Creating and Reading a Variable

    1. Create a variable: To create a variable in Linux, you simply assign a value to it: MY_VAR="Hello, World!"
    2. Read the variable: To read or print the value of the variable, use the echo command and prefix the variable name with a $ sign: echo $MY_VAR
  2. File and Directory Management

    1. Listing Files and Directories

      1. ls : List directory contents.

      2. ls -la : Shows all files, including hidden files, with detailed information.

      3. file -s file.txt : Displays the file type of file.txt.

      4. tar: is used for compressing (with gzip or bzip2), and extracting archive files.

        1. tar -cvf backup.tar /home/user/mydata/ : Creates a tar archive named backup.tar of the directory /home/user/mydata.
        2. tar -xvf archive_name.tar : Extracts the contents of archive_name.tar.
        3. tar -czvf archive_name.tar.gz /path/to/directory/ : Creates a gzip-compressed tar archive.
        4. tar -tvf archive_name.tar : Lists the contents of archive_name.tar without extracting.

        Note: c: Create a new archive, z: Indicates that the file is gzip compressed. x: Extract files from the archive. v: Verbose mode.f: Specifies the file name.

    2. Changing Directories

      1. cd : Change the current directory.
      2. cd /path/to/directory : Moves into the specified directory.
    3. Creating and Removing Directories

      1. mkdir : Create a new directory.
      2. mkdir new_directory : Creates a directory named new_directory.
      3. rmdir : Remove an empty directory.
      4. rmdir empty_directory : Removes the specified empty directory.
    4. Removing Files and Directories

      1. rm : Remove files or directories.
      2. rm file.txt : Removes file.txt.
      3. rm -r : Recursively remove a directory and its contents.
      4. rm -r directory_name : Deletes directory_name and all its contents.
    5. Copying and Moving Files

      1. cp : Copy files or directories. Ex- cp source.txt destination.txt >> Copies source.txt to destination.txt.
      2. mv : Move or rename files or directories. Ex- mv oldname.txt newname.txt >> Renames oldname.txt to newname.txt.
      3. sed: Stands for Stream Editor is designed for parsing and transforming text in a non-interactive way.
        1. sed 's/old_text/new_text/g' file.txt: Substitute text in a file
        2. sed -i 's/old_text/new_text/g' file.txt: In-place substitution
        3. sed '/pattern/d' file.txt: Delete lines that match a pattern
        4. sed 's/oldname.txt/newname.txt/' file.txt: Rename a file
    6. Viewing and Editing Files

      1. cat : Display the contents of a file. Ex- cat file.txt >> Shows the content of file.txt.
      2. nano : Open a file in the Nano text editor. Ex- nano file.txt >> Allows editing of file.txt.
      3. vim : Open a file in the Vim text editor. vim stands for Vi IMproved. It is an enhanced version of vi editor. Ex - vim file.txt >> Allows editing of file.txt.
    7. File Permissions and Ownership

      1. chmod : Change file permissions. Ex- chmod 755 script.sh >> Sets permissions for script.sh.
      2. chown : Change file owner. Ex- chown user file.txt : Changes the owner of file.txt to user.
      3. chgrp : Change group ownership. Ex - chgrp groupname file.txt >> Changes the group of file.txt to groupname.
    8. Mounting Filesystems

      1. mount : Attach a filesystem to a mount points (directory/folders).
      2. mount /dev/xvdf /mnt/mydrive : Mounts the device /dev/xvdf at /mnt/mydrive.
      3. umount : Detach a filesystem from a directory.
      4. umount /mnt/mydrive : Unmounts the filesystem mounted at /mnt/mydrive.

      Note : Every storage device or partition connected to a Linux system is represented as a file in the /dev/ directory. For example: If we plug in an SSD, Linux creates something like /dev/sda for that SSD. /dev/xvdf specifically represents a storage device in Linux, typically used for virtual drives in cloud environments, such as AWS, where it signifies a virtual disk that can be mounted and accessed like a physical hard drive.

  3. System Information and Management

    1. Checking System Info

      1. uname : Display system information.
      2. uname -a : Shows detailed information about the kernel and OS.
    2. Monitoring Disk

      1. df : df stands for "disk free". This command provides information about the disk space usage of all mounted file systems.
      2. df -k : Provides information about the disk space usage of all mounted file systems explicitly in kilobytes (1K blocks).
      3. df -h : Provides information about the disk space usage of all mounted file systems in a human-readable format, with the output shown in KB, MB, GB, or TB, depending on the size of the disk space.
      4. du : du stands for "disk usage". This command provides information about the space usage of files and directories.
      5. du -sh : Shows the size of each file and directory in the current directory.
      6. lsblk : Displays a detailed list of block devices, including their size, type, mount points, and more. Note` : lsblk and df -k are often used together: lsblk to understand disk structure, and df -k to check disk space usage.
    3. Checking Memory Usage

      1. free : Display memory usage.
      2. free -m : Shows memory usage in megabytes.
    4. System Load and Processes

      1. top : Provides real-time system information including memory usage
      2. htop : Enhanced version of top (requires installation).
      3. htop : Interactive process viewer with an improved interface.
    5. Process Management

      1. ps : Display current processes.
      2. ps aux : Shows all running processes with details.
      3. kill : Terminate a process by PID.
      4. kill 1234 : Terminates the process with PID 1234.
      5. pkill : Kill processes by name.
      6. pkill firefox : Terminates all processes named firefox.
  4. Networking Commands

    1. Checking Network Configuration

      1. ifconfig** or **ip a : Display network interface information.
      2. ifconfig : Shows the configuration of network interfaces.
      3. ping : Check connectivity to another host.
      4. ping example.com : Sends packets to example.com to test connectivity.
    2. Transferring Files

      1. scp : Securely copy files between hosts.
      2. scp file.txt user@remote\_host:/path/to/destination : Copies file.txt to a remote host.
      3. rsync : Efficiently sync files and directories between two locations.
      4. rsync -avz source/ user@remote\_host:/path/to/destination : Synchronizes files from source/ to a remote host.
    3. Downloading Files

      1. curl : Transfer data from or to a server.
      2. curl -O http://example.com/file.txt : Downloads file.txt from the specified URL.
      3. wget : Retrieve files from the web.
      4. wget http://example.com/file.txt : Downloads file.txt from the specified URL.
  5. User Management

    1. Viewing Current User

      1. whoami : Display the current logged-in user.
      2. whoami : Shows the username of the current user.
    2. User and Group Management

      1. adduser : Create a new user.
      2. sudo adduser newuser : Adds a new user called newuser.
      3. usermod : Modify a user’s properties.
      4. sudo usermod -aG groupname username : Adds username to groupname.
      5. deluser : Remove a user.
      6. sudo deluser username : Deletes the user named username.
  6. File Compression and Archiving

    1. Compressing Files

      1. tar : Create or extract compressed archives.
      2. tar -czvf archive.tar.gz directory/ : Creates a compressed archive of directory.
      3. unzip : Extract compressed ZIP files.
      4. unzip file.zip : Extracts the contents of file.zip.
      5. gzip : Compress a file.
      6. gzip file.txt : Compresses file.txt to file.txt.gz.
  7. File Searching and Finding

    1. `searching for Files

      1. find : Search for files and directories.
      2. find /path/to/search -name "\*.txt" : Finds all .txt files in the specified path.
    2. `searching for Text in Files

      1. grep : Search for specific text within files.
      2. **grep "search\_term" file.txt``** : Searches for search_terminfile.txt`.
      3. grep -r : Recursively search for text in a directory.
      4. grep -r "search\_term" /path/to/directory : Searches for search_term in all files within the specified directory.
  8. Disk and Partition Management

    1. artitioning Devices

      1. sudo fdisk /dev/sda: Opens the partitioning tool to create, modify, or delete partitions on the device.
      2. sudo parted /dev/sda: Launches the partitioning tool for larger disks and modern partitioning methods.
    2. Creating File Systems

      1. mkfs : Create a file system on a partition or device.
      2. sudo mkfs.ext4 /dev/sda1 : Creates an ext4 file system on the partition /dev/sda1.
      3. sudo mkfs.xfs /dev/sda1 : Creates an xfs file system on the partition /dev/sda1.
      4. sudo mkfs.vfat /dev/sda1 : Creates a FAT32 file system on the partition /dev/sda1.
    3. Mounting File Systems

      1. sudo mount /dev/sda1 /mnt/mydrive: Mounts the /dev/sda1 partition at the /mnt/mydrive directory, making it accessible.
      2. sudo umount /mnt/mydrive: Unmounts the file system at /mnt/mydrive.
    4. Checking and Managing File Systems

      1. lsblk: Displays a tree of all block devices, their partitions, and their mount points.
      2. sudo blkid: Displays file system type and other attributes of block devices.
    5. Managing Swap Space

      1. sudo mkswap /dev/sda2: Configures the /dev/sda2 partition as swap space.
      2. sudo swapon /dev/sda2: Enables the swap space on /dev/sda2, allowing the system to use it for memory paging.
  9. Package Management

    1. dnf: is a package manager used by certain Linux distributions (e.g., Fedora, CentOS, RHEL). DNF stands for Dandified YUM. Use dnf to install packages directly from the configured software repositories without needing to download the package manually first.
      1. dnf install package_name: Installs the specified package from the repositories.
      2. dnf update: Updates all installed packages to their latest versions.
      3. dnf remove package_name: Removes the specified package from the system.
      4. dnf search keyword: Searches for packages matching the specified keyword.
      5. dnf list installed: Displays a list of all installed packages on the system.
    2. wget: is a command-line utility for manually downloading files from the web. wget stands for World Wide Web Get. Use wget when need to download a file directly, and we know the specific URL.
      1. wget http://example.com/file.zip: This command will download the file file.zip to your current working directory.
  10. Service Management

    1. systemctl Command is used to examine and control the system (systemd) processes.
    2. Basic Commands
      1. systemctl start service_name: Starts the specified service immediately.
      2. systemctl stop service_name: Stops the specified service.
      3. systemctl restart service_name: Restarts the specified service.
      4. systemctl status service_name: Shows the current status of the specified service.
      5. systemctl enable service_name: Enables the specified service to start automatically at boot time.
      6. systemctl disable service_name: Disables the specified service from starting at boot time.
      7. systemctl list-units --type=service: Lists all loaded services and their current status.
  11. Miscellaneous Commands

    1. Viewing Command History

      1. history : Display the command history.
      2. history : Shows a list of previously executed commands.
    2. Rebooting and Shutting Down

      1. reboot : Restart the system.
      2. sudo reboot : Reboots the machine.
      3. shutdown : Shut down the system.
      4. sudo shutdown now : Shuts down the machine immediately.