π Table of Contents
- Unix Basics for Beginners
- Unix Users
- Using Unix Commands
- Working with Files
- Unix Directories
- Wildcards and File Handling
- Unix File Permission Setup
- Unix Environment
- The vi Editor
- Unix Pipes and Filters
- 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
- Kernel β Core of the OS; controls hardware & system resources.
- Shell β Command-line interface; users type commands here.
- Commands β Instructions given to the system (e.g.,
ls,pwd). - 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
- Bourne Shell (sh): The original shell developed for UNIX. It provides a command-line interface and scripting capabilities.
- C Shell (csh): Introduced features like command history and job control. It uses a syntax similar to the C programming language.
- Korn Shell (ksh): Combines features of the Bourne and C shells, offering advanced scripting capabilities and improved performance.
- 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.
- 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
exitcommand.
πΉ 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
passwdcommand to change your password for better security.
πΉ Shell Startup Files
- System Startup File β
/etc/profile- Sets global environment variables for all users.
- 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
- Intrinsic (Built-in) β part of the shell itself
- Example:
cdβ change directory
- Example:
- Extrinsic (External) β stored as separate files in the system
- Example:
lsβ list files
- Example:
πΉ Commonly Used Commands
pwdβ show current directorywhoamiβ show your usernamewhoβ list users currently logged inunameβ show system information
πΉ Getting Help
- Use manual pages with
manman 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.txtandfile.txtare different
πΉ Hidden Files
- Files starting with a dot (.) are hidden
- Example:
.hiddenfile
- Example:
- 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
- Example:
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
- Example:
- Relative Path β starts from current directory
- Example:
docs/notes
- Example:
πΉ 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*β matchesmyfile,myfile1,myfile2
- Example:
?(Question Mark) β matches exactly 1 character- Example:
myfile0?β matchesmyfile00,myfile01(but notmyfile010)
- Example:
[ ](Square Brackets) β matches a range/set of characters- Example:
[a-c]β matchesa,b, orc
- Example:
πΉ 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 withmyinside/etc
πΉ Miscellaneous File Handling
π Head & Tail
head filenameβ first 10 linestail 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:
- Owner β the user who created it
- Group β assigned user group
- 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:
passwdcommand (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/binPS1β 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
envto 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:
- Command Mode β run commands (save, quit, delete, etc.)
- 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), oro(new line). - Return to Command Mode: press
Esc.
πΉ Exiting vi
:qβ quit:q!β quit without saving:wβ save changes:wqorZZβ save & quit
πΉ Moving Around
hβ left,lβ rightjβ down,kβ up0β beginning of line$β end of line
πΉ Editing Text
xβ delete characterddβ delete lineuβ undo last action
πΉ Copy & Paste
yyβ copy (yank) linepβ paste after cursor
πΉ Searching
/wordβ search forward?wordβ search backwardnβ repeat search
πΉ Replacing Text
:s/old/new/gβ replace all “old” with “new” in current line
πΉ Advanced Commands
Jβ join linesRβ 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
