User management
A user is an entity, in a Linux operating system, that can manipulate files and perform several other operations. Each user is assigned an ID that is unique for each user in the operating system. After installation of the operating system, the ID 0 is assigned to the root user and the IDs 1 to 999 (both inclusive) are assigned to the system users and hence the ids for local user begins from 1000 onwards. In a single directory, we can create 60,000 users.
commands
cat /etc/passwd
-> this command shows list of all users in systemThe info about user is in specific format as:
username : x : user id : user group id : : /home/username : /bin/bash
id <username>
-> by using this command we can get id of usersudo useradd -m <username>
-> this will create user with mentioned usernamepasswd <username>
-> this will set password of usersudo userdel -r <username>
-> this command deletes mentioned user. If the user is part of a group then it will not be deleted directly, hence we will have to first remove him from the group and then we can delete him.
Group management
There are 2 categories of groups in the Linux operating system i.e. Primary and Secondary groups. The Primary Group is a group that is automatically generated while creating a user with a unique user ID simultaneously a group with an ID the same as the user ID is created and the user gets added to the group and becomes the first and only member of the group. This group is called the primary group. A secondary group is a group that can be created separately with the help of commands and we can then add users to it by changing the group ID of users.
commands
-
sudo groupadd <groupname>
-> this will creates group with mentioned name-
sudo usermod -G <groupname> <username>
-> this command adds user to existing groupnote: If we add a user to a group then it automatically gets removed from the previous groups, we can prevent this by the command given below
-
sudo usermod -aG <groupname> <username>
-> this command adds user to existing group without removing previous groups-
sudo gpasswd -M <user1>, <user2> ... <group>
-> To add multiple users to a group simultaneously, you can utilize the gpasswd command with the -M option. This command allows you to specify a list of usernames separated by commas.note: this will overwrite existing users in group
grep, awk, find commands
grep
The grep filter searches a file for a particular pattern of characters and displays all lines that contain that pattern. The pattern that is searched in the file is referred to as the regular expression (grep stands for global search for regular expression and printout).
To know more about regular expression click here.
commands
grep <options> pattern <files> <options>: These are command-line flags that modify the behavior of grep. <pattern>: This is the regular expression you want to search for.
case insensetive search
Displaying the count of number of matches
Display the file names that matches the pattern
Checking for the whole words in a file
Show line number while displaying the output using grep -n
Matching the lines that start with a string
Matching the lines that end with a string
find
The find command in Linux is a dynamic utility designed for comprehensive file and directory searches within a hierarchical structure. Its adaptability allows users to search by name, size, modification time, or content, providing a flexible and potent solution.
commands
find <path> <options> <expression> path: Starting directory for the search. Example: find /path/to/search options: Additional settings or conditions for the search. Example: find /path/to/search -type f -name "*.txt" expression: Criteria for filtering and locating files. Example: find /path/to/search -type d -name "docs"
Find A Specific File
in below example, it seeks "test.txt" within current folder
Search Files with a Pattern Using
In this case, it identifies files ending with ‘.sh’ within the current directory.
Empty Files and Directories
awk
Awk is a scripting language used for manipulating data and generating reports. The awk command programming language requires no compiling and allows the user to use variables, numeric functions, string functions, and logical operators. Awk is a utility that enables a programmer to write tiny but effective programs in the form of statements that define text patterns that are to be searched for in each line of a document and the action that is to be taken when a match is found within a line. Awk is mostly used for pattern scanning and processing. It searches one or more files to see if they contain lines that matches with the specified patterns and then perform the associated actions. Awk is abbreviated from the names of the developers – Aho, Weinberger, and Kernighan. What can we do with AWK? 1. AWK Operations: (a) Scans a file line by line (b) Splits each input line into fields (c) Compares input line/fields to pattern (d) Performs action(s) on matched lines 2. Useful For: (a) Transform data files (b) Produce formatted reports 3. Programming Constructs: (a) Format output lines (b) Arithmetic and string operations (c) Conditionals and loops Built in variables in awk Awk’s built-in variables include the field variables—$1, $2, $3, and so on ($0 is the entire line) — that break a line of text into individual words or pieces called fields. NR: NR command keeps a current count of the number of input records. Remember that records are usually lines. Awk command performs the pattern/action statements once for each record in a file. NF: NF command keeps a count of the number of fields within the current input record. FS: FS command contains the field separator character which is used to divide fields on the input line. The default is “white space”, meaning space and tab characters. FS can be reassigned to another character (typically in BEGIN) to change the field separator. RS: RS command stores the current record separator character. Since, by default, an input line is the input record, the default record separator character is a newline. OFS: OFS command stores the output field separator, which separates the fields when Awk prints them. The default is a blank space. Whenever print has several parameters separated with commas, it will print the value of OFS in between each parameter. ORS: ORS command stores the output record separator, which separates the output lines when Awk prints them. The default is a newline character. print automatically outputs the contents of ORS at the end of whatever it is given to print.
commands
Consider following content of file employee.txt
ajay manager account 45000 sunil clerk account 25000 varun manager sales 50000 amit manager account 47000 tarun peon sales 15000 deepak clerk sales 23000 sunil peon sales 13000 satvik director purchase 80000
Default behavior of Awk: By default Awk prints every line of data from the specified file.
awk '{print}' employee.txt
Print the lines which match the given pattern.
Printing specfic columns
Display Line Number
Display Line From specific line number to specific line number
file permission
There’s a lot of information in those lines.
The first character indicates it is file or directory.
The next nine characters (rw-r–r–) shows the security.
The next column shows the owner of the file. (Here it is
root
)The next column shows the group owner of the file. (Here it is
root
which has special access to these files)The next column shows the size of the file in bytes.
The next column shows the date and time the file was last modified. Last Column = File_name or Directory_name. (For example, here 90DaysOfDevOps, B, C, test.txt etc...)
commands
chmod <permisions> <file/directory>
to change permissions of file/directory we can use command
we can cahnge file permission's using to way one is using letters and other is using numbers (octal)
using letters
to give either read or write or execute permisson to user/group/other, following are the exapmles
chmod u+w <file/directory>
-> this will append write permission to user for mentioned file/dirchmod o+x <file/dirr>
-> this will append execute permission to others for mentioned file/dirchmod u+w,g+r,o+x <file/dir>
-> this will append write permission to user, read permision to group and execute permission to others for mentioned file/dirchmod o-x <file/dir>
->this will remove execute permission from user for mentioned file/dir
using numbers
for numeric permission following table is required
Octal | Binary | Permissions |
0 | 000 | --- |
1 | 001 | --x |
2 | 010 | -w- |
3 | 011 | -wx |
4 | 100 | r-- |
5 | 101 | r-x |
6 | 110 | rw- |
7 | 111 | rwx |
in short, we can say value for 'r' is '4', value for 'w' is '2' and value for 'x' is '1', using summation we can assign permissions
chmod 755 <file/dir>
means user has all permissions i.e. rwx: 4 + 2 + 1 = 7 group has read and execute permissions not write i.e. r-x: 4 + 1 = 5 similar to others