Born in 1976 , geek entertainer.

  • Understanding the Unix ‘head’ Command

    commandline session

    head is a program on Unix and Unix-like operating systems used to display the beginning of a text file or piped data.

    $sudo head /var/log/syslog
    Jan  5 20:10:05 debian syslogd 1.5.0#6.1: restart.
    Jan  5 20:10:05 debian anacron[1056]: Job `cron.daily' terminated
    Jan  5 20:10:05 debian anacron[1056]: Normal exit (1 job run)
    Jan  5 20:12:14 debian acpid: client connected from 1055[0:0]
    Jan  5 20:12:14 debian acpid: 1 client rule loaded
    Jan  5 20:12:14 debian acpid: client connected from 1055[0:0]
    Jan  5 20:12:14 debian acpid: 1 client rule loaded
    Jan  5 20:13:03 debian acpid: client 1055[0:0] has disconnected
    Jan  5 20:13:03 debian acpid: client 1055[0:0] has disconnected
    Jan  5 20:14:54 debian /usr/sbin/gpm[1239]: *** info [daemon/processrequest.c(42)]:
    $sudo head -n 5 /var/log/syslog
    Jan  5 20:10:05 debian syslogd 1.5.0#6.1: restart.
    Jan  5 20:10:05 debian anacron[1056]: Job `cron.daily' terminated
    Jan  5 20:10:05 debian anacron[1056]: Normal exit (1 job run)
    Jan  5 20:12:14 debian acpid: client connected from 1055[0:0]
    Jan  5 20:12:14 debian acpid: 1 client rule loaded
    $sudo head -n 0  /var/log/syslog
    $sudo head -n 1  /var/log/syslog
    Jan  5 20:10:05 debian syslogd 1.5.0#6.1: restart.
    $
    
    

    Print the first 10 lines of each FILE to standard output.
    Part of debian coreutils.

     -n, --lines=[-]NUM
     print the first NUM lines instead of the first 10; with the leading
     '-', print all but the last NUM lines of each file
    
    -c, --bytes=[-]NUM
     print the first NUM bytes of each file; with the leading '-', print 
    all but the last NUM bytes of each file
    $head  /etc/default/grub
    # If you change this file or any /etc/default/grub.d/*.cfg file,
    # run 'update-grub' afterwards to update /boot/grub/grub.cfg.
    # For full documentation of the options in these files, see:
    #   info -f grub -n 'Simple configuration'
    
    GRUB_DEFAULT=0
    GRUB_TIMEOUT=5
    GRUB_DISTRIBUTOR=`( . /etc/os-release && echo ${NAME} )`
    GRUB_CMDLINE_LINUX_DEFAULT="quiet splash video=HDMI-1:1920x1080@84"
    GRUB_CMDLINE_LINUX=""
    $head  -c 10 /etc/default/grub
    # If you c$
    
    
    -v, --verbose
    always print headers giving file names
    
    $head  -v /etc/default/grub
    ==> /etc/default/grub <==
    # If you change this file or any /etc/default/grub.d/*.cfg file,
    # run 'update-grub' afterwards to update /boot/grub/grub.cfg.
    # For full documentation of the options in these files, see:
    #   info -f grub -n 'Simple configuration'
    
    GRUB_DEFAULT=0
    GRUB_TIMEOUT=5
    GRUB_DISTRIBUTOR=`( . /etc/os-release && echo ${NAME} )`
    GRUB_CMDLINE_LINUX_DEFAULT="quiet splash video=HDMI-1:1920x1080@84"
    GRUB_CMDLINE_LINUX=""
    $
    
    
    
    -z, --zero-terminated
     line delimiter is NUL, not newline
    

    The -z option explained

  • ls -ltr command to sort files by time

    $ls
    greedy-algorithm-example-coin-change-problem.js
    longest-common-substring.js
    memoization-top-down-approach.js
    solving-knapsack-problem-a-recursive-solution.js
    solving-knapsack-problem.js
    tabulation-bottom-up-approach.js
    $
    ls shows the contents of the current directory


    $ls -ltr
    total 24
    -rw-rw-r-- 1 jeffrin jeffrin 591 Feb 9 21:37 memoization-top-down-approach.js
    -rw-rw-r-- 1 jeffrin jeffrin 436 Feb 9 21:56 tabulation-bottom-up-approach.js
    -rw-rw-r-- 1 jeffrin jeffrin 2133 Feb 16 22:13 longest-common-substring.js
    -rw-rw-r-- 1 jeffrin jeffrin 2288 Feb 18 21:54 solving-knapsack-problem.js
    -rw-rw-r-- 1 jeffrin jeffrin 1303 Feb 19 22:24 solving-knapsack-problem-a-recursive-solution.js
    -rw-rw-r-- 1 jeffrin jeffrin 1366 Feb 21 23:17 greedy-algorithm-example-coin-change-problem.js
    $
    -l use a long listing format
    -r reverse order while sorting
    -t sort by time, newest first
    here you can see that the oldest date comes first because -r is given for reverse sorting.
    Here you can see the latest file for checking new things.


  • Create table on a database using python and MySQL

    $python3  dt.py
    $cat dt.py 
    # importing required library
    import mysql.connector
    
    # connecting to the database
    dataBase = mysql.connector.connect(
    					host = "localhost",
    					user = "jeff",
    					passwd = "sixer",
    					database = "learning" )
    
    # preparing a cursor object
    cursorObject = dataBase.cursor()
    
    # creating table
    studentRecord = """CREATE TABLE STUDENT (
    				NAME VARCHAR(20) NOT NULL,
    				BRANCH VARCHAR(50),
    				ROLL INT NOT NULL,
    				SECTION VARCHAR(5),
    				AGE INT
    				)"""
    
    # table created
    cursorObject.execute(studentRecord)
    
    # disconnecting from server
    dataBase.close()
    $mysql --user=jeff -psixer
    mysql: [Warning] Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 9
    Server version: 8.0.30-1 (Debian)
    
    Copyright (c) 2000, 2022, Oracle and/or its affiliates.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> use learning;
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Database changed
    mysql> show tables;
    +--------------------+
    | Tables_in_learning |
    +--------------------+
    | STUDENT            |
    +--------------------+
    1 row in set (0.00 sec)
    
    mysql> desc STUDENT;
    +---------+-------------+------+-----+---------+-------+
    | Field   | Type        | Null | Key | Default | Extra |
    +---------+-------------+------+-----+---------+-------+
    | NAME    | varchar(20) | NO   |     | NULL    |       |
    | BRANCH  | varchar(50) | YES  |     | NULL    |       |
    | ROLL    | int         | NO   |     | NULL    |       |
    | SECTION | varchar(5)  | YES  |     | NULL    |       |
    | AGE     | int         | YES  |     | NULL    |       |
    +---------+-------------+------+-----+---------+-------+
    5 rows in set (0.20 sec)
    
    mysql>
  • Connect MySQL database using MySQL-Connector Python

    $cat fdb.py
    import mysql.connector
    
    
    conn = mysql.connector.connect(host='localhost',
    			database='learning',
    			user='jeff',
    			password='sixer')
    print(conn)
    conn.close
    $python3  fdb.py
    <mysql.connector.connection_cext.CMySQLConnection object at 0x7f143fc84790>
    $
    
  • Verifying MySQL Connector/Python installation

    $python3
    Python 3.10.6 (main, Aug 10 2022, 11:19:32) [GCC 12.1.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import mysql.connector
    >>> 
    >>> 
    >>> mysql.connector.connect(host='localhost',
    ...                         database='learning',
    ...                         user='jeff',
    ...                         password='sixer')
    <mysql.connector.connection_cext.CMySQLConnection object at 0x7f9ddd630400>
    >>> 
    $cat ted.py 
    import mysql.connector
    
    
    mysql.connector.connect(host='localhost',
    			database='learning',
    			user='jeff',
    			password='sixer')
    $
    
  • How to change root password of mysql on debian ?

    $sudo mysql
    [sudo] password for jeffrin: 
    Sorry, try again.
    [sudo] password for jeffrin: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 8
    Server version: 8.0.30-1 (Debian)
    
    Copyright (c) 2000, 2022, Oracle and/or its affiliates.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'unix';
    Query OK, 0 rows affected (0.46 sec)
    
    mysql>
  • How to create a new user and grant permissions in MySQL and also creating a database ?

    sudo mysql
    [sudo] password for jeffrin: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 9
    Server version: 8.0.30-1 (Debian)
    
    Copyright (c) 2000, 2022, Oracle and/or its affiliates.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> CREATE USER 'jeff'@'localhost' IDENTIFIED WITH authentication_plugin BY 'sixer';
    ERROR 1524 (HY000): Plugin 'authentication_plugin' is not loaded
    mysql> CREATE USER 'jeff'@'localhost' IDENTIFIED BY 'sixer';
    Query OK, 0 rows affected (0.49 sec)
    
    mysql> CREATE DATABASE learning;
    Query OK, 1 row affected (0.27 sec)
    
    mysql> UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('MyNewPass') WHERE User='root'' at line 1
    mysql> UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('MyNewPass') WHERE User='root'' at line 1
    mysql> GRANT ALL PRIVILEGES ON *.* TO 'jeff'@'localhost' WITH GRANT OPTION;
    Query OK, 0 rows affected (0.69 sec)
    
    mysql> FLUSH PRIVILEGES;
    Query OK, 0 rows affected (0.14 sec)
    
    mysql> 
  • How to install MySQL-Connector-Python module in Debian ?

    $pip install mysql-connector-python
    Defaulting to user installation because normal site-packages is not writeable
    
    Collecting mysql-connector-python
      Downloading mysql_connector_python-8.0.30-cp310-cp310-manylinux1_x86_64.whl (25.4 MB)
         ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 25.4/25.4 MB 2.3 MB/s eta 0:00:00
    Collecting protobuf<=3.20.1,>=3.11.0
      Downloading protobuf-3.20.1-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (1.1 MB)
         ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.1/1.1 MB 1.4 MB/s eta 0:00:00
    Installing collected packages: protobuf, mysql-connector-python
    Successfully installed mysql-connector-python-8.0.30 protobuf-3.20.1
    
  • How to install mysql-connector method for python in Debian ?

    $pip3 install  mysql-connector
    Defaulting to user installation because normal site-packages is not writeable
    Collecting mysql-connector
      Downloading mysql-connector-2.2.9.tar.gz (11.9 MB)
         ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 11.9/11.9 MB 2.0 MB/s eta 0:00:00
      Preparing metadata (setup.py) ... done
    Building wheels for collected packages: mysql-connector
      Building wheel for mysql-connector (setup.py) ... done
      Created wheel for mysql-connector: filename=mysql_connector-2.2.9-cp310-cp310-linux_x86_64.whl size=247966 sha256=24d2cc6f0033919bd3bbe1a59ef54a72bc3f302b9c4526115dea2c95e17d4fa7
      Stored in directory: /home/jeffrin/.cache/pip/wheels/76/48/9b/da67ff1a18fe8e9d428f9b1a177716d4a7d363d2bbe83bf6cf
    Successfully built mysql-connector
    Installing collected packages: mysql-connector
    Successfully installed mysql-connector-2.2.9
    $ 
  • Program to POST a form using HTTP with requests

     
    $cat  form_post_method.py 
    #!/usr/bin/env python3
    
    import requests
    
    data_dictionary = {'custname': 'customer','custtel': '323232',
    'size': 'large','custemail': 'email@domain.com'}
    response = requests.post("http://httpbin.org/post",data=data_dictionary)
    
    # we then print out the http status_code
    print("HTTP Status Code: " + str(response.status_code))
    
    if response.status_code == 200:
    	print(response.text)
    $python3  form_post_method.py 
    HTTP Status Code: 200
    {
      "args": {}, 
      "data": "", 
      "files": {}, 
      "form": {
        "custemail": "email@domain.com", 
        "custname": "customer", 
        "custtel": "323232", 
        "size": "large"
      }, 
      "headers": {
        "Accept": "*/*", 
        "Accept-Encoding": "gzip, deflate", 
        "Content-Length": "72", 
        "Content-Type": "application/x-www-form-urlencoded", 
        "Host": "httpbin.org", 
        "User-Agent": "python-requests/2.27.1", 
        "X-Amzn-Trace-Id": "Root=1-62fd14b6-46c6b0b363ae1c7571b8c3f0"
      }, 
      "json": null, 
      "origin": "27.57.31.195", 
      "url": "http://httpbin.org/post"
    }
    
    $       
    
    
  • Program to do digest authentication using requests module

    $cat  digest_authentication.py 
    #!/usr/bin/env python3
    
    import requests
    from requests.auth import HTTPDigestAuth
    
    url = 'http://httpbin.org/digest-auth/auth/user/pass'
    response = requests.get(url, auth=HTTPDigestAuth('user', 'pass'))
    print('Response.status_code:'+ str(response.status_code))
    if response.status_code == 200:
    	print('Login successful :'+str(response.json()))
    $
    $python3 digest_authentication.py 
    Response.status_code:200
    Login successful :{'authenticated': True, 'user': 'user'}
Design a site like this with WordPress.com
Get started