unit2 notes
unit2 notes
Shell Prompt:
o $ → Regular user prompt.
o # → Root user prompt.
Usage: If the GUI is unavailable, the shell prompt is your primary interface for
interacting with the Linux system.
Ctrl+Alt+F1
Example: Press Ctrl+Alt+F3 to open a plain-text login prompt, then log in with your
credentials.
Replace username with your actual username. The output shows the shell
configuration.
1. Local Variables: Defined in the current shell session and not inherited by child
processes.
2. Environment Variables: Exported to child processes and used system-wide.
3. Special Variables: Predefined by the shell, such as $0, $1, $?.
1. Assigning a Variable
variable_name=value
Example:
myvar="Hello, World!"
echo $myvar
Output:
Hello, World!
2. Accessing a Variable
echo $variable_name
3. Environment Variables
export variable_name=value
Example:
export PATH=$PATH:/new/directory
echo $PATH
Local Variables:
set
Environment Variables:
env
variable_name=$(command)
Example:
today=$(date)
echo $today
Output:
sql
CopyEdit
[Current date and time]
6. Deleting a Variable
unset variable_name
Output:
Output:
x=10
y=5
sum=$((x + y))
echo "The sum is $sum"
Output:
The sum is 15
first="Hello"
second="World"
combined="$first $second"
echo $combined
Output:
Hello World
current_dir=$(pwd)
echo "You are in $current_dir"
Output:
Special Variables
Example:
man ls
This shows the manual for the ls command, which lists directory contents.
ls --help
This gives a brief summary of how to use the ls command and its available options.
View groups of a
groups username groups alice
user
1. What is User Management?
User management involves creating, modifying, and deleting users on a Linux system.
It ensures system security and proper access control for different users.
Command:
Example:
This creates a new user alice and sets up their home directory (/home/alice).
Command:
Example:
c) Deleting a User
Command:
Example:
su - username
Example:
su - alice
Command:
whoami
Group management allows managing permissions and access for multiple users at
once.
Command:
Example:
sudo groupadd developers
Command:
Example:
Command:
groups username
Example:
groups alice
Command:
Example:
e) Deleting a Group
Command:
Example:
Practical Examples
groups bob
# Delete a group
1.ls
ls
Output:
2. ls -a
Purpose: Lists all files, including hidden files (those starting with a dot .).
Example:
ls -a
Output:
3. ls -l
ls -l
Output:
Purpose: Displays detailed information about the contents of the collection/ directory.
Example:
ls -l collection/
Output:
5. ls -ld collection/
Purpose: Displays detailed information about the collection/ directory itself, not its
contents.
Example:
ls -ld collection/
Output:
6.ls –aF
To classify the files and directories, you can use ls –aF command
Note: which one is ended with “/” is considered as directories and not ended with
“/” considered as file
7. ls -i
ls -i
Output:
8. ls -lt /home/student/
Purpose: Lists the contents of /home/student/ sorted by modification time (newest
first).
Example:
ls -lt /home/student/
Output:
9. ls -ltr /home/student/
Purpose: Lists the contents of /home/student/ sorted by modification time (oldest first).
Example:
ls -ltr /home/student/
Output:
ls -sh /home/student/
Output:
11. ls -s /boot
Purpose: Displays the size of each file in blocks (default block size).
Example:
ls -s /boot
Output:
32 config-5.11.0 64 initrd.img
12. ls -R /home/
Purpose: Recursively lists all files and subdirectories in /home.
Example:
ls -R /home/
Output:
/home/user:
documents downloads
/home/user/documents:
file1.txt file2.pdf
Purpose: Displays the directory structure in a tree-like format. (You may need to
install tree first.)
Example:
tree /home
Output:
/home
├── documents
├── downloads
└── pictures
14. history
history
Output:
1 ls
2 cd /home
3 pwd
Output:
/etc
cd ..
pwd
Output:
/
Using cd – to change the directory to previous working directory
cd
pwd
Output:
/root
cd collection
pwd
Output:
/root/collection
3. Paths
Absolute Path:
Example:
cd /root/collection
pwd
Output:
/root/collection
Relative Path:
Example:
cd collection
pwd
Output:
/root/collection
touch app
ls
Output:
app
Output:
touch app{1..5}.txt
ls
Output:
mkdir project
ls
Output:
project
Output:
mkdir -p office/departments/{Sales,Marketing,Accounts}
ls office/departments
Output:
cp app1.txt dir1/
ls dir1
Output:
app1.txt
app2.txt app3.txt
cp -r dir1 dir3/
ls dir3
Output:
dir1
mv app4.txt dir2/
ls dir2
Output:
Rename a file:
Example:
mv app5.txt renamed_app5.txt
ls
Output:
renamed_app5.txt
Delete a file:
Example:
rm app1.txt
ls
Output:
app2.txt app3.txt
Delete multiple files:
Example:
rm app2.txt app3.txt
ls
Output:
dir1 dir2
rm -r dir1
ls
Output:
dir2
Use the date command to display the current time and date.
Use lsblk command to check the hard disk and partition informations
Use uptime to check the machine load average.
Use cal, cal -3, cal –y (year) to list all the information of calendar in monthly
Use bc command is for opening the basic calculator (use ctrl+c or ctrl+d to close)
Permission Management
Key Notes
chmod manages file permissions:
o Symbolic: u (user), g (group), o (others), a (all)
o Numeric: r=4, w=2, x=1
chown changes file or directory ownership.
chgrp changes group ownership.
Explanation of Output
1. user::rw-: Default file owner (mary) has read and write permissions.
2. user:bill:rw-: Specific user bill has been granted read and write
permissions.
3. group::rw-: Default group (mary) has read and write permissions.
4. group:sales:rw-: Additional group sales has been granted read and write
permissions.
5. mask::rw-: The mask defines the maximum allowable permissions for
users and groups. If the mask restricts a permission, it will override the
user/group ACL settings.
6. other::r--: All other users not explicitly listed can only read the file.