Lab 2
Lab 2
Lab 02
BS Software Engineering
Fall-2024
Page 1
Page 2
Lab Policy and Rules:
1. 100% attendance is mandatory. In case of an emergency, you can avail yourself of up to 3 absences.
So, don't waste your absences , save them for emergencies. If you are debarred from the course due to
low attendance, do not come to me to correct it.
2. Disturbing the class environment during lab sessions. such as by talking, using mobile phones, eating,
etc. will result in penalties (e.g., deduction of marks).
3. Lab tasks will not be accepted if you are absent. If you miss a lab, you cannot submit that lab task.
4. Lab demos for each lab task will be conducted at the end of each session. If you miss the demo for
any reason, no retake will be allowed, and that lab will be marked as 0.
5. All quizzes will be unannounced. Be prepared for surprise quizzes. Quizzes will cover content from
previous labs as well as theory lectures from the corresponding theory course.
6. You can take help from the internet for lab tasks, but simply copying and pasting without
understanding will be marked as 0. You should be able to explain the syntax or material used in your
tasks.
7. Do not ask for personal favors. If you have concerns, such as short attendance, please speak with the
relevant authority (e.g., academics).
8. Students on warning: Now is the time to study and earn a good grade. Do not ask for extra marks at
the end if you are unable to clear your warning.
Page 3
Single Commands
To clear the screen
Syntax: $ clear
To view commands history
Syntax: $ history
To display first 10 lines of a file
Syntax: $ head file.txt
To display first 6 lines of a file
Syntax: $ head —n 6 file.txt
To display first 5 lines from 2 files
Syntax: $ head —n 5 title .txt file2.txt
To display last 10 lines of a file
Syntax: $ tail file.txt
To display last 6 lines of a file
Syntax: $ tail —n 6 file.txt
The wc command without passing any parameter will display a basic
result Of "file_txt' file. Result Of wc command will be (number of lines),
(number or words) and (number of chars/bytes) of the file.
Syntax: $ wc file.txt
To display the number or characters in a file
Syntax: $ wc —c file.txt
To display the number of lines
Syntax: $ wc—I file.txt
To display the number of words
Syntax: $ wc —w file.txt
TO display number Of lines with numbers
Syntax: $ nl file.txt
To sort the content of file
Syntax: $ sort file.txt
To sort the content Of file in reverse orderz
Page 4
Syntax: $ sort -r file-txt
To display the calendar.
Syntax: $ cal
To display system date.
Syntax: $ date
To display the login user details
Syntax: $ whoami
To display the contents of a file, line by line and page by page. we can use
less command.
Syntax: $ less file.txt
Note: to move up or down line by line use Arrow keys. And to move up
page by page use 'b' key and for move down use Space key. To quit form
the less command use ‘q' key.
To copy the file to another file
$ cp file1 -txt file2.txt
To copy a file within the other directory
$ cp file2.txt "Documents/directory1
To move a file to another directory
$ rmv file1.txt directory2
$ mv —Documents/file1.txt directory3
To rename a file:
$ mv file1.txt file2.txt
$ mv —Documents/file1.txt —/Documents/file2.txt
To delete a file
$ rm file1.txt
$ rm Dir1/file2.txt
find command: Find command is used to search and locate the files and
directories.
Find a specific file by name in current working directory and sub Directories of
current working directory.
$ find —name filename
Page 5
E.g: $ find —name file1.txt
Find a specific file by name in specified path(Absolute or Relative) and all sub
Directories of specified path.
$ find path —name filename
E.g: $ find ~/ Videos —name file2.txt
Find specific directory by name in current working directory and sub Directories
Of current working directory.
$ find —name directory
E.g: $ find —name D1
Find a specific directory by name in specified path(AbsoJute or Relative) and all
sub Directories of specified path.
$ find path —name filename
E.g: find ../Music —name M1
ownership and permissions, permission classes and changing permissions, user
management in linux, users and groups creation.
• Read: For a file, the read permission allows a user to view the
Contents Of a file. For directories. the read permission allows the user to
view the names of files and other directories stored in it.
• Write: For a file. the write permission allows a user to create. modify
a file. For directories. the permission allows a user to modify Its contents
(create. delete rename files in it).
Page 6
However. this permission has no effect on directories unless the execute
permission is also enabled.
• Execute: When set on a file. it works files that are executable like
script files. C executable files etc. However. the permission has no effect
on a file unless the read permission is also enabled. On the other hand.
for directories the execute permission allows the user to enter the
directory.
Permission Classes
For any file or directory. there are three types of "permission classes". You can
assign different permissions to these classes, and thus control who can access and
modify files. The permission classes are as follows:
• Group: Permissions in this class affect the group which owns the file.
However. if the owner user is in this group, the "user" permissions apply.
instead of the group permissions.
• Other: Permissions in this class affect all other users on the system.
Viewing permissions
The easiest way to view the permissions of files in a given directory is to run:
ls -l
After the execution of command (ls Output of command shows, the "file mode"
column which displays the file type and permissions in a compact way, as shown
below:
Page 7
The first character represents the type of file, e.g A normal file is represented with
a -, directories with a d etc. I-hen. we have the permissions often corresponding
to the “user". "group" and "other" classes. The triplet rwx represents whether
these classes have the read. write and execute permissions. If a - appears in this
triplet. it means (hat the corresponding permission is disabled.
Page 8
Define File Permission with octal/numeric Mode
Another way to specify permission is by using the octal/numeric format. This
option is faster, as it requires less typing, although it is not as straightforward as
the previous method instead of letters. the octal format represents privileges with
numbers:
read has the value of4
write has the value of 2
execute has the value of I
no permission has the value of 0
The privileges are summed up and depicted by one number. Therefore. the
possibilities are:
7 — for read. write. and execute permission
6 — for read and write privileges
Page 9
chmod 774 file.txt
above command gives read, write and execute to user and groups while read only
to others.
This is a key difference between su and sudo. su( short for substitute user)
switches you to the root user account and requires the root account’s password.
sudo( short for superuser do) runs a single command with root privileges – it
doesn’t switch to the root user or require a separate root user password.
Both su and sudo are used to run commands with root user permissions. The root
user is basically equivalent to the administrator user on Windows – the root user
has maximum permissions and can do anything to the system. Normal users on
Linux run with reduced permissions – for example, they can’t create users, install
software. To do something that requires these permissions, you’ll have to acquire
them with su or sudo. Eg: sudo passwd root
su command
The su command switches to the super user or root user from current login user.
You’ll have to enter the root account’s password. This isn’t all the su command
does, though – you can use it to switch to any user account. If you execute the su
Ahmed command, you’ll be prompted to enter Ahmed user password and the
shell will switch to Ahmed’s user account. Once you’re done running commands
Page 10
in the root shell, you should type exit to leave the root shell and go back to
limited-privileges mode. Following command will switch to root user account
after entering the root user password.
$ su -
sudo command
sudo runs a single command with root privileges. With sudo, one or more users
are granted superuser privileges on an as needed basis. To execute a command as
the superuser, the desired command is simply preceded with the sudo command.
After the command is entered, the user is prompted for the their own password
rather than the superuser's. By default, password for fifteen minutes and won’t
ask for a password again until the fifteen minutes are up. Following syntax will
execute the command having superuser privileges after entering own password
by the user.
$ sudo command
By default root user is disabled. To enable the root user account, use the
following command to set a password for it.
Sudo will prompt you for your current user account’s password before you can set
a new password of root password. Use your new password to log in as root with
the su command.
Page 11
User Management in Linux
To add users, using the useradd command: $sudo useradd -m ali . You can
also use sudo adduser ali command . This is high-level command and
useradd is low level-command.
For example, if you want to add the user named Ahmed, then the
command will be like: $sudo useradd -m Ahmed Note: useradd create a
home directory, we’ve used the -m switch.
To view all added users : ls /home
To view GID and UID : grep 'user1\|user2\|user3' /etc/passwd
To view GID and UID for current user : id
To view GID and UID for specific user : id user1
At this point, the user has been created, but they don’t have a password
and can’t log in. So, to assign a password to the newly created user, run the
passwd command: $sudo passwd For example, to set the password of
Ahmed: $sudo passwd Ahmed.
Note: The command will ask for the new password, and ask you to confirm
it:
To add a new user and also set its password, we can use –p option to set its
password:
Page 12
For example $sudo useradd –m Ahmed –p 123
We can use the userdel command to delete user account and all the related
directories/files. $ sudo userdel -r Asif
Every user has some user ID(a unique ID is assigned to a user after its creation).
In order to make managing users easier, you can add users into a “group”. A
group can have zero or more users. A particular user is associated with a “default
group”(primary group) having same name as of username, and can also be a
member of other groups(Secondary groups) on the system.
Page 13
Following command can be used to view the list the names of groups(all
groups), in which user is a member.
$ id -Gn userone
Note: First group that will be displayed in the list will be primary
group(primary group has the same name as of user name) and rest of
groups will be secondary groups.
Following command can be used to view the list the ID’s of groups(all
groups), in which user is a member.
$ id -G userone
Following command can be used to make a user a member of a group. In
following example user Ahmed has been made as a member of sudo
group. $ sudo usermod –a -G sudo Ahmed
Following command can be used to remove a user from a group. In
following example user Ahmed has been removed from sudo group.
$ sudo gpasswd –d Ahmed sudo OR $ sudo deluser Ahmed sudo
Following command can be used to add/create a new group. For example
g2 is created/added.
$ sudo groupadd g2
Following command can be used to remove a group. For example g2 is
removed.
$ sudo groupdel g2
Software/Package Management
apt-get is a command which helps in handling packages/softwares in Linux.
Its main task is to retrieve the information and packages from the
Page 14
authenticated sources for installation, upgrade and removal of packages.
Here APT stands for the Advanced Packaging Tool.
Most Used Commands:
update : The update command is used to download package information
from all configured sources. So when you run update command, it
downloads the package information from the Internet. It is useful to get
info on an updated version of packages. You need to perform an update
before you upgrade. $sudo apt-get update
upgrade : This command is used to install the latest versions of the
packages currently installed on the user’s system. The installed packages
which have new packages available are retrieved and installed. You need to
perform an update before the upgrade, so that apt-get knows that new
versions of packages are available. $sudo apt-get upgrade
install : This command is used to install packages. In the following example
vlc mediaplayer is being installed. $sudo apt-get install vlc
remove : This is similar to install, with the difference being that it removes
the packages instead of installing. $sudo apt-get remove vlc
Process Management
Linux bash terminal has a number of commands that can be used to find
information about the running processes.
$ ps command
The ps command lists running processes. The output includes information
about the shell(bash terminal ) and the process running in the shell.
$ ps –e command
Page 15
The ps command with –e option will display all the processes in the system.
$ ps –ef command
The ps command with –f option will provide full-format listing, which
provides detailed information about the processes.
$ ps –C ProcessName Command
Using ps command with –C option and giving the process name, it will as a
result give us the PID(process id) of specified process name.
$ top command
The top command provide the view of processes according to resource
usage and see the processes that are taking up the system resources.
$ kill ProcessID command
The kill command signal can kill a process, given its process ID.
Page 16
$ echo SomeString command
echo is a command that outputs the string it is being passed as arguments.
For example: $echo “Hello” will give us the output “Hello”
pipe command
In linux, the pipe command(represented as | symbol), sends the output of
one command to another command as an input. For Example: In the
following example output of ls –l command is send to less command as an
input. $ ls –l | less
1: sudo visudo
OR
Page 17