Professional Documents
Culture Documents
Lab No1
Lab No1
01
INTRODUCTION TO LINUX (UBUNTU) AND
COMMAND-LINE
Task 1 10
Task 2 10
Task 3 10
Task 4 10
Task 5 10
Total
50
Marks
Lab Instructor
Lab Objectives and CLOs Mapping
CLOs
Lab Objectives
a b c
1
2
3
Instructions
▪ This is individual Lab work/task.
▪ Copy paste from the Internet will give you negative marks.
▪ Lab work is divided into small tasks, completing all tasks sequentially.
▪ In-Lab Exercises/Tasks
The development of Linux is one of the most prominent examples of free and open source
software collaboration; typically all the underlying source code can be used, freely modified,
and redistributed by anyone.
Linux was developed by "Linus Torvalds" at the University of Helsinki, with the help of UNIX
programmers from across the Internet.
Linux Distributions
A typical Linux distribution comprises a Linux kernel, GNU tools and libraries, additional
software, documentation, a window system (the most common being the X Window System),
a window manager, and a desktop environment.
Most of the included software is free and open-source software made available both as compiled
binaries and in source code form, allowing modifications to the original software. Usually, Linux
distributions optionally include some proprietary software that may not be available in source
code form, such as binary blobs required for some device drivers.
Usually distributions are put on CD that contains the kernel and programming tools and utilities.
These distributions usually come with a setup program on CD to install a Linux system, they
have the same kernel but with different interfaces.
▪ Ubuntu
▪ RedHat
▪ Fedora
▪ Debian
▪ Slackware
▪ SUSE
Directory Structure
File system: The way the files of an operating system are organized on the disk.
▪ All the files are grouped together in the directory structure. The file-system is arranged in
a hierarchical structure, like an inverted tree. The top of the hierarchy is traditionally
called root (written as a slash /).
▪ Linux sorts directories descending from the root directory according to their importance
to the boot process.
▪ File systems from other hard drive partitions mount to directories beneath the root
directory, providing access to a single directory structure.
▪ The File system hierarchy standard (FHS) governs the unified file system for Linux by
defining a standard set of directories, sub-directories and files.
▪ Linux is a case sensitive operating system.
Figure 2: Directory Structure of Linux (Ubuntu)
Directory Description
The root directory, all directories are below the / (root directory)
/
of the system.
/srv Contains the files for services like FTP and Web servers.
LINUX COMMANDS
1. pwd Command
The pwd command prints out the path of the current working directory (folder) you’re in. The
command will return an absolute (full) path, which is basically a path of all the directories that
starts with a forward slash (/).
2. cd Command
To navigate through the Linux files and directories, use the cd command. It requires either the
full path or the name of the directory, depending on the current working directory that you’re in.
Another scenario is if you want to switch to a completely new directory, for example,
/home/username/Movies. In this case, you have to type cd followed by the directory’s absolute
path: cd /home/username/Movies.
3. ls Command
The ls command is used to view the contents of a directory. By default, this command will
display the contents of the current working directory.
If we want to see the content of other directories, type ls and then the directory’s path. For
example, enter ls /home/username/Documents to view the content of Documents.
4. mkdir Command
‘mkdir’ command creates a new directory in the current working directory.
Use mkdir command to make a new directory — if we type mkdir cs303 it will create a
directory called cs303.
● To generate a new directory inside another directory, use this Linux basic
command mkdir Music/Newfile
● use the p (parents) option to create a directory in between two existing directories. For
example, mkdir -p Music/2020/Newfile will create the new “2020” file.
5. rmdir Command
If we need to delete a directory, then we use the rmdir command. However, rmdir only allows
us to delete empty directories.
Sample output of the command is shown.
6. clear Command
‘clear’ command performs clear screen operation on the terminal.
Sample output of the command is shown.
▪ cat filename1 filename2>filename3 joins two files (1 and 2) and stores the output of
them in a new file (3)
▪ to convert a file to upper or lower case use, cat filename | tr a-z A-Z >output.txt
cat >info.txt
Press Ctrl + D
cat info.txt
3) Concatenate Contents of Multiple Files
We can also concatenate the contents of multiple files into a single file
cat info.txt second.txt >third.txt
8. cp Command
The cp command is used to copy files from the current directory to a different directory.
9. mv Command
The primary use of the mv command is to move files, although it can also be used to rename
files.
The arguments in mv are similar to the cp command. We need to type mv, the file’s name, and
the destination’s directory. For example: mv file.txt /home/username/Documents.
10. rm Command
The rm command is used to delete directories and the contents within them. If we only want to
delete the directory — as an alternative to rmdir — we use rm -r.
Note: Be very careful with this command and double-check which directory you are in. This will
delete everything and there is no undo.
11. sudo Command
Short for “SuperUser Do”, this command enables us to perform tasks that require administrative
or root permissions. However, it is not advisable to use this command for daily use because it
might be easy for an error to occur if we did something wrong.
▪ It displays the username of the current user when this command is invoked.
▪ Games
Syntax :
$man [OPTION]... [COMMAND NAME]...
Options and Examples
In this example, manual pages of the command ‘printf‘ are simply returned.
2. Section-num: Since a manual is divided into multiple sections so this option is used to display
only a specific section of a manual.
Syntax :
$ man [SECTION-NUM] [COMMAND NAME]
Example:
$ man 2 intro
Output:
In this example, the manual pages of command ‘intro‘ are returned which lies in the section 2.
3. -f option: One may not be able to remember the sections in which a command is present. So
this option gives the section in which the given command is present.
Syntax:
$ man -f [COMMAND NAME]
Example:
$ man -f ls
Output:
In this example, the command ‘ls‘ is returned with its section number.
4. -a option: This option helps us to display all the available intro manual pages in succession.
Syntax:
$ man -a [COMMAND NAME]
Example:
$ man -a intro
Output:
In this example you can move through the manual pages (sections) i.e. either reading (by
pressing Enter) or skipping (by pressing ctrl+D) or exiting (by pressing ctrl+C).
5. -k option: This option searches the given command as a regular expression in all the manuals
and it returns the manual pages with the section number in which it is found.
Syntax:
$ man -k [COMMAND NAME]
Example:
$ man -k cd
Output:
The command ‘cd‘ is searched in all the manual pages by considering it as a regular expression.
6. -w option: This option returns the location in which the manual page of a given command is
present.
Syntax:
$ man -w [COMMAND NAME]
Example:
$ man -w ls
Output:
The command ‘printf‘ is taken as case-sensitive i.e ‘printf‘ returns the manual pages but ‘Printf‘
gives error.
14. sort Command
Sort command is used to sort a file, arranging the records in a particular order. By default, the
sort command sorts file assuming the contents are ASCII. Using options in sort command, it can
also be used to sort numerically.
▪ sort is a standard command line program that prints the lines of its input or concatenation
of all files listed in its argument list in sorted order.
▪ The sort command is a command line utility for sorting lines of text files. It supports
sorting alphabetically, in reverse order, by number, by month and can also remove
duplicates.
▪ The sort command can also sort by items not at the beginning of the line, ignore case
sensitivity and return whether a file is sorted or not. Sorting is done based on one or more
sort keys extracted from each line of input.
▪ By default, the entire input is taken as a sort key. Blank space is the default field
separator.
1. Lines starting with a number will appear before lines starting with a letter.
2. Lines starting with a letter that appears earlier in the alphabet will appear before lines
starting with a letter that appears later in the alphabet.
3. Lines starting with a lowercase letter will appear before lines starting with the same letter
in uppercase.
Examples
Command:
$ sort file.txt
Output :
asad
chaudary
danyal
haris
naveen
raja asim
saad
LAB TASKS
Task 1
Practice all the above Linux commands on your Ubuntu environment.
Task 2
What is the username which is logged-in?
What is the current working directory on your terminal?
Task 3
Perform following tasks.
▪ List the content of your home directory
▪ List the content of any directory by using its absolute path in first then its relative path
▪ List the content of any directory with the ls command and the option –R
▪ List the content of any directory with the ls command and the option -al or -a -l
Task 4
Perform following tasks.
▪ Create a text file with the name of friends.txt, using cat command, having names of all
your friends.
▪ Create another text file with the name of teachers.txt, using cat command, having names
of all your teachers.
▪ Print the contents of both files, i.e. friends.txt and teachers.txt, in sorting form.
Task 5
Perform following tasks.