Advance deep dive in Linux

Advance deep dive in Linux

In this blog, you'll get to know about some more terms and commands relevant to the Linux operating system that is used very comprehensively.

Extensive fundamentals of Linux

Advance commands with Shell scripting

Creating directory or directories the recursive way

$ mkdir day{1..90}
$ ls
backup  day14  day2   day25  day30  day36  day41  day47  day52  day58  day63  day69  day74  day8   day85  day90
day1    day15  day20  day26  day31  day37  day42  day48  day53  day59  day64  day7   day75  day80  day86  shell
day10   day16  day21  day27  day32  day38  day43  day49  day54  day6   day65  day70  day76  day81  day87
day11   day17  day22  day28  day33  day39  day44  day5   day55  day60  day66  day71  day77  day82  day88
day12   day18  day23  day29  day34  day4   day45  day50  day56  day61  day67  day72  day78  day83  day89
day13   day19  day24  day3   day35  day40  day46  day51  day57  day62  day68  day73  day79  day84  day90
  1. Previous making directory using a shell script

     #!/bin/bash
    
     echo "making directories"
    
     mkdir daydire
     cd daydire
     mkdir day{1..90}
     ls
    

  2. Second script for print movies 1-50 using loop conditions

     #!/bin/bash
    
     mkdir movies
         cd movies
     # loop through numbers 1 to 50
     for i in {1..50}; do
       # create directory named "movie" with number appended
       mkdir "movie$i"
     done
    

About Cron & Crontab

Cron is a time-based job scheduler in Unix-like operating systems, including Linux. It allows users to schedule tasks to run automatically at specified intervals, such as daily, weekly, or monthly.

Crontab is a file that contains the list of cron jobs for a user. Each line in the crontab file represents a command to be executed, along with the schedule for when the command should be run. The crontab file can be edited using the "crontab" command, which allows users to add, remove, and modify their cron jobs.

Backup script using cron

#!/bin/bash

# Define variables
BACKUP_DIR="/path/to/backup/directory"
DATE=$(date +%Y-%m-%d-%H-%M-%S)
ARCHIVE_NAME="backup-$DATE.tar.gz"
SOURCE_DIR="/path/to/source/directory"

# Create backup directory if it doesn't exist
if [ ! -d $BACKUP_DIR ]; then
    mkdir -p $BACKUP_DIR
fi

# Create the backup archive
tar -czf $BACKUP_DIR/$ARCHIVE_NAME $SOURCE_DIR

# Delete backups older than 7 days
find $BACKUP_DIR/* -mtime +7 -exec rm {} \;

User Management In Linux

In Linux, a user is an account that a person, process, or system can use to access the resources of the operating system. A user account defines a username and a password, and it provides a way to authenticate a user and control access to files, directories, and other resources on the system.

It also supports the concept of groups, which allow multiple users to share resources and permissions. A group is a collection of user accounts that share the same permissions and access rights. Users can belong to one or more groups, and groups can be used to control access to files, directories, and other resources on the system.

User accounts are typically stored in the system's user database, which is usually located in the /etc/passwd file. In addition to the username and password, a user account can also have other attributes, such as a home directory, a default shell, and a user group.

sudo adduser <username> #to add a user
cat /etc/groups #to look into no of users in our system

File Permissions and Access Control list

In Linux, file system management involves creating, organizing, and managing files and directories on a Linux file system. Linux uses a hierarchical file system structure, where files and directories are organized into a tree-like structure that begins at the root directory ("/") and branches out into subdirectories and files.

File permissions control who can read, write, and execute a file or directory. Each file and directory has three sets of permissions, which are set for the owner of the file, the group that owns the file, and all other users.

The three types of permissions that can be set are:

  • Read permission (r): Allows a user to view the contents of a file or directory.

  • Write permission (w): Allows a user to modify or delete a file or directory.

  • Execute permission (x): Allows a user to execute a file or traverse a directory.

File permissions can be changed using the chmod command, which allows the setting of permissions using either numeric or symbolic notation. Numeric notation involves assigning a three-digit number to each set of permissions, based on the sum of the permissions that are granted (e.g. 755 means read, write, and execute permissions for the owner, and read and execute permissions for the group and other users). Symbolic notation involves using symbols such as u (user/owner), g (group) & o (other) to specify which permissions to add or remove (e.g. chmod u+x filename.txt or chmod 707 filename.txt adds execute permission for the owner of the file).

vim filename.txt #to create a text file
ls -ltr #to check out the whole permission sets of the file
chmod 777 filename.txt #this means the whole file permission has been changed to full mode i.e r+w+x for all u+g+o which denotes as 777 total

Mode of permissions

  1. Owner permissions (u): This category applies to the owner of the file or directory. The owner is the user who created the file or directory. Owner permissions are usually the most restrictive, as they determine what actions the owner can perform on the file or directory. The owner can be granted read, write, and execute permissions.

  2. Group permissions (g): This category applies to a group of users who have been given access to the file or directory. Group permissions allow all members of the group to perform the same actions on the file or directory. Group permissions are often used to grant access to a particular project or department. The group can be granted read, write, and execute permissions.

  3. Other permissions (o): This category applies to all users who are not the owner of the file or directory and not a member of the group. Other permissions determine what actions any other user can perform on the file or directory. The other category can be granted read, write, and execute permissions.

One of the important command i.e chmod . Where each category of permissions can be set separately using the chmod command. File and directory permissions are represented by a 10-character string, where the first character indicates the type of file and the next three characters represent owner permissions, followed by three characters for group permissions, and three characters for other permissions.

ACL (getfacl and facl)

Access Control Lists (ACLs) provide an additional layer of permission control for files and directories. It's a set of rules that defines access permissions for individual users or groups on a file or directory. Each ACL entry specifies a user or group and the permissions that they are granted, which can include read, write, and execute permissions, as well as other permissions such as delete, setuid, or setgid.

The getfacl command is used to display the ACLs for a file or directory. The output of getfacl shows the users and groups that have ACL entries, as well as the specific permissions granted to each user or group.

The setfacl command is used to modify the ACLs for a file or directory. You can use setfacl to add or remove ACL entries, modify existing ACL entries, or reset the ACLs to default values.

In addition, some filesystems may support POSIX ACLs, which are similar to ACLs but are integrated with the traditional Unix permissions system. The getacl and setacl commands can be used to view and modify POSIX ACLs on files and directories.

Overall, ACLs provide a more granular level of permission control than traditional Unix permissions, allowing administrators to grant specific permissions to individual users or groups. However, managing ACLs can be more complex than managing traditional Unix permissions, and not all filesystems support ACLs.

Thanks for learning...!

Upcoming blog on Package manager and advance commands.