OS Lab 01
OS Lab 01
OS Lab 01
Spring 2023
Linux Shell:...................................................................................................................................................................................... 4
Linux Command:............................................................................................................................................................................. 4
Linux Vs Windows:
Windows is widely used operating system while Linux has its own advantages. Following are the
advantages discussed in the table below.
LINUX WINDOWS
Linux is an open source operating system i.e. Windows OS is a commercial operating system
user can change source code as per requirement and its closed source i.e. its source code is
inaccessible.
Linux has Monolithic kernel i.e. Whole operating Windows has a hybrid kernel i.e. Combination of
system works in the kernel space. microkernel and monolithic kernel.
File names are case-sensitive in Linux. File names are not case-sensitive in Windows.
Booting can be done from any disk. Booting can only be done from the prime disk.
Linux is highly reliable and secure. It has a Windows is less reliable than Linux. Over the
deeprooted emphasis on process management, recent years, Windows reliability has been
system security, and uptime. improved a lot. However, it still has some system
instabilities and security weaknesses because of
its oversimplified design.
Linux Components:
Major components of Linux are:
• Kernel
• System user space
• Applications Kernel:
Kernel is the base component of the OS. Without it, the OS doesn’t work. The kernel manages the
system’s resources and communicates with the hardware. It’s responsible for memory, process, and
file management.
System user space is the administrative layer for system-level tasks like configuration and software
installation. This includes the shell or command line, processes that run in the background, and the
desktop environment.
A type of software that lets you perform a task. Applications include everything from desktop tools and
programming languages to multiuser business suites. Most Linux distributions offer a central database
to search for and download additional apps.
Linux Shell:
Shell is a program that receives commands from the user and gives it to the OS to process, and it
shows the output. Shell is command line interface (CLI), a console that interacts with the system via
texts and processes. It’s similar to the Command Prompt application in Windows.
Linux Command:
A Linux command is a program or utility that runs on shell. Linux commands are executed on Terminal
by pressing ‘Enter’ at the end of the line. User can run commands to perform various tasks, from
package installation to user management and file manipulation.
A command may contain an option or a parameter. In some cases, it can still run without them. These
are the three most common parts of a command:
NOTE: All Linux commands are case sensitive i.e. ‘cp’ is not equivalent to ‘CP’. Also, all the files and
directories in linux are case sensitive so for example ‘/etc/hosts’ is not equivalent to ‘/etc/Hosts’ and so
hosts and Hosts are two different files in the same directory. When executing multiple commands in a
single line, use ; to separate them.
1. man
2. clear
3. exit
4. history
5. echo
6. alias, unalias
7. |
8. &&
1. man command
The man command provides a user manual of any commands or utilities you can run in Terminal,
including the name, description, and options.
man man[command_name]
For example, you want to access the manual for the ls command:
man ls
Enter this command if you want to specify the displayed section: man
man man 2 ls
3. exit command
4. history command
With history, the system will list up to 500 previously executed commands, allowing you to reuse
them without re-entering. Keep in mind that only users with sudo privileges can execute this
command. How this utility runs also depends on which Linux shell you use. To run it, enter the
command below:
history [option]
5. echo command
The echo command is a built-in utility that displays a line of text or string using the standard output.
Here’s the basic syntax: echo
[option] [string]
For example, you can display the text Hostinger Tutorials by entering:
-E displays the default option and disables the interpretation of backslash escapes.
6. alias, unalias commands alias allows you to create a shortcut with the same functionality as a
When executed, it instructs the shell to replace one string with another.
alias Name=String
For example, you want to make e the alias for the exit command:
alias e=exit
unalias [alias_name]
7. |
If a user in Linux likes to combine two or more commands, pipes is the option. Pipe is
represented by the symbol ‘|’
8. &&
And command && is used to only allow the next command to run if the previous one is successful.
1. date
2. shutdown, poweroff , reboot
3. ps
4. kill
5. df
1. date command
The date command will display the system date.
3. ps command
The process status or ps command produces a snapshot of all running processes in your system. The
static results are taken from the virtual files in the /proc file system.
Executing the ps command without an option or argument will list the running processes in the shell
along with:
use:
4. kill command
To kill a program, you must know its process identification number (PID). If you don’t know the PID,
run the following command:
ps ux
After knowing what signal to use and the program’s PID, enter the following syntax:
There are 64 signals that you can use, but these two are among the most commonly used:
SIGTERM requests a program to stop running and gives it some time to save all of its progress.
The system will use this by default if you don’t specify the signal when entering the kill
command.
SIGKILL forces programs to stop, and you will lose unsaved progress.
For example, the program’s PID is 63773, and you want to force it to stop:
df command
Use the df command to report the system’s disk space usage, shown in percentage and kilobyte (KB).
Here’s the general syntax:
df [options] [file]
For example, enter the following command if you want to see the current directory’s system disk space
usage in a human-readable format:
df -h
If you want to check how much space a file or a directory takes up, use the du command. You can run
this command to identify which part of the system uses the storage excessively.
Remember, you must specify the directory path when using the du command. For example, to check
/home/user/Documents enter: du /home/user/Documents
Adding a flag to the du command will modify the operation, such as:
-h informs the last modification date of the displayed folders and files.
7. top command
The top command in Linux Terminal will display all the running processes and a dynamic real-time view
of the current system. It sums up the resource utilization, from CPU to memory usage.
The top command can also help you identify and terminate a process that may use too many system
resources.
8. htop command
The htop command is an interactive program that monitors system resources and server processes in
real time. It is available on most Linux distributions, and you can install it using the default package
manager.
Compared to the top command, htop has many improvements and additional features, such as mouse
operation and visual indicators. To use it, run the following command:
htop [options]
The uname or unix name command will print detailed information about your Linux system and
hardware. This includes the machine name, operating system, and kernel. To run this command,
simply enter uname into your CLI.
uname [option]
sudo command
Short for superuser do, sudo is one of the most popular basic Linux commands that lets you perform
tasks that require administrative or root permissions.
When using sudo, the system will prompt users to authenticate themselves with a password. Then, the
Linux system will log a timestamp as a tracker. By default, every root user can run sudo commands for
15 minutes/session.
If you try to run sudo in the command line without authenticating yourself, the system will log the
activity as a security event.
sudo (command)
1. useradd
2. addgroup
3. adduser
4. usermod
5. deluser
6. passwd
su username -
ping
2. wget
3. hostname
4. ip
1. ping command
The ping command is one of the most used basic Linux commands for checking whether a network or
a server is reachable. In addition, it is used to troubleshoot various connectivity issues.
For example, you want to know whether you can connect to Google and measure its response time:
ping google.com
2. wget command
The Linux command line lets you download files from the internet using the wget command. It works in
the background without hindering other running processes.
The wget command retrieves files using HTTP, HTTPS, and FTP protocols. It can perform recursive
downloads, which transfer website parts by following directory structures and links, creating local
versions of the web pages.
For example, enter the following command to download the latest version of WordPress:
wget https://wordpress.org/latest.zip
hostname [option]
For example, enter the following command to know your computer’s IP address:
hostname –i
4. ip command
The ip command is a Linux net-tool for system and network administrators. IP stands for Internet
Protocol and as the name suggests, the tool is used for configuring network interfaces.
Older Linux distributions used the ifconfig command, which operates similarly. However, ifconfig has a
limited range of capabilities compared to the ip command.
OBJECTS (or subcommands) that you will use most often include:
2. address (addr/a) – used to display and modify protocol addresses (IP, IPv6).
4. neigh (n) – used to display and manipulate neighbor objects (ARP table).
Use the pwd command to find the path of your current working directory. Simply entering pwd will
return the full current path – a path of all the directories that starts with a forward slash (/). For
example, /home/username.
[option]
cd command
To navigate through the Linux files and directories, use the cd command. Depending on your current
working directory, it requires either the full path or the directory name.
Running this command without an option will take you to the home folder. Keep in mind that only users
with sudo privileges can execute it.
cd Photos
If you want to switch to a completely new directory, for example, /home/username/Movies, you have to
enter cd followed by the directory’s absolute path: cd /home/username/Movies
previous directory.
ls command
The ls command lists files and directories within a system. Running it without a flag or parameter will
show the current working directory’s content
To see other directories’ content, type ls followed by the desired path. For example, to view files in the
Documents folder, enter:
ls /home/username/Documents
ls -lh shows the file sizes in easily readable formats, such as MB, GB, and TB.
mkdir command
Use the mkdir command to create one or multiple directories at once and set permissions for each of
them. The user executing this command must have the privilege to make a new folder in the parent
directory, or they may receive a permission denied error.
mkdir Music
To make a new directory called Songs inside Music, use this command:
mkdir Music/Songs
-p or –parents create a directory between two existing folders. For example, mkdir -p
Music/2020/Songs will make the new “2020” directory.
-m sets the file permissions. For instance, to create a directory with full read, write, and execute
permissions for all users, enter mkdir -m777 directory_name.
rmdir command
To permanently delete an empty directory, use the rmdir command. Remember that the user running
this command should have sudo privileges in the parent directory.
For example, you want to remove an empty subdirectory named personal1 and its main folder mydir:
rmdir -p mydir/personal1
cp command
To copy one file from the current directory to another, enter cp followed by the file name and the
destination directory. For example:
cp filename.txt /home/username/Documents
To copy files to a directory, enter the file names followed by the destination directory:
To copy the content of a file to a new file in the same directory, enter cp followed by the source file and
the destination file:
cp filename1.txt filename2.txt
To copy an entire directory, pass the -R flag before typing the source directory, followed by the
destination directory:
cp -R /home/username/Documents /home/username/Documents_backup
mv command
The primary use of the mv command is to move and rename files and directories. Additionally, it
doesn’t produce an output upon execution.
Simply type mv followed by the filename and the destination directory. For example, you want to move
filename.txt to the /home/username/Documents directory: mv filename.txt
/home/username/Documents. You can also use the mv command to rename a file:
mv old_filename.txt new_filename.txt
rm command
The rm command is used to delete files within a directory. Make sure that the user performing this
command has write permissions.
Remember the directory’s location as this will remove the file(s) and you can’t undo it.
rm filename
touch command
The touch command allows you to create an empty file or generate and modify a timestamp in the
Linux command line.
For example, enter the following command to create an HTML file named Web in the Documents
directory:
touch /home/username/Documents/Web.html
cat command
Concatenate, or cat, is one of the most frequently used Linux commands. It lists, combines, and writes
file content to the standard output. To run the cat command, type cat followed by the file name and its
extension. For instance: cat filename.txt.
cat filename1.txt filename2.txt > filename3.txt merges filename1.txt and filename2.txt and stores the
output in filename3.txt.
Linux allows users to edit and manage files via a text editor, such as nano, vi, or jed. nano and vi come
with the operating system, while jed has to be installed.
The nano command denotes keywords and can work with most languages. To use it, enter the
following command: nano [filename] vi uses two operating modes to work – insert and command.
insert is used to edit and create a text file. On the other hand, the command performs operations, such
as saving, opening, copying, and pasting a file.
jed has a drop-down menu interface that allows users to perform actions without entering keyboard
combinations or commands. Like vi, it has modes to load modules or plugins to write specific texts.
• Search commands:
locate command
Moreover, adding the -i argument will turn off case sensitivity, so you can search for a file even if you
don’t remember its exact name.
To look for content that contains two or more words, use an asterisk (*). For example:
locate -i school*not
The command will search for files that contain the words school and note, whether they use uppercase
or lowercase letters.
find command
Use the find command to search for files within a specific directory and perform subsequent operations.
Here’s the general syntax:
For example, you want to look for a file called notes.txt within the home directory and its subfolders:
grep command
Another basic Linux command on the list is grep or global regular expression print. It lets you find a
word by searching through all the texts in a specific file.
For example, you want to search for the word blue in the notepad.txt file:
head command
The head command allows you to view the first ten lines of a text. Adding an option lets you change the
number of lines shown. The head command is also used to output piped data to the CLI.
For instance, you want to view the first ten lines of note.txt, located in the current directory:
head note.txt
-n or –lines prints the first customized number of lines. For example, enter head -n 5 filename.txt to
show the first five lines of filename.txt.
tail command
The tail command displays the last ten lines of a file. It allows users to check whether a file has new
data or to read error messages.
For example, you want to show the last ten lines of the colors.txt file:
tail -n colors.txt
Concatenate, or cat, is one of the most frequently used Linux commands. It lists, combines, and writes
file content to the standard output. To run the cat command, type cat followed by the file name and its
extension. For instance: cat filename.txt.
cat filename1.txt filename2.txt > filename3.txt merges filename1.txt and filename2.txt and stores the
output in filename3.txt.
chmod command
chmod is a common command that modifies a file or directory’s read, write, and execute permissions.
In Linux, each file is associated with three user classes – owner, group member, and others.
For example, the owner is currently the only one with full permissions to change note.txt. To allow
group members and others to read, write, and execute the file, change it to the -rwxrwxrwx permission
type, whose numeric value is 777:
chown command
The chown command lets you change the ownership of a file, directory, or symbolic link to a specified username.
5. Wildcards:
2) Create the following directories with one command. dirOSLAB -> subDir ->
subsubdir -> OSLAB2
3) Write 2 C program one prints “I love Operating System” and other prints “I love
Linux”. Compile and Run both programs and print the output to two different
files. After then combine both the files in one new file using a single command.