0% found this document useful (0 votes)
18 views

Os Practicals

Uploaded by

tanyatomar766
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Os Practicals

Uploaded by

tanyatomar766
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

PRACTICALS

Ques-1 Connect to the Linux Server and understand the basic Directory
Structure of Linux.
Code- Syntax- ssh username@server_ip

Basic Directory Structure of Linux:-


1. /: The root directory, where everything starts
Syntax:- /

2. ls (List Directory Contents)


Syntax:- ls /

3. pwd (Print Working Directory)


Syntax:- pwd
4. /bin (Binary)
Syntax:- ls /bin

5. /home(User home directories)


Syntax:- ls /home

6. /lib (libraries)
Syntax:- ls /lib

7. /sbin (System Binary)


Syntax:- ls /sbin
Ques-2 To understand help commands like:- man, info, help, whatis, apropos
Code- Commands:-
1. man (Manual Pages)
The man command displays the manual pages (documentation) for Linux
commands.
Syntax:- man <command>

Example- man ls

2. info (GNU Info Pages)


The info command displays more detailed and structured documentation about
commands, often in a more user-friendly format than ‘man’.
Syntax- info <command>

Example- info ls
3. help (Bash Built-in Help)
The ‘help’ command provides brief help for built-in shell commands in Bash.
Syntax:- help <command>

Example- help cd

4. whatis (Brief Description of Commands)


The whatis command gives a one-line description of a command or program.
Syntax:- whatis <command>

Example- whatis ls

5. apropos (Search for Commands by Keyword)


The apropos command searches the whatis database for keywords related to
commands.
Syntax:- apropos <keyword>

Example- apropos directory


Ques-3 To understand basic directory navigation commands like cat, cd, mv, cp,
rm, mkdir, rmdir, file, pwd command.
Code- Directory Navigation Commands:-
1. cat (Concatenate and Display Files)
The cat command is used to view the contents of a file or concatenate multiple
files.
Syntax:- cat <filename>

2. cd (Change Directory)
The cd command changes the current working directory.
Syntax:- cd <directory>

3. mv (Move or Rename Files)


The mv command is used to move files from one location to another or rename
files.
Syntax:- mv <source> <destination>

Syntax:- mv oldname.txt newname.txt

4. cp (Copy Files and Directories)


The cp command copies files or directories.
Syntax:- cp <source> <destination>
5. rm (Remove Files or Directories)
The rm command deletes files or directories.
Syntax:- rm <filename>

6. mkdir (Make Directory)


The mkdir command creates a new directory.
Syntax:- mkdir <directory_name>

7. rmdir (Remove Empty Directory)


The rmdir command removes empty directories.
Syntax:- rmdir <directory_name>

8. file (Determine File Type)


The file command displays the type of a file.
Syntax:- file <filename>

9. pwd (Print Working Directory)


The pwd command prints the full path of the current working directory.
Syntax:- pwd
Ques-4 To understand basic commands like:- date, cal, echo, bc, ls, who,
whoami, hostname, uname, tty, aliase.
Code- Commands:-
1. date (Display or Set Date and Time)
The date command displays the current date and time.
Syntax:- date

2. cal (Calendar)
The cal command displays a calendar of the current month or a specific
year/month.
Syntax:- cal

3. echo (Print Text or Variables to the Screen)


The echo command prints text or variables to the terminal.
Syntax:- echo <message>

4. bc (Basic Calculator)
The bc command is an arbitrary-precision calculator.
Syntax:- bc
5. ls (List Directory Contents)
The ls command lists files and directories.
Syntax:- ls

6. who (Display Who is Logged In)


The who command shows who is logged into the system.
Syntax:- w

7. hostname (Show or Set the System's Hostname)


The hostname command displays or sets the system’s hostname.
Syntax:- hostname

8. whoami (Display Current User)


The whoami command shows the name of the currently logged-in user.
Syntax:- whoami

9. uname (Display System Information)


The uname command displays information about the system, such as the kernel
version.
Syntax:- uname
10. tty (Print Terminal Name)
The tty command shows the terminal you're using.
Syntax:- tty

11. alias (Create Aliases for Commands)


The alias command is used to create shortcuts for long or complex commands.
Syntax:- alias <shortcut_name>="command"
Ques-5 To understand vi basics, Three modes of vi Editor, how to write, save,
execute a shell script in vi editor.
Code- Three mode of vi Editor:-

1. Normal Mode (Command Mode):


This is the default mode when you open a file in vi. In this mode, you can navigate
through the file and run commands like copy, paste, delete, or move text.

2. Insert Mode:
Insert mode allows you to type and edit text.

3. Command-Line Mode:
This mode is used to save, quit, or execute commands.

How to write, save, execute a shell script in vi editor:-


1. To create or edit a shell script named
Syntax:- vi <script_name>.sh

2. Enter Insert Mode:


Press i to enter Insert Mode. You’ll be able to start typing your script.

3. Write Your Shell Script:


Begin typing your script. For example, a simple shell script

4. Save the File:


Press Esc to switch back to Normal Mode.
Type :w and press Enter to save the file.

5. Exit the vi Editor:


In Normal Mode, type :q and press Enter to quit.
Alternatively, type :wq to save and quit in one step.

6. Make the Script Executable:


Syntax:- chmod
Ques-6 To understand process related commands like: - ps, top, pstree, nice,
renice in Linux
Code- Commands:-
1. ps Command (Process Status)

The ps command displays information about the currently running processes on


your system. It shows details such as the process ID (PID), terminal associated
with the process, CPU usage, memory usage, etc.

Syntax:- ps [options] (ps, ps -e, ps -ef, ps -e –forest)

2. top Command (Real-time Process Monitoring)

The top command provides a dynamic, real-time view of running processes and
system resource usage. It displays CPU and memory usage, the number of
processes, and the load on the system.

Syntax:- top

3. pstree Command (Process Tree)

The pstree command shows running processes in a tree-like structure, making it


easier to see parent and child relationships between processes.

Syntax:- pstree [options]


4. nice Command (Set Process Priority)

The nice command is used to start a process with a modified priority level. Lower
priority processes receive fewer CPU resources compared to higher priority
processes.

Syntax:- nice -n <priority> <command>

5. renice Command (Change the Priority of Running Processes)

The renice command allows you to change the priority of an already running
process.

Syntax:- renice <priority> -p <PID>


Ques-7 To understand how to examine and change File permissions.
Code- Commands:-
1. Examine File Permissions:-
Can examine file permissions using

Syntax:- ls -l <filename>

2. Change File Permissions


a. Symbolic Mode
In symbolic mode, you specify the user (owner, group, others) and the
permission (read, write, execute) you want to add, remove, or set.

Syntax:- chmod [who][operation][permission] <filename>

b. Numeric Mode
In numeric mode, file permissions are represented by numbers.

Syntax- chmod <octal_permissions> <filename>

3. Changing Ownership of a File


The chown command is used to change the owner and/or group of a file.

Syntax:- chown <owner>:<group> <filename>


Ques-8 Set a file to be read-only with the chmod command. Interpret the file
permissions displayed by the ls -l command.
Code- Commands:-
1. Navigate to the directory where your file is located.

Syntax:- cd /path/to/directory

2. Set the file to read-only for the owner, group, and others using the chmod
command:
Syntax:- chmod 444 example.txt

3. To check the file permissions, use the ls -l command:

Syntax:- ls -l example.txt
Ques-9 Delete one or more directories with the rmdir command. See what
happens if the directory is not empty. Experiment (carefully!) with the rm -r
command to delete a directory and its content.
Code- Commands:-
1. Deleting a empty directory
Syntax:- rmdir <empty_directory_name>

2. Deleting a non-empty directory and its content

Can only delete empty directories. If the directory contains any files or sub
directories, it will not delete the directory and will display an error.
Syntax:- rmdir <non-empty_directory_name>

3. Deleting a non-empty directory and its content with ‘rm -r’

Deletes the directory along with all files and subdirectories inside it, regardless of
whether the directory is empty or not.

Syntax:- rm -r <directory_name>
Ques-10 Change your directory to the directory exercises. Create a file in that
directory, named the file as example1 using the cat command containing the
following text: water, water everywhere and all the boards did shrink; water,
water everywhere, no drop to drink.
Code- Commands:-
1. Change Directory to exercises
Syntax:- cd <directory_name>

2. Create a File Named example1 Using ‘cat’


Syntax:- cat > <file_name>

3. Type the following text:-

To save the file, press CTRL + D to indicate the end of input.

4. Verify the File Content:-


Ques-11 Write basic shell script to display the table of a number.
Code- Commands:-
1. Open a terminal and create a new shell script file

2. Enter Insert mode by pressing i, then type the following script:

3. Save and exit the script:


Press Esc to leave insert mode.
Type :wq and press Enter to save and quit.

4. Give it execute permissions using the chmod command:

5. Now, run the script using the following command:


Ques-12 Write basic shell script to input a character from user and then check
whether it is uppercase, lowercase or digit.
Code- Commands:-
1. Open a terminal and create a new shell script file

2. Enter Insert mode by pressing i, then type the following script:

3. Press Esc to leave insert mode and type :wq and press Enter to save and quit.
4. Give it execute permissions using the chmod and run the script using the following
command:
Ques-13 Write basic shell script to calculate factorial of a number.
Code- Commands:-
1. Open a terminal and create a new shell script file

2. Enter Insert mode by pressing i, then type the following script:

3. Press Esc to leave insert mode and type :wq and press Enter to save and quit.
4. Give it execute permissions using the chmod and run the script using the following
command:
Ques-14 Write basic shell script to input the month number and generate
corresponding calendar
Code- Commands:-
1. Open a terminal and create a new shell script file

2. Enter Insert mode by pressing i, then type the following script:

3. Press Esc to leave insert mode and type :wq and press Enter to save and quit.
4. Give it execute permissions using the chmod and run the script using the following
command:
Ques-15 Write basic shell script to list all directories.
Code- Commands:-
1. Open a terminal and create a new shell script file

2. Enter Insert mode by pressing i, then type the following script:

3. Press Esc to leave insert mode and type :wq and press Enter to save and quit.

4. Give it execute permissions using the chmod and run the script using the following
command:
Ques-16 Write basic shell script to display greatest of three numbers.
Code- Commands:-
1. Open a terminal and create a new shell script file

2. Enter Insert mode by pressing i, then type the following script:

3. Press Esc to leave insert mode and type :wq and press Enter to save and quit.

4. Give it execute permissions using the chmod and run the script using the following
command:
Ques-17 Write basic shell script to check whether the number entered by user is
prime or not.
Code- Commands:-
1. Open a terminal and create a new shell script file

2. Enter Insert mode by pressing i, then type the following script:

3. Press Esc to leave insert mode and type :wq and press Enter to save and quit.

4. Give it execute permissions using the chmod and run the script using the following
command:

You might also like