Unix Operating System

πŸ“š Table of Contents

  1. Unix Basics for Beginners
  2. Unix Users
  3. Using Unix Commands
  4. Working with Files
  5. Unix Directories
  6. Wildcards and File Handling
  7. Unix File Permission Setup
  8. Unix Environment
  9. The vi Editor
  10. Unix Pipes and Filters
  11. Question & Answer

1: Unix Getting Started

🌟 What is Unix?

  • Unix is an operating system, first developed in 1969.
  • Supports multi-user access β†’ many users can work at the same time.
  • Supports multitasking β†’ multiple programs can run simultaneously.

πŸ—οΈ Unix Structure

  1. Kernel β†’ Core of the OS; controls hardware & system resources.
  2. Shell β†’ Command-line interface; users type commands here.
  3. Commands β†’ Instructions given to the system (e.g., ls, pwd).
  4. Files & Directories β†’ Data stored in files, organized in a directory structure.

⚑ Starting Up Unix

  • When powered on, Unix loads the kernel.
  • User must log in with a username & password.

πŸ’» What is a Shell?

  • The shell is the user’s interface to Unix.
  • You type commands β†’ the shell interprets them β†’ the system executes them.
  • Different types of shells exist:
    • Bourne Shell (sh)
    • C Shell (csh)
    • Korn Shell (ksh)
    • Bash (Bourne Again Shell) – most common today.

Shell Prompt

  • $ prompt β†’ usually indicates you are logged in as a regular (non-root) user.
  • # prompt β†’ usually indicates you are logged in as the superuser (root).

πŸ‘‰ Example:

$ whoami
raj         # Regular user prompt shows $

# whoami
root        # Superuser prompt shows #

Shell Scripts

  • Shell scripts are groups of commands that run together. They can include comments and loops to make tasks easier.

Shells Types

  1. Bourne Shell (sh): The original shell developed for UNIX. It provides a command-line interface and scripting capabilities.
  2. C Shell (csh): Introduced features like command history and job control. It uses a syntax similar to the C programming language.
  3. Korn Shell (ksh): Combines features of the Bourne and C shells, offering advanced scripting capabilities and improved performance.
  4. Bash (Bourne Again SHell): An enhanced version of the Bourne Shell, it includes features from the C Shell and Korn Shell, and is widely used in Linux systems.
  5. Z Shell (zsh): Incorporates features from other shells and offers advanced customisation options, including themes and plugins.

MCQs

1. What is the primary function of the UNIX operating system?
A. To manage hardware resources
B. To provide a graphical user interface
C. To act as a link between the computer and the user
D. To run only one program at a time

Answer: C

2. Which of the following is a component of the UNIX architecture?
A. User Interface
B. Kernel
C. Application Software
D. All of the above

Answer: B

3. What is the role of the shell in UNIX?
A. To manage hardware
B. To interpret user commands
C. To store files
D. To provide a graphical interface

Answer: B

4. Which command is used to display the current date and time in UNIX?
A. time
B. date
C. now
D. current

Answer: B

5. Which of the following shells is the first shell to appear on UNIX systems?
A. C Shell
B. Korn Shell
C. Bourne Shell
D. Bash Shell

Answer: C

6 What is the purpose of a shell script?
A. To create graphical applications
B. To execute a list of commands in order
C. To manage system resources
D. To provide a user interface

Answer: B

7. Which of the following is NOT a type of shell in UNIX?
A. Bourne Shell
B. C Shell
C. Python Shell
D. Korn Shell

Answer: C

8. What is the core of the UNIX operating system called?
A. Shell
B. Kernel
C. Command Line
D. User Interface

Answer: B


2: Unix Users

πŸ”Ή Logging In & Out

  • Each user has a unique username & password.
  • Login β†’ enter credentials.
  • Logout β†’ use the exit command.

πŸ”Ή The Superuser (root)

  • root user = superuser with full system privileges.
  • Can perform tasks like adding/removing users, installing software, system maintenance.
  • Prompt symbol:
    • $ β†’ normal user
    • # β†’ superuser

πŸ”Ή Home Directory

  • Each user has a home directory (usually /home/username).
  • It’s the default working directory after login.

πŸ”Ή Changing Password

  • Use the passwd command to change your password for better security.

πŸ”Ή Shell Startup Files

  1. System Startup File β†’ /etc/profile
    • Sets global environment variables for all users.
  2. User Startup File β†’ .profile (in user’s home directory)
    • Custom settings & user-specific configurations.

πŸ”Ή Environment Variables

Common variables set during login:

  • PATH β†’ directories to search for commands
  • HOME β†’ user’s home directory
  • TERM β†’ terminal type


MCQ

1. What command is used to log out of a UNIX session?
A. exit
B. logout
C. quit
D. close

Answer: A

2. Which user has full administrative privileges in a UNIX system?
A. Regular user
B. Guest user
C. Superuser
D. System user

Answer: C

3. What symbol indicates a superuser prompt in UNIX?
A. $
B. #
C. %
D. &

Answer: B

4. Where is a user’s home directory typically located?
A. /usr
B. /etc
C. /home
D. /var

Answer: C

5. What is the purpose of the /etc/profile file?
A. To store user-specific configurations
B. To manage user passwords
C. To set global environment variables
D. To log user activities

Answer: C

6. Which file allows users to customise their environment settings?
A. /etc/profile
B. .bashrc
C. .profile
D. .login

Answer: C

7. What does the PATH environment variable specify?
A. The current working directory
B. The location of user files
C. The directories to search for executable commands
D. The user’s home directory

Answer: C

8. What is the significance of the HOME environment variable?
A. It indicates the current shell type.
B. It specifies the user’s home directory.
C. It sets the command prompt style.
D. It defines the system’s root directory.

Answer: B

9. Which of the following is true about the superuser in UNIX?
A. The superuser can only read files.
B. The superuser has limited access to system files.
C. The superuser can perform any administrative task.
D. The superuser cannot change system settings.

Answer: C


3: Using UNIX Commands

πŸ”Ή Running a Command

  • Type the command name and press Enter.
  • Example: date # shows current date & time

πŸ”Ή Types of Commands

  1. Intrinsic (Built-in) – part of the shell itself
    • Example: cd β†’ change directory
  2. Extrinsic (External) – stored as separate files in the system
    • Example: ls β†’ list files

πŸ”Ή Commonly Used Commands

  • pwd β†’ show current directory
  • whoami β†’ show your username
  • who β†’ list users currently logged in
  • uname β†’ show system information

πŸ”Ή Getting Help

  • Use manual pages with man man ls # detailed help for 'ls'

πŸ”Ή Running Multiple Commands

  • Separate commands with a semicolon (;) cal; date

πŸ”Ή Options & Arguments

  • Options β†’ start with -, modify behavior
  • Arguments β†’ extra input for the command
  • Example: ls -l # list files in long (detailed) format


MCQ

1.Which command is used to display the current working directory in UNIX?
A. ls
B. pwd
C. cd
D. dir

Answer: B

2. What does the whoami command do?
A. Displays the current date and time
B. Lists all users currently logged in
C. Shows the name of the current user
D. Changes the current user

Answer: C

3. Which command would you use to get help on the ls command?
A. help ls
B. ls –help
C. man ls
D. info ls

Answer: C

4. What is the purpose of the man command in UNIX?
A. To execute a command
B. To display manual pages for commands
C. To list files in a directory
D. To change the current directory

Answer: B

5. How can you execute multiple commands in one line?
A. By using a comma (,)
B. By using a semicolon (;)
C. By using a colon (:)
D. By using a space

Answer: B

6. Which command is used to list all files in the current directory?
A. list
B. ls
C. dir
D. show

Answer: B

7. What does the -l option do when used with the ls command?
A. Lists files in reverse order
B. Lists files with detailed information
C. Lists only hidden files
D. Lists files in a long format

Answer: B

8. Which command would you use to find out how many lines are in a file?
A. count
B. wc -l
C. ls -l
D. grep -c

Answer: B

9. What is the function of the cd command?
A. To display the contents of a directory
B. To change the current directory
C. To create a new directory
D. To delete a directory

Answer: B

10. Which command shows the system information, including the kernel version?
A. uname
B. version
C. sysinfo
D. info

Answer: A


4: Working with Files

πŸ”Ή File Operations

  • Create a file β†’ cat > filename
  • List files β†’ ls
  • Delete a file β†’ rm filename

πŸ”Ή Viewing File Contents

  • Display entire file β†’ cat filename
  • View page by page β†’ more filename

πŸ”Ή File Naming Rules

  • Maximum length: 256 characters
  • Can include letters, numbers, and some special characters
  • Case-sensitive β†’ File.txt and file.txt are different

πŸ”Ή Hidden Files

  • Files starting with a dot (.) are hidden
    • Example: .hiddenfile
  • Show hidden files β†’ ls -a

πŸ”Ή File Permissions

  • Check permissions β†’ ls -l
  • Permissions decide who can read (r), write (w), or execute (x) a file.
    • Example: -rw-r--r-- file.txt
      • Owner β†’ read & write
      • Group β†’ read
      • Others β†’ read

    MCQ

    1. Which command is used to create a new file in UNIX?
    A. touch
    B. cat > filename
    C. mkdir
    D. newfile

    Answer: B

    2. What does the command ls -l display?
    A. Only the file names
    B. Detailed information about files including permissions
    C. Only hidden files
    D. The current directory path

    Answer: B

    3. Which command is used to delete a file in UNIX?
    A. remove filename
    B. delete filename
    C. rm filename
    D. del filename

    Answer: C

    4. How can you view the contents of a file named example.txt?
    A. view example.txt
    B. cat example.txt
    C. show example.txt
    D. open example.txt

    Answer: B

    5. What is the maximum length of a file name in UNIX?
    A. 128 characters
    B. 256 characters
    C. 512 characters
    D. 64 characters

    Answer: B

    6. Which command will display hidden files in the current directory?
    A. ls
    B. ls -a
    C. ls -l
    D. ls -h

    Answer: B

    7. What does the rm command do?
    A. Renames a file
    B. Moves a file
    C. Deletes a file
    D. Copies a file

    Answer: C

    8. Which of the following is true about file permissions in UNIX?
    A. Permissions are not important.
    B. Permissions determine who can read, write, or execute a file.
    C. All files have the same permissions by default.
    D. Only the root user can change file permissions.

    Answer: B

    9. What does the command cat > newfile do?
    A. Displays the contents of newfile
    B. Creates a new file named newfile and allows you to enter text
    C. Deletes the file newfile
    D. Copies the contents of newfile

    Answer: B

    10. Which command is used to display the contents of a file one page at a time?
    A. cat
    B. more
    C. less
    D. view

    Answer: B


    5: Unix Directories

    πŸ”Ή What is a Directory?

    • A directory is a special file that stores file names and related info.
    • Unix uses a hierarchical structure (directory tree).

    πŸ”Ή Home Directory

    • The default directory after login.
    • Command to go home: cd ~

    πŸ”Ή Pathnames

    • Absolute Path β†’ starts from root /
      • Example: /etc/passwd
    • Relative Path β†’ starts from current directory
      • Example: docs/notes

    πŸ”Ή Directory Operations

    • List contents β†’ ls dirname
    • Create β†’ mkdir dirname
    • Create with parents β†’ mkdir -p /path/to/dir
    • Remove empty β†’ rmdir dirname
    • Remove non-empty (dangerous) β†’ rm -rf dirname
    • Rename/move β†’ mv olddir newdir

    πŸ”Ή Changing Directories

    • Move into a directory β†’ cd dirname
    • Go up one level β†’ cd ..

    πŸ”Ή Special Directories

    • . β†’ current directory
    • .. β†’ parent directory

    MCQ

    1. What is the command to change to your home directory in UNIX?
    A. cd ~
    B. cd /home
    C. cd ..
    D. cd /

    Answer: A

    2. Which command is used to create a new directory?
    A. mkdir dirname
    B. newdir dirname
    C. create dirname
    D. dircreate dirname

    Answer: A

    3. What does the command rmdir dirname do?
    A. Removes a directory and its contents
    B. Removes an empty directory
    C. Renames a directory
    D. Lists the contents of a directory

    Answer: B

    4. Which of the following represents the parent directory?
    A. .
    B. ..
    C. /
    D. ~

    Answer: B

    5. How can you view the contents of a directory?
    A. ls
    B. dir
    C. list
    D. show

    Answer: A

    6. What is the command to move up one level in the directory structure?
    A. cd ..
    B. cd /
    C. cd ~
    D. cd .

    Answer: A

    7. Which command is used to rename a directory?
    A. rename olddir newdir
    B. mv olddir newdir
    C. change olddir newdir
    D. edit olddir newdir

    Answer: B

    8. What does the command mkdir -p /tmp/dir1/dir2 do?
    A. Creates only dir1
    B. Creates dir1 and dir2, including any necessary parent directories
    C. Removes dir1 and dir2
    D. Lists the contents of dir1 and dir2

    Answer: B

    9. Which command will display the current working directory?
    A. pwd
    B. ls
    C. dir
    D. cd

    Answer: A

    10. What does the ls -d command do?
    A. Lists all files in the directory
    B. Displays the attributes of the directory itself
    C. Deletes the directory
    D. Changes to the specified directory

    Answer: B



    6: Wildcards and File Handling

    πŸ”Ή Wildcards (Pattern Matching)

    • * (Asterisk) β†’ matches 0 or more characters
      • Example: myfile* β†’ matches myfile, myfile1, myfile2
    • ? (Question Mark) β†’ matches exactly 1 character
      • Example: myfile0? β†’ matches myfile00, myfile01 (but not myfile010)
    • [ ] (Square Brackets) β†’ matches a range/set of characters
      • Example: [a-c] β†’ matches a, b, or c

    πŸ”Ή Searching File Contents (grep)

    • Basic Search β†’ grep "text" filename
    • Options:
      • -i β†’ ignore case
      • -c β†’ count matches
      • -v β†’ show lines that do not match
    • Search multiple files: grep "text" file1 file2

    πŸ”Ή Finding Files (find)

    • Locate files in the directory tree: find /etc -name "my*" β†’ finds all files starting with my inside /etc

    πŸ”Ή Miscellaneous File Handling

    πŸ“„ Head & Tail

    • head filename β†’ first 10 lines
    • tail filename β†’ last 10 lines

    πŸ”’ Word Count (wc)

    • wc filename β†’ counts lines, words, characters
    • Options:
      • -l β†’ lines
      • -w β†’ words
      • -c β†’ characters

    πŸ”— Linking Files

    • Hard link: ln filename linkname
    • Soft (symbolic) link: ln -s filename linkname

    MCQ

    1. What does the asterisk (*) wildcard represent in UNIX?
    A. Matches exactly one character
    B. Matches zero or more characters
    C. Matches a specific character
    D. Matches a range of characters

    Answer: B

    2. Which command is used to search for a specific text string in a file?
    A. find
    B. grep
    C. search
    D. locate

    Answer: B

    3. What does the question mark (?) wildcard match?
    A. Zero or more characters
    B. Any single character
    C. A specific character
    D. A range of characters

    Answer: B

    4. Which command would you use to find all files starting with “my” in the /etc directory?
    A. grep my /etc/*
    B. find /etc -name “my*”
    C. ls /etc/my*
    D. search /etc my*

    Answer: B

    5. What does the -i option do when used with the grep command?
    A. Counts the number of matches
    B. Displays lines that do not match
    C. Makes the search case-insensitive
    D. Searches for multiple files

    Answer: C

    6. Which command displays the last 10 lines of a file?
    A. head filename
    B. tail filename
    C. cat filename
    D. more filename

    Answer: B

    7.What is the purpose of the wc command?
    A. To count the number of files in a directory
    B. To display the contents of a file
    C. To count lines, words, and characters in a file
    D. To search for a string in a file

    Answer: C

    8. Which wildcard matches a range of characters specified inside square brackets?
    A. *
    B. ?
    C. [ ]
    D. #

    Answer: C

    9. What does the command ln -s filename linkname do?
    A. Creates a hard link to a file
    B. Creates a soft (symbolic) link to a file
    C. Deletes a file
    D. Renames a file

    Answer: B

    10. Which command would you use to count only the number of lines in a file?
    A. wc -w filename
    B. wc -c filename
    C. wc -l filename
    D. wc filename

    Answer: C



    7: Unix File Permission Setup

    πŸ”Ή File Ownership

    • Every file has:
      1. Owner β†’ the user who created it
      2. Group β†’ assigned user group
      3. Others β†’ all other users

    πŸ”Ή Permission Types

    • Owner permissions β†’ what the file’s creator can do
    • Group permissions β†’ what users in the same group can do
    • Other permissions β†’ what everyone else can do

    πŸ”Ή Permission Indicators

    • r (read) = 4
    • w (write) = 2
    • x (execute) = 1

    βœ… Example:

    -rwxr-xr--
    
    • Owner: rwx β†’ read, write, execute
    • Group: r-x β†’ read, execute
    • Others: r– β†’ read only

    πŸ”Ή Changing Permissions (chmod)

    1. Symbolic Mode

    • + β†’ add permission
    • - β†’ remove permission
    • = β†’ set exact permission
    chmod g+w filename    # add write permission to group
    chmod o-r filename    # remove read permission for others
    

    2. Absolute (Numeric) Mode

    • Add values (r=4, w=2, x=1) for each category:
      • 7 = rwx (4+2+1)
      • 6 = rw- (4+2)
      • 5 = r-x (4+1)
      • 4 = r– (4)
    chmod 755 filename    # Owner=rwx, Group=rx, Others=rx
    chmod 644 filename    # Owner=rw, Group=r, Others=r
    

    πŸ”Ή Changing Ownership

    • Change file owner: chown newowner filename
    • Change group: chgrp newgroup filename

    πŸ”Ή Special Permissions

    • SUID (Set User ID)
      • Run file with owner’s permissions
      • Example: passwd command (runs with root privileges)
    • SGID (Set Group ID)
      • Run file with group’s permissions
      • On directories: new files inherit group ownership
    • Sticky Bit
      • Applied on directories β†’ only owner, directory owner, or root can delete files
      • Commonly used in /tmp

    MCQ

    1. What does the ‘r’ permission allow a user to do?
    A. Write to the file
    B. Read the contents of the file
    C. Execute the file
    D. Change the file permissions

    Answer: B

    2. Which command is used to change the owner of a file?
    A. chgrp
    B. chmod
    C. chown
    D. mv

    Answer: C

    3. What does the numeric value ‘7’ represent in file permissions?
    A. Read and execute only
    B. Read, write, and execute
    C. Read only
    D. Write only

    Answer: B

    4. Which permission allows a user to delete a file?
    A. Read
    B. Write
    C. Execute
    D. None of the above

    Answer: B

    5. What does the ‘x’ permission allow for a directory?
    A. Read the contents of the directory
    B. Write new files to the directory
    C. Traverse into the directory
    D. Change the directory permissions

    Answer: C

    6. Which command is used to change the group ownership of a file?
    A. chown
    B. chmod
    C. chgrp
    D. mv

    Answer: C

    7. What does the SUID permission do?
    A. Allows users to read the file
    B. Allows users to execute the file with the owner’s permissions
    C. Allows users to write to the file
    D. Allows users to change the file’s group

    Answer: B

    8. Which of the following represents no permissions in numeric format?
    A. 1
    B. 0
    C. 4
    D. 2

    Answer: B

    10. What does the sticky bit do in a directory?
    A. Allows anyone to delete files
    B. Restricts file deletion to the file owner, directory owner, or root
    C. Grants execute permission
    D. Allows read access to all users

    Answer: B

    11. Which command would you use to set read and write permissions for the owner and read-only for the group and others?
    A. chmod 644 filename
    B. chmod 755 filename
    C. chmod 600 filename
    D. chmod 444 filename

    Answer: A



    8: Unix Environment

    πŸ”Ή What are Environment Variables?

    • Variables that store system and user settings.
    • Control how the shell and programs behave.
    • Examples: HOME, PATH, TERM, USER.

    πŸ”Ή Setting & Using Variables

    • Set variable: VARIABLE=value
    • Access value: echo $VARIABLE

    πŸ”Ή Shell Initialization

    • When you log in, shell reads config files to set environment.
    • System-wide: /etc/profile
    • User-specific: ~/.profile

    πŸ”Ή Important Variables

    • TERM β†’ defines terminal type (affects display/output).
    • PATH β†’ directories searched for commands. PATH=/bin:/usr/bin
    • PS1 β†’ primary prompt string (default: $ for user, # for root).
    • PS2 β†’ secondary prompt (default: > for multi-line input).
    • HOME β†’ user’s home directory.
    • PWD β†’ current working directory.
    • LANG β†’ system language/locale.

    πŸ”Ή Viewing Environment Variables

    • Use env to display all current environment variables: env

    MCQ

    1. What does the HOME environment variable represent?

    A. The current working directory

    B. The user’s home directory

    C. The path for executable files

    D. The terminal type

    Answer: B

    2. Which command is used to display all current environment variables?

    A. showenv

    B. env

    C. set

    D. printenv

    Answer: B

    3. What is the purpose of the PATH variable?

    A. To set the terminal type

    B. To define the user’s home directory

    C. To specify directories for executable commands

    D. To store user preferences

    Answer: C

    4. Which file is typically used for user-specific shell initialization?

    A. /etc/profile

    B. ~/.bashrc

    C. ~/.profile

    D. /etc/bash.bashrc

    Answer: C

    5. What does the PS1 variable control?

    A. The secondary command prompt

    B. The primary command prompt

    C. The terminal type

    D. The user’s home directory

    Answer: B

    6. Which of the following is NOT a common environment variable?

    A. TERM

    B. USER

    C. SHELL

    D. CONFIG

    Answer: D

    7. How do you set an environment variable in the shell?

    A. set VARIABLE=value

    B. VARIABLE=value

    C. export VARIABLE=value

    D. Both B and C

    Answer: D

    8. What does the PWD variable indicate?

    A. The path to the user’s home directory

    B. The current working directory

    C. The path for executable files

    D. The user’s login name

    Answer: B

    9. Which command would you use to check the current terminal type?

    A. echo $TERM

    B. show terminal

    C. get terminal

    D. print $TERM

    Answer: A

    10. What happens when you log into a Unix system?

    A. The system automatically sets the PATH variable

    B. The shell reads the profile files to set up the environment

    C. The user is prompted to enter environment variables

    D. The system displays the current date and time

    Answer: B


    9: The vi Editor

    πŸ”Ή Introduction

    • vi = powerful text editor in Unix.
    • Works in two modes:
      1. Command Mode β†’ run commands (save, quit, delete, etc.)
      2. Insert Mode β†’ type/edit text

    πŸ”Ή Starting vi

    • Open/create file β†’ vi filename
    • Open read-only β†’ view filename

    πŸ”Ή Switching Modes

    • Enter Insert Mode: press i (insert), a (append), or o (new line).
    • Return to Command Mode: press Esc.

    πŸ”Ή Exiting vi

    • :q β†’ quit
    • :q! β†’ quit without saving
    • :w β†’ save changes
    • :wq or ZZ β†’ save & quit

    πŸ”Ή Moving Around

    • h β†’ left, l β†’ right
    • j β†’ down, k β†’ up
    • 0 β†’ beginning of line
    • $ β†’ end of line

    πŸ”Ή Editing Text

    • x β†’ delete character
    • dd β†’ delete line
    • u β†’ undo last action

    πŸ”Ή Copy & Paste

    • yy β†’ copy (yank) line
    • p β†’ paste after cursor

    πŸ”Ή Searching

    • /word β†’ search forward
    • ?word β†’ search backward
    • n β†’ repeat search

    πŸ”Ή Replacing Text

    • :s/old/new/g β†’ replace all “old” with “new” in current line

    πŸ”Ή Advanced Commands

    • J β†’ join lines
    • R β†’ replace multiple characters
    • :set nu β†’ show line numbers

    MCQ

    1. Which command is used to start the vi editor with a file?

    A. edit filename

    B. vi filename

    C. open filename

    D. start filename

    Answer: B

    2. What mode do you enter when you press i in vi?

    A. Command mode

    B. Insert mode

    C. Visual mode

    D. Read-only mode

    Answer: B

    3. How do you save changes and exit vi?

    A. :q!

    B. :wq

    C. :exit

    D. ZZ

    Answer: B

    4. Which command deletes the character under the cursor in vi?

    A. d

    B. x

    C. del

    D. remove

    Answer: B

    5. What does the command dd do in vi?

    A. Deletes the current line

    B. Duplicates the current line

    C. Displays the current line

    D. Does nothing

    Answer: A

    6. Which command is used to search for a term in vi?

    A. find term

    B. /term

    C. search term

    D. locate term

    Answer: B

    7. What is the purpose of the command :s/old/new/g in vi?

    A. To save the file

    B. To search for a term

    C. To replace all occurrences of “old” with “new”

    D. To delete a line

    Answer: C

    8. How do you copy a line in vi?

    A. cp

    B. yy

    C. copy

    D. duplicate

    Answer: B

    9. Which command moves the cursor to the end of the line in vi?

    A. 0

    B. ^

    C. $

    D. G

    Answer: C

    10. What does the command u do in vi?

    A. Redoes the last action

    B. Undoes the last action

    C. Saves the file

    D. Exits the editor

    Answer: B



    10: Unix Pipes and Filters

    πŸ”Ή Pipes

    • Definition β†’ Connects two commands; output of one = input of another.
    • Syntax β†’ command1 | command2
    • Example β†’ ls | more (list files and view page by page)

    πŸ”Ή Filters

    • Definition β†’ Commands that process data and output results.
    • Common filters: grep, sort, more, pg.

    πŸ”Ή grep – Search Text

    • Syntax β†’ grep pattern filename
    • Options:
      • -i β†’ case-insensitive search
      • -v β†’ invert match (show lines not matching)
      • -n β†’ show line numbers

    πŸ”Ή sort – Arrange Text

    • Syntax β†’ sort filename
    • Options:
      • -n β†’ sort numerically
      • -r β†’ sort in reverse order

    πŸ”Ή Viewing Long Output

    • more β†’ view text one screen at a time (forward only).
    • pg β†’ view text one screen at a time (forward + backward).

    πŸ”Ή Combining Commands with Pipes

    • Build powerful chains of commands: ls -l | grep "Aug" | sort +4n | more β†’ Lists August files, sorts by size, shows page by page.

    πŸ”Ή Shell Scripts with Pipes & Filters

    • Automate tasks using pipes in scripts.
    • Script starts with shebang: #!/bin/sh command1 | command2 | command3

    πŸ‘‰ This version makes Pipes & Filters easy to grasp with definitions β†’ syntax β†’ examples flow.


    1. What is the purpose of a pipe in Unix?

    A. To connect two commands

    B. To create a file

    C. To delete a file

    D. To display help

    Answer: A

    2. Which command is used to search for a specific pattern in a file?

    A. find

    B. grep

    C. search

    D. locate

    Answer: B

    3. How do you sort the contents of a file in reverse order?

    A. sort -r filename

    B. sort filename -r

    C. sort filename –reverse

    D. sort -reverse filename

    Answer: A

    4. What does the -i option do in the grep command?

    A. Inverts the match

    B. Ignores case sensitivity

    C. Shows line numbers

    D. Displays the matched line only

    Answer: B

    5. Which command allows you to view the output one screen at a time?

    A. cat

    B. more

    C. head

    D. tail

    Answer: B

    6. What is the correct syntax to combine two commands using a pipe?

    A. command1; command2

    B. command1 | command2

    C. command1 & command2

    D. command1 > command2

    Answer: B

    7. Which command would you use to sort a list of files by size?

    A. ls -s

    B. sort -n

    C. sort -s

    D. ls -l

    Answer: B

    8. What does the -v option do in the grep command?

    A. Displays the matched lines

    B. Displays the line numbers

    C. Inverts the match to show non-matching lines

    D. Ignores case sensitivity

    Answer: C

    9. Which command is used to display the first few lines of a file?

    A. tail

    B. head

    C. more

    D. cat

    Answer: B

    10. What is the purpose of a shell script in Unix?

    A. To create a new user

    B. To automate a series of commands

    C. To manage files

    D. To display system information

    Answer: B