LINUX BASICS
LINUX BASICS
ls [OPTION]... [FILE]...
OPTIONS
ls -l
ls -a
ls -r
ls -R
ls -S
ls -t
ls --color=no
ls --color=yes
Examples:
2. Change Directory cd
Search for help for the ls command
Note: The cd command is used to change the current working directory, and it
does not have many options like other commands. However, it can be used with
various arguments to navigate through directories.
cd --help
man cd
Absolute Path
Relative Path
Examples:
man pwd
pwd --help
Examples:
`pwd
man pwd - show manual page Note: The pwd command is used to print the current
working directory, and it has only a few options to modify its output.
man mkdir
mkdir --help
Examples:
rmdir --helo
man rmdir
Examples:
Note: The rmdir command is used to remove empty directories, and its options allow
for customization of the removal process.
Examples:
Note: The touch command is used to create a new empty file or update the timestamp
of an existing file.
man rm
rm --help
Examples:
Note: The rm command is used to remove files and directories, and its options allow for
customization of the removal process.
man cp
cp --help
Examples:
man mv
mv --help
Examples:
Note: The mv command is used to move or rename files and directories, and its options
allow for customization of the move process.
COURSE: LINUX OPERATING SYSTEM TOPIC: USERS, GROUPS AND
PERMISSIONS INSTRUCTOR: ZAKARIYYA DAHURU MUSA
OBJECTIVES
Understand Linux users and group
Manage users and groups
Modify file permissions and ownership
USERS Users are entities that interact with with the system directly or indirectly, they are
identified by a unique username and user ID (UID) with associated permissions, group
memberships and resources.
TYPES OF USERS
Root User: This is a super user with unlimited privileges and access to all system
resources. username = root
System Users: Created by the system for specific purposes such as:
Daemon users e.g. http, ftp which can be use to run system services and
daemons
System Service users: Users that manage system services e.g. MySQL
Regular Users: account created for individual people or applications with limited
privilege's and access to system resources.
Group users: Members of a group sharing common permissions and access to
resources
USERS CHARACTERISTICS
USER COMMANDS
GROUPS A group is a collection of users who share common permissions, access rights,
and resources. It helps manage user permissions and access control.
Group name - Unique identifier for the group
Group ID (GID) - Numeric identifier for the group
Members - users who belong to the group
Permissions - Access rights and priviledges assigned to the group
TYPES OF GROUP
Primary group - A user's default group, assigned when the user is created
Secondary group - Additional groups a user belongs to , providing extra permissions
BENFIT OF GROUPS
GROUP COMMANDS
TYPES OF PERMISSION
PERMISSION COMMAND
example
chmod 655 filename = rw,rx,rx
Using Letters
u = user
g = group members
o = other users
id user2
Deleting a user
STEPS
user1:password1:1002:1002::/home/user2:/usr/bin/zsh
user2:password2:1003:1003::/home/user3:/usr/bin/zsh
user3:password3:1004:1004::/home/user4:/usr/bin/zsh
run the newuser command and redirect to the file newusers < new_users
Check existing users
getent passwd
who
\
GROUP MANAGEMENT Adding a new group
getent group
Deleting a group
groupdel <group_name>
getent group
Add user to a specific group
OBJECTIVES
Perform advanced file operations.
Use text processing tools and commands.
File Operations
Text Processing
File Editing: Modifying file contents using editors or commands like sed
GUI
mousepad
CLI
nano
gedit
vim
USING find COMMAND The find command is a powerful tool in Linux and Unix-like
operating systems that allows you to search for files and directories based on various
criteria such as name, type, user etc.
Components:
Path: The directory path where the search starts. If omitted, the current directory is
used.
Options: Modifiers that affect the search behavior, such as -name, -type, -size, etc.
Expression: The search criteria, such as a filename, size, or modification time.
find / -type f -size +1M = files larger than 1MB in the entire file
system.
find . -mtime -1 = find . -mtime -1: files modified in the last 24 hours
in the current directory and subdirectories.
ADVANCE options FOR find
-type: Search for files by type (e.g., d for directories, f for files).
-size: Search for files by size.
Note:
-mtime -1 means "less than 1 day ago" (i.e., in the last 24 hours)
-mtime +7 means "more than 7 days ago"
-mtime 3 means "exactly 3 days ago"
Note:
-atime -1 means "less than 1 day ago" (i.e., in the last 24 hours)
-atime +7 means "more than 7 days ago"
-atime 3 means "exactly 3 days ago"
-ctime -1 means "less than 1 day ago" (i.e., in the last 24 hours)
-ctime +7 means "more than 7 days ago"
-ctime 3 means "exactly 3 days ago"
Examples
grep can also be used with pipes (|) to search output of other commands:
Other Options
TERMS
A FIELD
Examples
```bash
awk '{print $1}' file.txt = prints the first column (field) of each line
in file.txt
awk '/AIVTIC/ {print $0}' file.txt = This prints entire lines from
file.txt containing the string "AIVTIC"
awk '{print $1 "," $2 "," $3}' file.txt = This prints the first three
fields of each line in file.txt, separated by commas.
CONCATENATING
awk '{print $1 $2}' - Concatenates the first and second fields without a
separator.
awk '{print $1 "." $2}' - Concatenates the first and second fields with
a period in between.
MODIFYING
awk '{ $1 = "AIVTIC"; print $0 }' - Replaces the first field with
"AIVTIC" and prints the entire line.
awk '{ $2 = $2 + 10; print $0 }' - Increments the second field by 10 and
prints the entire line.
ARITHMETIC OPERATION
awk '{ $1 = $2 + $3; print $0 }' - Sets the first field to the sum of
the second and third fields and prints the entire line.
awk '{ $2 = $1 * 2; print $0 }' - Multiplies the second field by 2 and
prints the entire line.
STRING OPERATION
awk '{if ($2 < 10) $2 = 10; print $0}' - Sets the second field to 10 if
it's less than 10 and prints the entire line.
USE CASES
Text replacement
Text deletion
Text insertion
TEXT REPLACEMENT
A package manager is a utility that automates the installation, update, and removal of
software packages, making it easier to maintain and configure your Linux system. It acts
as a central repository for software packages, allowing you to:
Update existing packages to the latest versions, ensuring you have the latest
security patches and features.
Install new software packages with ease, ensuring dependencies are resolved and
conflicts are avoided.
Remove unwanted packages, freeing up disk space and reducing clutter.
Open a terminal and run the command: sudo apt install package name
Enter your password to authenticate
The software package will be installed
Open a terminal and run the command: sudo apt remove package name
Enter your password to authenticate
The software package will be removed
Open a terminal and run the command: sudo yum remove package name
Enter your password to authenticate
The software package will be removed
REMOVE VLC
wget http://testphp.vulnweb.com/login.php
BENEFITS OF SCRIPTING
Automate repetitive tasks: Perform tasks without human intervention, saving time
and reducing errors.
Simplify complex processes: Break down intricate tasks into manageable, step-
by-step instructions.
Increase productivity: Focus on high-priority tasks while scripts handle routine or
mundane work.
Improve accuracy: Reduce human error by ensuring tasks are executed
consistently and correctly.
Enhance flexibility: Easily modify or update scripts as needs change, without
affecting underlying systems.
Configuration management: Scripts can be used to configure software
environments, set up systems, and manage application deployment
System Admin: System administrators use scripts to manage user accounts,
monitor system health, automate updates, and handle other administrative tasks.
This reduces the workload and ensures consistency across the systems they
manage.
The shell is, after all, a real programming language, complete with variables, control
structures, and so forth. No matter how complicated a script gets, it is still just a list of
commands executed sequentially.
EXECUTING A SCRIPT
#!/bin/sh
echo "Welcome to AIOVTI?"
read student
echo "Hello, $student"
#!/bin/sh This tells the system that the commands that follow are to be executed by
the Bourne shell. it's called a shebang because the # symbol is called a hash, and the !
symbol is called a bang.
COMMENTS A good script must have comments for readability, we use # to declare
comments in scripting. A comment is not executed by the interpreter. It gives information
about a line of code or the entire script.
#!/bin/sh
# Welcome message
echo "Welcome to AIOVTI?"
VARIABLES A variable is a container that allows us to store data from a user input or by
declaring it. A variable name can have:
VALID VARIABLES
_MUSA
AIOVTI_A
VAR_1
VAR_2
INVALID VARIABLES
2_MUSA
-VARIABLE
@MUSA
VAR_A!
TYPES OF VARIABLES
ARRAY VARAIBLES
colors=("red" "green" "blue")
INTEGAR VARIABLES
Age=20
STRING VARIABLES
Batch="Batch C24
LOCAL VARIABLES
local greetings="Hello, World!"
GLOBAL VARIABLES
greetings="Hello, World!"
READONLY VARAIBLES
readonly PI=3.14
SPECIAL VARIABLES
#!/bin/bash
ARITHMETIC OPERATIONS
+ (Addition) Adds values on either side of the operator
!= (Not Equality)
RELATIONAL OPERATOR Bourne Shell supports the following relational operators that
are specific to numeric values. These operators do not work for string values unless their
value is numeric.
STRING OPERATORS
FILE OPERATORS
IF STATEMENT
if condition; then
# code to execute if condition is true
fi
if [ $x -gt 10 ]; then
echo "x is greater than 10"
else
echo "x is less than or equal to 10"
fi
IF-ELSE STATEMENT
IF-ELSE STATEMENT
if condition; then
# code to execute if condition is true
else
# code to execute if condition is false
fi
IF-ELIF-ELSE STATEMENT
IF-ELIF-ELSE STATEMENT
bash
if condition1; then
# code to execute if condition1 is true
elif condition2; then
# code to execute if condition1 is false and condition2 is true
else
# code to execute if both conditions are false
fi
if [ $x -gt 10 ]; then
echo "x is greater than 10"
elif [ $x -eq 10 ]; then
echo "x is equal to 10"
else
echo "x is less than 10"
fi
LOOPS Loops are a fundamental concept in programming that allow you to execute a
block of code repeatedly for a specified number of times or until a certain condition is
met.
For loop:
#!/bin/bash
BENEFITS OF LOOPING
Repeating tasks
Iterating over data structures
Implementing algorithms
Simplifying code
PRACTICAL EXERCISES
#!/bin/bash
#!/bin/bash
# Loop from 1 to 10
for i in {1..20}
do
echo $i
done
#!/bin/bash