0% found this document useful (0 votes)
10 views21 pages

Aabid Unix

Unix projects file basic level

Uploaded by

Mohammad Aabid
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)
10 views21 pages

Aabid Unix

Unix projects file basic level

Uploaded by

Mohammad Aabid
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/ 21

A PRACTICAL FILE

ON

UNIX Lab

BCA-506P

The Study Hall College

Affiliated To

UNIVERSITY OF LUCKNOW

Course
Bachelors in Computer Application
2024-2025

SUBMITTED TO: SUBMITTED BY:


Juan Carlos Mohammad Aabid
2212414040027
INDEX
S. No. Topic Page No. Signature

1 Execute the following list of basic commands in UNIX: 1


(i)pwd
(ii) mkdir
(iii) cd
2 Execute the following list of basic commands in UNIX: 2
(i)who
(ii) echo
(iii) cat
3 Execute the following list of basic commands in UNIX: 3
(i)rm
(ii)mv
(iii)wc
(iv)cp
4 Execute the basic file attributes with all possible options: 7
(i)ls
(ii)chmod
5 Execute basic commands using vi editor: 13
a. Input mode commands
b. Saving text and quitting
6 Execute basic commands using vi editor: 14
a. Navigation
b. Editing text
c. Searching pattern
7 Execute the following filters using regular expressions with all 16
possible options:
(i) grep
(ii) sed
8 Write a shell script to display current date and calendar 19
STUDY HALL COLLEGE UNIX Lab 2024

1. Execute the following list of basic commands in UNIX:


(i) pwd
pwd (print working directory) is a command that prints the current directory.pwd command
writes to standard output the full path name of your current directory (from the root directory).
Syntax: pwd[options]

(ii) mkdir
The mkdir command in UNIX is a command-line utility that allows users to create new directories.
mkdir stands for “make directory”.
Syntax: mkdir[options][directories]

(iii) cd
cd stands for “change directory”. The cd command is used to change the current working directory.
Using ‘cd’ followed by a directory name or path changes your location within the file system.
Syntax: cd[directory]

1|Page
STUDY HALL COLLEGE UNIX Lab 2024

2. Execute the following list of basic commands in UNIX:


(i) who
The who command is used to show the user’s list logged in currently.
Syntax: who [options][filename]

(ii) echo
Displays lines of text or strings that are passed as arguments on the command line. It is commonly
used in shell scripts and Bash files to display output status text.
Syntax: echo[options][string]

(iii) cat
Reads files sequentially, writing them to standard output. The name comes from its function to
concatenate files.
Syntax: cat file_name

2|Page
STUDY HALL COLLEGE UNIX Lab 2024

3. Execute the following list of basic commands in UNIX:


(i) rm
The rm command removes files, directories, symbolic links and so on from the file systems.
Syntax: rm file_name

Options of rm command:-
a) -r, -R, --recursive: Recursively remove directories and their
contents.rm -r directory_name

b) -d, --dir: Remove empty directories.


rm -d empty_directory

(ii) mv
`mv` stands for “move”. But in this article, we will be talking about the “mv command in
Linux”.
This command is used to rename files or move files.
Syntax: mv file_name Directory
MV is used to move file one place to another place.

3|Page
STUDY HALL COLLEGE UNIX Lab 2024

Syntax: mv file_Name1 file_Name2 Directory

Syntax: mv old_filename new_filename


MV is used to rename file and move the old file to new file.

(iii) wc
The wc command in UNIX is a command line utility or printing newline, word
and byte counts for files. It can return the number of lines in a file, the number
of characters in a file and the number of words in a file.
Syntax: wc file_name

Options of wc command : -

a) -l, --lines: Print the number of lines.


wc -l filename

4|Page
STUDY HALL COLLEGE UNIX Lab 2024

b) -w, --words: Print the number of words.


wc -w filename

c) -c, --bytes: Print the number of bytes.


wc -c filename

d) -m, --chars: Print the number of characters.


rm -i file_name

e) -L, --max-line-length: Print the length of the longest line.


wc -L filename

5|Page
STUDY HALL COLLEGE UNIX Lab 2024

(iv) Cp

The cp command copies the contents of the source file or directory to the target
file or directory. If the target file exists, the copy will overwrite the original
contents.
Syntax: cp file_name Destination_name

6|Page
STUDY HALL COLLEGE UNIX Lab 2024

4. Execute the basic file attributes with all possible options:


(i) ls
The ls command in UNIX lists the names of the files in a directory.
Syntax: ls [option] [file/directory]

Options:

a) -l: Long format listing.


ls –l

b) a: List all entries including hidden files (those starting with a dot).
ls –a

c) -R: Recursive listing (lists subdirectories recursively).


ls -R

7|Page
STUDY HALL COLLEGE UNIX Lab 2024

d) -t: Sort by modification time, newest first.

ls –lt

e) -d: List directories themselves, not their contents.


ls -d

(ii) Chmod

The chmod command is used to change the access mode of a file. The name is an abbreviation
of change mode. Which states that every file and directory has a set of permissions that control the
permissions like who can read, write or execute the file. In this the permissions have three
categories: read, write, and execute simultaneously represented by `r`, `w` and `x`. These letters
combine together to form a specific permission for a group of users.
The `chmod` command is used to modify this permission so that it can grant or restrict access to
directories and files.

Syntax: chmod [options][mode][file_name]


Options:
 -R (Apply the permission change recursively to all the files and directories within the
specified directory.)
 -v (It will display a message for each file that is processed. while indicating the permission
change that was made.)
 -c (It works same as `-v` but in this case it only displays messages for files whose
permission is changed.)
 -f (It helps in avoiding display of error messages.)
 -h (Change the permissions of symbolic links instead of the files they point to.)

8|Page
STUDY HALL COLLEGE UNIX Lab 2024

Each file is associated with a particular owner & have permission access for different users.
The userclasses may be:
 Owner
 Group member
 Other ( everybody else )

This mode can be specified in several ways, we will discuss 2 modes:


1. Symbolic Mode
2. Absolute Mode

1. Symbolic Mode:
The symbolic method in chmod allows you to change file permissions using a symbolic
representation,where you use letters and symbols to specify the changes you want to make to the
existing permissions.

The basic syntax for symbolic mode is:


chmod [options] permissions file
Here's a breakdown of the symbolic representation:
 u: User/Owner
 g: Group
 o: Others
 a: All (user, group, and others)
 +: Add permission
 -: Remove permission
 =: Set exact permission

9|Page
STUDY HALL COLLEGE UNIX Lab 2024

Add or Remove Individual Permissions:

Setting Permissions with =:

2. Absolute Mode:
The absolute mode in the chmod command allows you to set permissions explicitly using a
three-digitoctal (base-8) representation. Each digit corresponds to the permission settings for the
user, group, andothers, respectively. The digits are calculated by adding the values for read (4),
write (2), and execute (1).
The basic syntax for absolute mode is:
chmod xxx file
Here, each 'x' represents a digit (0-7) corresponding to the permission settings. The three digits
togetherrepresent the permissions for the user, group, and others.
 The first digit: User/Owner permissions
 The second digit: Group permissions
 The third digit: Others
permissionsThe digits of the
permissions are:-

 4: Read permission
 2: Write permission
 1: Execute permission

10 | P a g e
STUDY HALL COLLEGE UNIX Lab 2024

You add these values to get the desired combination. For example, read and execute would be 4 + 1 = 5.

Setting Read, Write, and Execute for User, Read and Execute for Group, and Read-only for Others:

Read and Write for User, Read-only for Group and Others:

Full Permissions for Everyone:

11 | P a g e
STUDY HALL COLLEGE UNIX Lab 2024

5. Execute the basic command using vi editor:


(a)input mode commands
The vi editor is elaborated as visual editor. It is installed in every Unix system. In other words, it is
available in all Linux distros. It is user-friendly and works same on different distros and platforms.
It is a very powerful application. An improved version of vi editor is vim.
Insert mode is the mode to be in when inserting text into the file. Command mode is the mode to
be in when giving commands which will move the cursor, delete text, copy and paste, save the file
etc. When entering a file, vi is in command mode. To enter text, you must enter insert mode. If in
insert mode, enter command mode by hitting the escape, <esc>, key.
Syntax: vi File_name
Some of the input mode commands are:
 i (insert text before the cursor)
 a (insert text after the cursor)
 A (insert text at the end of the line)
 o (open a line below the current line)
 O (open a line above the current line)
 r (replace the current character)
 R (replace characters until <ESC>, overwrite)

(b) Saving text and quiting


You can save and quit vi editor from command mode. Before writing save or quit command you
have to press colon (:). Colon allows you to give instructions to vi.
Commands:
 :wq (Save and quit)
 :w (Save)
 :q (Quit)
 :w fname (Save as fname)
 ZZ (Save and Quit)
 :q! (Quit discarding changes made)
 :w! (Save (and write to non-writable file))

12 | P a g e
STUDY HALL COLLEGE UNIX Lab 2024

6. Execute Basic commands using vi editor:


(a) Navigation
In Normal Mode, you execute commands to navigate and manipulate text. The key to efficient
navigation lies in mastering the commands available in this mode.

Basic Navigation Commands:


1. Moving the Cursor:
 Use the arrow keys (up, down, left, right) to move the cursor one character at a time.
 h (moves the cursor left)
 j (moves the cursor down)
 k (moves the cursor up)
 l (moves the cursor right)
2. Word Navigation:
 w (moves the cursor to the beginning of the next word)
 e (moves the cursor to the end of the current word)
 b (moves the cursor to the beginning of the previous word)
3. Line Navigation:
 0 (moves the cursor to the beginning of the current line)
 $ (moves the cursor to the end of the current line)
 ^ (moves the cursor to the first non-blank character of the current line)
4. Scrolling:
 Ctrl + u (scrolls up (half-screen))
 Ctrl + d (scrolls down (half-screen))
 Ctrl+ b (scrolls up (full screen))
 Ctrl + f (scrolls down (full screen))
5. Searching:
 / (followed by a search term initiates a forward search)
 ? (followed by a search term initiates a backward search)
 Press n to find the next occurrence, or `N` to find the previous one.

(b) Editing Text


We can edit text using vi editor. vi editor has two modes: Command and insert mode. When you first
open a file with vi, you are in command mode. To enter insert mode press ‘i’ or to exit insert mode
press Esc key once.
 a (insert text after the cursor)
 i (insert text before the cursor)
 A (insert text at the end of the line)
 (open a line below the current line)
 (open a line above the current line)
 r (replace the current character)
 R (replace characters until <ESC>, overwrite)
Deleting Characters:
 X (Deletes the character before the cursor location)
 x (Deletes the character at the cursor location)
 dw (Deletes from the current cursor location to the next word)
 d^ (Deletes from current cursor position to the beginning of the line)
 d$ (Deletes from current cursor position to the end of the line)
 dd (Deletes the line the cursor is on)

14 | P a g e
STUDY HALL COLLEGE UNIX Lab 2024

Copy and paste Commands:


 yy (copies the current line)
 9yy (yank current line and 9 lines below)
 p (puts the copied text after the cursor)
 P (puts the yanked text before the cursor)

Save and Exit commands:


 :w (Save)
 :q (Quit)
 :w fname (Save as fname)
 ZZ (Save and Quit)
 :q! (Quit discarding changes made)
 :w! (Save (and write to non-writable file))
 :wq (Save and quit)

(c) Searching Pattern


In command mode, with the help of ‘/’ , string can be searched in a forward direction and with the
help of ‘?’, string can be searched in a backward direction .
Syntax: /String

14 | P a g e
STUDY HALL COLLEGE UNIX Lab 2024

7. Execute the following filters using regular expression with all possible options:
(i) grep
The grep filter searches a file for a particular pattern of characters, and displays all lines that contain
patterns. The pattern that is searched in the file is referred to as the regular expression. grep stands
for global search for regular expression and print out.
Syntax: grep [options] “string” file_name
Options:
 -i (ignore case distinctions in both the pattern and the input files)
 -r, -R (Recursively search subdirectories)
 -n (Display line numbers along with the lines containing the pattern)
 -c (Display only the count of lines that match the pattern)
 -l (Displays only the names of file containing the pattern)

Options of grep command:


a) -i, --ignore-case: Ignore case distinctions in both the pattern and the input files.
grep -i "pattern" file.txt

b) -r, -R, --recursive: Recursively search subdirectories.


grep -r "pattern" directory/

c) -n, --line-number: Display line numbers along with the lines containing the
pattern.wc -cfilename.
grep -n "pattern" file.txt

d) -c, --count: Display only the count of lines that match the pattern.

16 | P a g e
STUDY HALL COLLEGE UNIX Lab 2024

grep -c "pattern" file.txt

e) -l, --files-with-matches: Display only the names of files containing the


pattern.grep -l "pattern" directory/

(ii) sed
sed is a text stream editor used to edit files quickly and efficiently. The tool searches through,
replaces, adds and deletes lines in a text file without opening the file in text editor. Using it complex
pattern matching can be performed.
Syntax: sed [options] ‘address action’ [file_name]

• SED is a powerful text stream editor. Can do insertion, deletion, search and replace(substitution).
• SED command in unix supports regular expression which allows it perform complex pattern
matching.

Sed command is mostly used to replace the text in a file.

17 | P a g e
STUDY HALL COLLEGE UNIX Lab 2024

Printing only the replaced lines :

To Delete a particular line say n :

18 | P a g e
STUDY HALL COLLEGE UNIX Lab 2024

8. Write a shell script to display current date and calendar.


A shell script is a list of commands in a computer that is run by the Unix shell which is command line

interpreter. A shell script usually has the comments the describe the steps. The different operations
performed by shell scripts are program execution, file manipulation and text printing

15 | P a g e
STUDY HALL COLLEGE UNIX Lab 2024

19 | P a g e

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy