21CSC202J-Operating Systems
Exp. No.:2(a) BASIC LINUX COMMANDS Date:
Aim:
To execute basic linux commands
Procedure:
a) Basics
1. echo SRM ➔ to display the string SRM
2. clear ➔ to clear the screen
3. date ➔ to display the current date and time
4. cal 2003 ➔ to display the calendar for the year 2003
5. cal 6 2003 ➔ to display the calendar for the June-2003
6. passwd ➔ to change password
b) Working with Files
1. ls ➔ list files in the present working directory
ls –l ➔ list files with detailed information (long list)
ls –a ➔ list all files including the hidden files
2. cat > f1 ➔ to create a file (Press ^d to finish typing)
cat f1 ➔ display the content of the file f1
3. wc f1 ➔ list no. of characters, words & lines of a file f1
wc –c f1 ➔ list only no. of characters of file f1
wc –w f1 ➔ list only no. of words of file f1
wc –l f1 ➔ list only no. of lines of file f1
4. cp f1 f2 ➔ copy file f1 into f2
5. mv f1 f2 ➔ rename file f1 as f2
6. rm f1 ➔ remove the file f1
7. head –5 f1 ➔ list first 5 lines of the file f1
tail –5 f1 ➔ list last 5 lines of the file f1
c) Working with Directories
1. mkdir elias ➔ to create the directory elias
2. cd elias ➔ to change the directory as elias
© SRMIST
21CSC202J-Operating Systems
3. rmdir elias ➔ to remove the directory elias
4. pwd ➔ to display the path of the present working directory
5. cd ➔ to go to the home directory
cd .. ➔ to go to the parent directory
cd - ➔ to go to the previous working directory
cd / ➔ to go to the root directory
d) File name substitution
1. ls f? ➔ list files start with ‘f’ and followed by any one
character
2. ls *.c ➔ list files with extension ‘c’
3. ls [gpy]et ➔ list files whose first letter is any one of the
character g, p or y and followed by the word et
4. ls [a-d,l-m]ring ➔ list files whose first letter is any one of
the character from a to d and l to m and followed by the word
ring.
e) I/O Redirection
Input redirection
wc –l < ex1 ➔ To find the number of lines of the file ‘ex1’
Output redirection
who > f2 ➔ the output of ‘who’ will be redirected to file f2
cat >> f1 ➔ to append more into the file f1
f) Piping
Syntax :
Command1 | command2
Output of the command1 is transferred to the command2 as input.
Finally output of the command2 will be displayed on the monitor.
Example.
cat f1 | more list the contents of file f1 screen by screen
head –6 f1 |tail –2 prints the 5th & 6th lines of the file f1.
g) Environment variables
1. echo $HOME ➔ display the path of the home directory
2. echo $PS1 ➔ display the prompt string $
© SRMIST
21CSC202J-Operating Systems
3. echo $PS2 ➔ display the second prompt string ( > symbol by
default )
4. echo $LOGNAME ➔ login name
5. echo $PATH ➔ list of pathname where the OS searches for an
executable file
h) File Permission
-- chmod command is used to change the access permission of a file.
Method-1:
Syntax :
chmod ugoa+/-rwx filename
u : user, g : group, o : others, + : Add permission - : Remove the permission r : read, w : write, x
:execute, a : all permissions
Example:
chmod ug+rw f1
Adding ‘read & write’ permissions of file f1 to both user and group members.
Method-2
Syntax :
chmod octnum file1
The 3 digit octal number represents as follows:
First digit -- file permissions for the user
Second digit -- file permissions for the group
Third digit -- file permissions for others
Each digit is specified as the sum of following:
4 – read permission, 2 – write permission, 1 – execute permission
Example:
chmod 754 f1
It changes the file permission for the file as follows:
read, write & execute permissions for the user ie; 4+2+1 = 7
read, & execute permissions for the group members ie; 4+0+1 = 5
only read permission for others ie; 4+0+0 = 4
© SRMIST
21CSC202J-Operating Systems
Exp. No.:2(b) FILTERS and ADMIN COMMANDS Date:
Aim:
To execute filters and admin commands.
Procedure:
A. FILTERS
1. cut
Used to cut characters or fileds from a file/input
Syntax :
cut -cchars filename
-ffieldnos filename
Example:
cut –c 5 file.txt
cut –c 5-10 file.txt
cut –c 5,7,9 file.txt
By default, tab is the filed separator(delimiter). If the fileds of the files are separated by any other
character, we need to specify explicitly by –d option
cut -ddelimitchar -ffileds filename
2. grep
Used to search one or more files for a particular pattern.
Syntax :
grep pattern filename(s)
Lines that contain the pattern in the file(s) get displayed
pattern can be any regular expressions
More than one files can be searched for a pattern
-v option displays the lines that do not contain the pattern
-l list only name of the files that contain the pattern
-n displays also the line number along with the lines that matches the pattern
© SRMIST
21CSC202J-Operating Systems
3. sort
Used to sort the file in order
Syntax :
sort filename
Sorts the data as text by default
Sorts by the first filed by default
-r option sorts the file in descending order
-u eliminates duplicate lines
-o filename writes sorted data into the file fname
-tdchar sorts the file in which fileds are separated by dchar
-n sorts the data as number
+1n skip first filed and sort the file by second filed numerically
4. uniq
Displays unique lines of a sorted file
Syntax :
uniq filename
-d option displays only the duplicate lines
-c displays unique lines with no. of occurrences.
5. diff
Used to differentiate two files
Syntax :
diff f1 f2
compare two files f1 & f2 and prints all the lines that are differed between f1 & f2.
Questions:
Q1. Write a command to cut 5 to 8 characters of the file f1.
$
Q2. Write a command to display user-id of all the users in your system.
$
Q3. Write a command to check whether the user judith is available in your system or not. (use grep)
$
© SRMIST
21CSC202J-Operating Systems
Q4. Write a command to display the lines of the file f1 starts with SRM.
$
Q5. Write a command to sort the file /etc/passwd in descending order
$
Q6. Write a command to display the unique lines of the sorted file f21. Also display the number of
occurrences of each line.
$
Q7. Write a command to display the lines that are common to the files f1 and f2.
$
© SRMIST
21CSC202J-Operating Systems
B. SYSTEM ADMIN COMMANDS
INSTALLING SOFTWARE
1. Update the package repositories
sudo apt-get update
2. Update installed software
sudo apt-get upgrade
3. Install a package/software
sudo apt-get install <package-name>
4. Remove a package from the system
sudo apt-get remove <package-name>
5. Reinstall a package
sudo apt-get install <package-name> --reinstall
Q8. Update the package repositories
Q9. Install the package “simplescreenrecorder”
Q10. Remove the package “simplescreenrecorder”
MANAGING USERS
Managing users is a critical aspect of server management.
In Ubuntu, the root user is disabled for safety.
Root access can be completed by using the sudo command by a user who is in the “admin” group.
When you create a user during installation, that user is added automatically to the admin group.
6. Add a user
sudo adduser username
7. Disable a user
sudo passwd -l username
8. Enable a user
sudo passwd -u username
9. Delete a user
sudo userdel –r username
10. Create a group
sudo addgroup groupname
11. Delete a group
sudo delgroup groupname
© SRMIST
21CSC202J-Operating Systems
12. Create a user with group
sudo adduser username groupname
13. See the password expiry value for a user,
sudo chage -l username
14. Make changes
sudo chage username
15. GUI Tool for user management
If you do not want to run the commands in terminal to manage users and groups, then you can
install a GUI add-on .
sudo apt install gnome-system-tools
Once done, type
users-admin
Q11. Create a user ‘elias’. Login to the newly created user and exit.
$
Q12. Disable the user ‘elias’, try to login and enable again.
$
© SRMIST