Monday, 10 May 2021

Linux Permission Overview

In Linux, every file and directory has permissions that control who can read, write, and execute the file or access the directory. These permissions are represented by a series of letters and symbols.

There are three types of permissions in Linux:

  • read (r) - Allows a user to view the contents of a file or list the files in a directory.
  • write (w) - Allows a user to modify the contents of a file or add or delete files in a directory.
  • execute (x) - Allows a user to run a file as a program or access files in a directory.

These permissions are assigned to three groups of users:

  • user (u) - The owner of the file or directory.
  • group (g) - The group that the file or directory belongs to.
  • others (o) - Any user that is not the owner or a member of the group.

You can use the ls -l command to view the permissions of files and directories in the current directory. The output will be a list of files and directories, with the permissions for each item listed in the first column. The permissions are represented by a series of letters and symbols, where the first three characters represent the permissions for the user, the next three characters represent the permissions for the group, and the last three characters represent the permissions for others.

Here is an example of the ls -l output:

drwxrwxr-x  2 user group   4096 Jan  1 12:00 myfolder
-rw-rw-r--  1 user group    123 Jan  1 12:00 myfile.txt

In this example, the first character din the first line shows that "myfolder" is a directory.

The next three characters rwxshow that the user has read, write, and execute permissions on the directory. The next three characters rwxshow that the group has read, write, and execute permissions on the directory. The last three characters r-xshow that others have read and execute permissions on the directory but do not have write permissions.

The first character - in the second line shows that "myfile.txt" is a regular file. The next three characters rw-show that the user has read and write permissions on the file.

The next three characters rw-show that the group has read and write permissions on the file. The last three characters r--show that others have only read permissions on the file but do not have write or execute permissions.

You can use the chmod command to change the permissions of a file or directory. The chmod command takes two arguments: the first is the permissions you want to set, and the second is the file or directory you want to change.

There are two ways to specify the permissions in the chmod command:

  1. Symbolic mode: This method uses symbols to specify the permissions. The symbols used are:

    • r (read)
    • w (write)
    • x (execute)

      The symbolic mode also uses the following symbols to specify which users the permissions apply to:

    • u (user/owner)

    • g (group)
    • o (others)
    • a (all)

To set permissions using the chmod command, you can use a combination of the above options and the following symbols:

    • (plus): Adds the permission.
    • (minus): Removes the permission.
  • = (equals): Sets the permission exactly as specified.

Example:

  1. To give the owner, group, and others read and write permissions on a file:
chmod a+rw file.txt

In this example, represents all users (owner, group, and others), and +rwadds read and write permissions.

b. To give the owner execute permission and remove write permission on a file:

chmod u+x,g-w file.txt

In this example, u+xadds execute permission for the owner, and g-wremoves write permission for the group.

c. To give the owner read and write permission, and remove all permissions for the group and others:

chmod u+rw,g-rwx,o-rwx file.txt

In this example, u+rwadds read and write permission for the owner, g-rwxremoves all permissions for the group, and o-rwxremoves all permissions for others.

Remember that the order of the symbolic mode options matters. The permissions are applied in the order they are specified, so make sure to specify the options in the correct order to achieve the desired result.

  1. Numeric mode: This method uses numeric values to specify the permissions. Each permission is assigned a numeric value:
  2. 4 (read)
  3. 2 (write)
  4. 1 (execute)

You can add these values to specify the permissions you want to set. For example, to give the owner read, write, and execute permissions on a file, you would use the command:

chmod 700 file.txt

In this example, the first digit (7) represents the owner's permissions (4 + 2 + 1), and the remaining digits (0) represent the group and other users' permissions.

let’s take another example:

To give the owner read and execute permissions, and remove all permissions for the group and others on a directory:

chmod 500 directory/

In this example, the first digit (5) represents the owner's permissions (4 + 1 = read and execute), and the remaining digits (00) represent the group and others' permissions.

Friday, 30 April 2021

Getting Started with Git - part one

Git is a version control system that allows you to track changes to your code and collaborate with others on software development projects. Here are the basic steps to get started with Git:

  1. Install Git on your computer. You can download the latest version of Git from the official website (https://git-scm.com/downloads).
  2. Open a command line or terminal window and navigate to the folder where you want to create your Git repository.
  3. Use the git init command to initialize a new repository in the current folder. This command creates a new subfolder called ".git" that contains the necessary files for the repository.
  4. Use the git add command to stage the files that you want to include in the next commit. For example, git add file1.txt will stage the file "file1.txt" for the next commit.
  5. Use the git commit command to save the staged changes to the repository. The m option allows you to add a message describing the changes. For example, git commit -m "Initial commit" will create the first commit with the message "Initial commit".
  6. Use the git status command to check the current status of the repository. This command will show which files have been modified, which files have been staged for the next commit, and which branch you are currently working on.
  7. Use the git log command to see a list of all commits in the repository, along with their corresponding messages.
  8. Use the git branch command to manage branches in your repository. You can use git branch to list existing branches, git branch newbranch to create a new branch, and git checkout branch to switch to a different branch.
  9. Finally, the git push origin <branch_name> (ex git push origin main) command to push the changes to remote repository.
These are the basic steps to get started with Git. As you become more familiar with Git, you can start using more advanced features such as branching, merging, and working with remote repositories.

It's important to note that Git is distributed version control system so you can work on a local repository and then push/pull changes to a remote repository. Most developers use Github, Gitlab, and Bitbucket as remote repositories.

Do comment if you're facing any issue on git.

Saturday, 17 April 2021

Introduction to Python re module

The re module in Python is used to work with regular expressions. It provides functions to search for patterns in strings, and to perform substitutions and splits. Some common functions include:

  • search(): searches for a match to a pattern in a string
  • findall(): returns all non-overlapping matches of a pattern in a string
  • sub(): replaces all occurrences of a pattern in a string with a replacement string
  • split(): splits a string by a specified pattern

The re module also includes several functions for compiling and working with regular expression patterns, including:

  • compile(): compiles a regular expression pattern into a pattern object
  • match(): attempts to match a pattern at the start of a string
  • fullmatch(): attempts to match a pattern against all of a string

Regular expressions are a powerful tool, They are mostly used to match or find the pattern in the string, You can use special characters and sets to define patterns, and you can use groups and flags to modify the behavior of the match.

It's important to note that regular expressions can be quite complex and hard to read, So, It's always a good idea to use comments in the pattern.

here are a few examples of how the re module can be used in Python:

  1. Finding all occurrences of a pattern in a string:

     import re
    
     text = "The cat is in the hat"
    
     # Find all occurrences of "at" in the text
     matches = re.findall("at", text)
    
     print(matches) 
     # Output: ['at', 'at']
    
  2. Replacing all occurrences of a pattern in a string:

     import re
    
     text = "The cat is in the hat"
     # Replace all occurrences of "cat" with "dog"
     new_text = re.sub("cat", "dog", text)
     print(new_text) 
     # Output: "The dog is in the hat"
    
  3. Splitting a string by a pattern:

     import re
    
     text = "The,cat,is,in,the,hat"
    
     # Split the text by ","
     parts = re.split(",", text)
    
     print(parts) 
     # Output: ['The', 'cat', 'is', 'in', 'the', 'hat']
    
  4. Matching a pattern at the start of a string:

     import re
    
     text = "The cat is in the hat"
    
     # Check if the text starts with "The"
     match = re.match("The", text)
    
     if match:
         print("Text starts with 'The'")
     else:
         print("Text does not start with 'The'")
    
     # Output: Text starts with 'The'
    
  5. Using groups to extract parts of a match:

     import re
    
     text = "The cat is in the hat"
    
     # Find all occurrences of "at" preceded by a word
     matches = re.findall(r"(\w+)at", text)
    
     print(matches) 
     # Output: ['cat', 'hat']
    
  6. Using a flag to make the search case-insensitive:

     import re
    
     text = "The Cat is in the Hat"
    
     # Find all occurrences of "cat" or "Cat"
     matches = re.findall("cat", text,re.IGNORECASE)
    
     print(matches) 
     # Output: ['Cat']
    
  7. Using the search() function to find a match:

     import re
    
     text = "The cat is in the hat"
    
     # Search for the first occurrence of "cat"
     match = re.search("cat", text)
    
     if match:
         print("Found a match:", match.group())
     else:
         print("No match found.")
    
     # output: Found a match: cat
    
  8. Using the compile()function to create a pattern object:

     import re
    
     text = "The cat is in the hat"
    
     # Compile a regular expression pattern
     pattern = re.compile("cat")
    
     # Search for the first occurrence of the pattern in the text
     match = pattern.search(text)
    
     if match:
         print("Found a match:", match.group())
     else:
         print("No match found.")
    
  9. Using the finditer() function to find all matches and iterate over them:

     import re
    
     text = "The cat is in the hat. The bat is in the mat."
    
     # Find all occurrences of "at"
     matches = re.finditer("at", text)
    
     # Iterate over the matches
     for match in matches:
         print("Found a match:", match.group())
    
     #Output:
     # Found a match: at
     # Found a match: at
     # Found a match: at
     # Found a match: at
    
  10. Using the escape()function to escape special characters in a string:

     import re
    
     text = "The .*+?^$[]{}\|() cat is in the hat"
    
     # Escape special characters in the text
     escaped_text = re.escape(text)
    
     print(escaped_text) 
     # Output: "The \.\*\+\?\^\$\[\]\{\}\|\(\) cat is in the hat"
    
  11. Using the purge()function to clear the regular expression cache.

     import re
    
     re.purge()
    

These are just a few examples of how the remodule can be used in Python. There are many more functions and options available in the remodule, so I recommend reading the official documentation for more information and examples.https://docs.python.org/3/library/re.html

I hope these examples help you understand the basics of working with regular expressions in Python.