Tag: Linux

  • MySQL CLI Quick Reference

    Connecting to MySQL

    • Login to MySQL:
      mysql -u username -p

    Prompts for a password.

    • Specify Database on Login:
      mysql -u username -p -D database_name

    Basic Commands

    • Show all databases:
      SHOW DATABASES;
    • Select a database:
      USE database_name;
    • Show all tables in the current database:
      SHOW TABLES;
    • Describe the structure of a table:
      DESCRIBE table_name;
    • Show the current MySQL user:
      SELECT USER();
    • Show current database:
      SELECT DATABASE();
    • Show process list:
      SHOW PROCESSLIST;
    • Show full process list (to see full queries):
      SHOW FULL PROCESSLIST;
    • Exit MySQL:
      EXIT;

    CRUD Operations

    • Create a new database:
      CREATE DATABASE database_name;
    • Create a new table:
      CREATE TABLE table_name (
        id INT AUTO_INCREMENT PRIMARY KEY,
        name VARCHAR(100),
        age INT
      );
    • Insert data into a table:
      INSERT INTO table_name (name, age) VALUES ('John', 30);
    • Select data from a table:
      SELECT * FROM table_name;
    • Update data in a table:
      UPDATE table_name SET age = 31 WHERE name = 'John';
    • Delete data from a table:
      DELETE FROM table_name WHERE name = 'John';
    • Drop a table:
      DROP TABLE table_name;

    Loading .sql Files

    To load and execute an SQL file that contains queries or database structure (e.g., schema.sql or data.sql):

    Method 1: From Inside the MySQL Prompt
    1. Open MySQL:
       mysql -u username -p
    1. Select the database you want to load the file into:
       USE database_name;
    1. Load the SQL file:
       SOURCE /path/to/your/file.sql;
    Method 2: Directly from the Command Line

    You can execute the SQL file directly from the command line without entering the MySQL prompt:

    mysql -u username -p database_name < /path/to/your/file.sql

    This method runs the .sql file directly into the specified database.

    Common Tips

    • View running MySQL queries:
      Use SHOW FULL PROCESSLIST; to view running queries, especially helpful for debugging long-running queries.
    • Check MySQL version:
      SELECT VERSION();
    • Enable SQL logging:
      Use SET GLOBAL general_log = 'ON'; to start logging all queries. To view the log, check the path from the following:
      SHOW VARIABLES LIKE 'general_log_file';

    These commands and tips should help you navigate MySQL efficiently!

  • Uninstall Chrome on Linux

    1. Open a terminal window.
      • On GNOME, Unity, and Cinnamon environments, press Ctrl + Alt + t.
      • On KDE environments, go to Application Menu System Konsole.
    2. Enter the uninstall command:
      • Debian-based systems: Enter sudo dpkg -r google-chrome-stable.
      • Other systems: Enter sudo rpm -e google-chrome-stable.
    3. When prompted, enter your computer’s password.

    Sauce

    Uninstall Chrome on Linux
  • Linux Commands Cheat Sheet

    Here’s a list of common commands along with their descriptions and usage examples:

    1. ls – List Directory Contents
      • Usage: ls [options] [directory]
      • Example: ls -l /home (lists all files in /home with detailed information)
    2. cd – Change Directory
      • Usage: cd [directory]
      • Example: cd /home/user/Documents (changes the current directory to /home/user/Documents)
    3. pwd – Print Working Directory
      • Usage: pwd
      • Example: pwd (displays the path of the current directory)
    4. cat – Concatenate and Display Files
      • Usage: cat [options] [file]
      • Example: cat file.txt (displays the content of file.txt)
    5. cp – Copy Files and Directories
      • Usage: cp [options] source destination
      • Example: cp file1.txt file2.txt (copies file1.txt to file2.txt)
    6. mv – Move or Rename Files and Directories
      • Usage: mv [options] source destination
      • Example: mv file1.txt /home/user/Documents (moves file1.txt to /home/user/Documents)
    7. rm – Remove Files or Directories
      • Usage: rm [options] file
      • Example: rm file.txt (removes file.txt)
    8. mkdir – Create a New Directory
      • Usage: mkdir [options] directory
      • Example: mkdir new_folder (creates a new directory named new_folder)
    9. rmdir – Remove Empty Directories
      • Usage: rmdir [options] directory
      • Example: rmdir old_folder (removes the directory old_folder if it’s empty)
    10. grep – Search Text Using Patterns
      • Usage: grep [options] pattern [file]
      • Example: grep "hello" file.txt (searches for the word “hello” in file.txt)
    11. find – Search for Files in a Directory Hierarchy
      • Usage: find [path] [options] [expression]
      • Example: find /home -name "file.txt" (finds all files named “file.txt” in /home)
    12. chmod – Change File Modes or Access Control Lists
      • Usage: chmod [options] mode file
      • Example: chmod 755 script.sh (sets the permission of script.sh to 755)
    13. chown – Change File Owner and Group
      • Usage: chown [options] owner[:group] file
      • Example: chown user:group file.txt (changes the owner of file.txt to ‘user’ and the group to ‘group’)
    14. tail – Output the Last Part of Files
      • Usage: tail [options] [file]
      • Example: tail -n 5 file.txt (displays the last 5 lines of file.txt)
    15. head – Output the First Part of Files
      • Usage: head [options] [file]
      • Example: head -n 5 file.txt (displays the first 5 lines of file.txt)
    16. man – Interface to the System Reference Manuals
      • Usage: man [command]
      • Example: man ls (displays the manual page for the ls command)

    This is just a basic set of commands, and there are many more commands and options in Linux. For detailed information and more commands, you can always use the man command followed by the command name to read the manual pages.

    Linux Commands Cheat Sheet
  • Crontab

    Issue:

    How to setup cron jobs in Ubuntu (and Linux)?

    Solution:

    Setting up cron jobs on a Linux system like Ubuntu is a straightforward process. Cron jobs are scheduled tasks that your system runs at specified times or intervals. Here’s how you can set them up:

    Accessing the Cron Table:

    1. Open the Terminal: Launch your terminal application.
    2. Edit Cron Jobs for Your User: To edit the cron jobs for your current user, use the following command: crontab -e This command opens your user’s cron file in the default text editor. If it’s your first time using crontab, it may ask you to select an editor (like nano, vi, etc.).
    3. Add a New Cron Job: In the text editor, you can add a new cron job in the following format: * * * * * command-to-execute This is broken down as:
      • Minute (0 - 59)
      • Hour (0 - 23)
      • Day of the month (1 - 31)
      • Month (1 - 12)
      • Day of the week (0 - 6) (Sunday = 0)
      For example, to run a script every day at 5 AM, you would write: 0 5 * * * /path/to/script.sh Make sure your script (script.sh in this example) is executable. You can make it executable with chmod +x /path/to/script.sh.
    4. Save and Exit: After adding your cron job, save and exit the editor. This will install the new cron job.
    5. Verify Your Cron Jobs: To ensure your cron job is listed, use: crontab -l

    Special Syntax:

    Instead of the asterisks, you can use special strings for common intervals:

    • @reboot: Run once, at startup.
    • @yearly or @annually: Run once a year, “0 0 1 1 *”.
    • @monthly: Run once a month, “0 0 1 * *”.
    • @weekly: Run once a week, “0 0 * * 0”.
    • @daily: Run once a day, “0 0 * * *”.
    • @hourly: Run once an hour, “0 * * * *”.

    Common Examples:

    • Backup Every Day at Midnight:
      0 0 * * * /path/to/backup/script.sh
    • Run a PHP Script Every Hour:
      0 * * * * /usr/bin/php /path/to/your/script.php

    Tips:

    • Environment Variables: Cron jobs run in a minimal environment, so you might need to define environment variables that your task requires.
    • Output Handling: By default, cron sends the output of the job to the user’s mail (local mail, not internet email). You can redirect the output to a file or to /dev/null if you don’t need it.
    • Logging: To log the output of a cron job, redirect the output to a file:
      * * * * * command-to-execute >> /path/to/logfile 2>&1
    • Script Permissions: Ensure that any scripts you’re calling are executable and have the appropriate permissions.

    Cron jobs are particularly useful for automating routine tasks like backups, system updates, or periodic cleanup of temporary files.

    Crontab
  • LAMP Stack Setup

    Setup for Ubuntu 20.04 LTS or 22.04 LTS. Lifted from this resource on AWS.

    Linux

    WSL, Ubuntu Multipass, or equivalant.

    Apache

    Version: distribution

    sudo apt update -y
    sudo apt install apache2 -y

    PHP FPM

    Version: 8.2

    Run the following commands to install PHP:

    sudo apt-get install software-properties-common
    sudo add-apt-repository ppa:ondrej/php
    sudo apt-get update -y
    sudo apt install php8.2 php8.2-{fpm,mysql,curl,gd,mbstring,mysql,xml,mcrypt,zip,ldap} libapache2-mod-php8.2 -y

    Install PHP-FPM

    sudo a2enmod proxy_fcgi setenvif
    sudo a2enconf php8.2-fpm
    sudo a2dismod php8.2
    sudo systemctl enable php8.2-fpm
    sudo service apache2 restart;sudo service php8.2-fpm restart

    MariaDB

    Version: 11.1.2

    Run the following command to add the MariaDB yum repository (For all Linux distributions):

    curl -LsS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | sudo bash -s -- --mariadb-server-version="mariadb-11.1.2"

    Install MariaDB Package

    sudo apt install mariadb-server -y

    MariaDB Secure Installation

    sudo mysql_secure_installation

    When prompted, set a password for the root account:

    • Enter the current root password. By default, the root account doesn’t have a password set.
    • Press Enter.
    • Press N to switch to unix_socket authentication.
    • Press Y to set a password, and then enter a secure password twice. Make sure to store this password in a safe place.
    • Press Y to remove the anonymous user accounts.
    • Press Y to disable the remote root login.
    • Press Y to remove the test database.
    • Press Y to reload the privilege tables and save your changes.

    Permissions

    The following command will add the ubuntu user to the www-data group:

    sudo usermod -a -G www-data ubuntu

    The following commands will set the correct permissions for the /var/www directory:

    sudo chown -R ubuntu:www-data /var/www
    sudo chmod 2775 /var/www
    find /var/www -type d -exec sudo chmod 2775 {} \;
    find /var/www -type f -exec sudo chmod 0664 {} \;

    Your site is ready!

    Visit your website by entering your instance’s public IP address in your browser. You should see the default Apache page.

    LAMP Stack Setup
  • PDF to HTML

    WSL, Ubuntu 20.05, command line, for “PDF to HTML.” Step by step.

    Here are the short instructions to convert a PDF to HTML using WSL with Ubuntu 20.04:

    1. Install WSL with Ubuntu 20.04.
    2. Launch the Ubuntu terminal.
    3. Update Ubuntu: sudo apt update && sudo apt upgrade -y.
    4. Install Poppler: sudo apt install -y poppler-utils.
    5. Convert PDF to HTML using:
      pdftohtml -s <input_file>.pdf <output_file>.html
    6. Access the converted HTML file in the same directory.

    That’s it!

    PDF to HTML