Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

LAB NO.

01
INTRODUCTION TO LINUX (UBUNTU) AND
COMMAND-LINE

Following are the lab objectives:

1. Understand the Linux Concept and Distributions


Lab 2. Understand the directory structure of Ubuntu

Objectives 3. Apply Linux Commands using Terminal

Roll No.Student Student Name


Obtained Marks
Marks Comments

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.

▪ Complete this lab work within lab timing.

▪ Discussion with peers is not allowed.

▪ You can consult any book, notes & Internet.

▪ Copy paste from the Internet will give you negative marks.

▪ Lab work is divided into small tasks, completing all tasks sequentially.

▪ Show the solution of each lab task to your Lab Instructor.

▪ In-Lab Exercises/Tasks

▪ Write your code at provided space after each question

▪ You need to upload code for all tasks at LMS.


INTRODUCTION TO LINUX (UBUNTU)
Linux
Linux is a generic term referring to Unix-like computer operating systems based on the Linux
kernel. Linux is actually just a kernel, so to create a complete Linux system you have to install
the source code of the kernel and many other freely distributed software programs.

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.

Some Linux based distributions are;

▪ Ubuntu

▪ RedHat

▪ Fedora

▪ Debian

▪ Slackware
▪ SUSE

Figure 1: Linux Distributions

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.

/bin Contains binary commands available to all users.

/boot Contains kernel and boot loader files.

/dev Contains device files.

/etc Contains system configuration files.

/home Contains by default the user home directories.

/lib Contains shared programs libraries and kernel modules.

/root Home directory for the root user.

/media Mount point for removable media.

/mnt Mount point for mounting a file system temporarily.


/opt Add-on application software packages.

/sbin Contains system binary commands.

/proc Contains information about system state and processes.

/srv Contains the files for services like FTP and Web servers.

/tmp Contains temporary files.

/usr Contains system commands and utilities.

/var Contains data files that are changed constantly.

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 (/).

Sample output of the command is shown.

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.

Let’s say you’re in /home/username/Documents and you want to go to Photos, a subdirectory


of Documents. To do so, simply type the following command: cd Photos.

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.

There are some shortcuts to help you navigate quickly:


▪ cd .. (with two dots) to move one directory up

▪ cd to go straight to the home folder

▪ cd- (with a hyphen) to move to your previous directory

Sample output of the command is shown.

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.

There are variations we can use with the ls command:

● ls -R will list all the files in the sub-directories as well


● ls -a will show the hidden files
● ls -al will list the files and directories with detailed information like the permissions, size,
owner, etc.

Sample output of the command is shown.

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.

There are extra mkdir commands as well:

● 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.

Sample output of the command is shown.

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.

After running the clear command, screen on terminal is clear now.


7. cat Command
cat (short for concatenate) is one of the most frequently used commands in Linux. It is used to
list the contents of a file on the standard output (sdout). To run this command, type cat followed
by the file’s name and its extension. For instance: cat file.txt.

Here are other ways to use the cat command:

▪ cat > filename creates a new file

▪ 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

Following are demonstrations of cat command to perform different tasks.

1) Writing Contents in Text File

We can write some text in a fresh file using cat command.

cat >info.txt

Some text of your choice.

Press Ctrl + D

2) Display Contents of Text File

Display contents of an existing file.

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.

For instance, the command cp scenery.jpg /home/username/Pictures would create a copy


of scenery.jpg (from your current directory) into the Pictures 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.

To rename files, the Linux command is mv oldname.ext newname.ext

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.

12. whoami Command


whoami command is used both in Linux Operating System and as well as in Windows Operating
System.

▪ It is basically the concatenation of the strings “who”,”am”,”i” as whoami.

▪ It displays the username of the current user when this command is invoked.

13. man Command


man command in Linux is used to display the user manual of any command that we can run on
the terminal.

It provides a detailed view of the command which includes NAME, SYNOPSIS,


DESCRIPTION, OPTIONS, EXIT STATUS, RETURN VALUES, ERRORS, FILES,
VERSIONS, EXAMPLES, AUTHORS and SEE ALSO.

Every manual is divided into the following sections:

▪ Executable programs or shell commands

▪ System calls (functions provided by the kernel)

▪ Library calls (functions within program libraries

▪ Games

▪ Special files (usually found in /dev)

▪ File formats and conventions eg /etc/passwd

▪ Miscellaneous (including macro packages and conventions), e.g. groff(7)

▪ System administration commands (usually only for root)

▪ Kernel routines [Non standard]

Syntax :
$man [OPTION]... [COMMAND NAME]...
Options and Examples

1. No Option: It displays the whole manual of the command.


Syntax :

$ man [COMMAND NAME]


Example:
$ man printf
Output:

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 location of command ‘ls‘ is returned.

7. -I option: It considers the command as case sensitive.


Syntax:
$ man -I [COMMAND NAME]
Example:
$ man -I printf
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 command sorts the contents of a text file, line by line.

▪ 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.

The sort command follows these features as stated below:

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

Suppose you create a data file with name file.txt


Command:
$ cat > file.txt
asad
chaudary
saad
raja asim
naveen
danyal
haris
Sorting a file : Now use the sort command
Syntax :
$ sort filename.txt

Command:
$ sort file.txt
Output :
asad
chaudary
danyal
haris
naveen
raja asim
saad

15. zip and unzip Commands


The zip command is used to compress files into a zip archive, and the unzip command is used to
extract the zipped files from a zip archive.
Sample output of the command is shown.

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 directory CS303Lab1 using mkdir command

▪ Navigate into the directory CS303Lab1 using cd command

▪ 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.

▪ Duplicate the file friends.txt, having a different name, using cp command.

▪ Duplicate the file teachers.txt, having different names, using cp command.

▪ Now zip the all four files using zip command.

You might also like