Skip to document

Unix Modul 2 - Ntg

Ntg
Course

Software Engineering (CS530)

376 Documents
Students shared 376 documents in this course
Academic year: 2021/2022
Uploaded by:
Anonymous Student
This document has been uploaded by a student, just like you, who decided to remain anonymous.
Visvesvaraya Technological University

Comments

Please sign in or register to post comments.

Preview text

Module 02

Chapter 01

File Attributes & Permissions

The ls command with options:

The ls command is to obtain a list of all file names in the current directory. ls with options:

Option Description

-x Multicolumnar Output

-F Marks executables with *, directories with / and symbolic link with @

-a Shows all filenames beginning with a dot including. and ..

-R Recursive list

-r Sorts filenames in reverse order(ASCII collating sequence by default)

-l Long listing in ASCII collating sequence seven attributes of a file.

-d dirname Lists only dirname if dirname is a directory.

-t Sort filenames by last modification time.

-lt Sorts listing by last modification time.

-u Sorts filenames by last access time.

-lu Sorts by ASCII collating sequence but listing shows last access time.

-i Displays inode number.

Listing File Attributes(ls –l):

ls command is used to obtain a list of all filenames in the current directory. The output in UNIX lingo is often referred to as the listing. Sometimes we combine this option with other options for displaying other attributes, or ordering the list in a different sequence. ls look up the file‘s inode to fetch its attributes. It lists seven attributes of all files in the current directory and they:

  1. File type & Permission: The first column shows the type and permissions associated with each file. The first character in this column is mostly a -, which indicates that file is an ordinary one.
  2. Links: The second column indicates the number of links associated with the file. This is actually the number of filenames maintained by the system of that file.
  3. Ownership:When you create a file, you automatically become its owner. The third column shows kumar is the owner of all of these files. The owner has full authority to tamper with a files contents and permissions—a privilege not available with others except the root user.
  4. Group Ownership: The fourth column represents the group owner of the file. Every user is attached to a group owner. Every member of that group can access the file depending on the permission assigned.
  5. File size: File size in bytes is displayed. It is the number of character in the file rather than the actual size occupied on disk.
  6. Last modification time: Last modification time is the next field. If you change only the permissions or ownership of the file, the modification time remains unchanged. If at least one character is added or removed from the file then this field will be updated.
  7. File name: The last column displays the filenames arranged in ASCII collating sequence.

For Ex:

$ ls -l total 72 -rw-r--r-- 1 kumar metal 19514 may 10 13:45 chap -rw-r--r-- 2 kumar metal 19555 may 10 15:45 chap drwxr-xr-x 2 kumar metal 512 may 09 12:55 helpdir drwxr-xr-x 3 kumar metal 512 may 09 11:05 progs -d option: listing directory attributes $ ls -d helpdir progs drwxr-xr-x 2 kumar metal 512 may 09 12:55 helpdir drwxr-xr-x 3 kumar metal 512 may 09 11:05 progs

Changing File Permission:

A file or a directory is created with a default set of permissions, which can be determined by umask. Let us assume that the file permission for the created file is -rw-r-- r--. Using the chmod command, we can change the file permissions and allow the owner to execute his file. The command can be used in two ways:

  1. In a relative manner by specifying the changes to the current permissions.
  2. In an absolute manner by specifying the final permissions. Relative Permissions: ● chmod only changes the permissions specified in the command line and leaves the other permissions unchanged. ● Its syntax is: chmod category operation permission filename(s) ● chmod takes an expression as its argument which contains:
  3. user category (user, group, others).
  4. operation to be performed (assign or remove a permission).
  5. type of permission (read, write, execute). ● Category : u – user g – group o – others a - all (ugo) ● operations : + assign - remove = absolute ● permissions: r – read w – write x - execute Example: ● Initially, -rw-r—r-- 1 kumar metal 1906 sep 23:38 xstart $chmod u+x xstart -rwxr—r-- 1 kumar metal 1906 sep 23:38 xstart

● The command assigns (+) execute (x) permission to the user (u), other permissions remain unchanged. $chmod ugo+x xstart or chmod a+x xstart or chmod +x xstart $ls –l xstart -rwxr-xr-x 1 kumar metal 1906 sep 23:38 xstart

● chmod accepts multiple file names in command line $chmod u+x note note1 note ● Let Initially, -rwxr-xr-x 1 kumar metal 1906 sep 23:38 xstart $chmod go-r xstart Then it becomes $ls –l xstart -rwx—x--x 1 kumar metal 1906 sep 23:38 xstart

Recursively Changing File Permissions: It’s possible to make chmod descend a directory hierarchy and apply the expression to every file and subdirectory it finds. This is done with the – R(recursive) option:

$chmod –R a+x shell_scripts

This makes all files and subdirectories found in the tree-walk executable by all users. We can provide multiple directory and filenames.

If we want to use chmod on your home directory tree then “cd” to it and use it in one of these ways:

$chmod –R 755. # Works on hidden files also $chmod –R a+x * # Leaves out hidden files.

CHAPTER 02

The Shell Interpretive Cycle

Introduction:

 The shell sits between you and the operating system, acting as a command interpreter. It reads your terminal input and translates the commands into actions taken by the system. The shell is analogous to command in DOS.  When you log into the system you are given a default shell. When the shell starts up it reads its startup files and may set environment variables, command search paths, and command aliases, and executes any commands specified in these files.

 The original shell was the Bourne shell, sh. Every Unix platform will either have the Bourne shell, or a Bourne compatible shell available.

 Numerous other shells are available. Some of the more well known of these may be on your

Unix system: the Korn shell, ksh, by David Korn, C shell, csh, by Bill Joy and the Bourne Again SHell, bash, from the Free Software Foundations GNU project, both based on sh, the T-C shell, tcsh, and the extended C shell, cshe, both based on csh though the shell appears not to be doing anything meaningful when there is no activity at the terminal, it swings into action the moment you key in something.

 The following activities are typically performed by the shell in its interpretive cycle:

  1. Shell issues the prompt and waits for you to enter a command.
  2. After a command is issued, the shell scans command line for metacharacters and expands abbreviations to recreate a simplified command line.
  3. It then passes on the command line to kernel for execution.
  4. The shell waits for the command to complete and normally can’t do any work while the command is running.
  5. After the command execution, the prompt reappears and the shell returns to its waiting role to start the next cycle.

Pattern Matching-Wild Cards:

A pattern is framed using ordinary characters and a meta character (like *) using well- defined rules. The pattern can then be used as an argument to the command, and the shell will expand it suitably before the command is executed. The meta characters that are used to construct the generalized pattern for matching filenames belong to a category called wild-cards. The following table lists them:

Wild Card Matches

  • Any number of characters including none.

? A single character.

[ijk] A single character – either an i, j or k.

[x-z] A single character that is within the ASCII range of characters x and z.

[!ijk] A single character that is not an i, j or k (Not in C shell).

[!x-z] A single character that is not within the ASCII range of the characters x and z (Not in C Shell).

{pat1,pat2...} Pat1, pat2, etc. (Not in Bourne shell).

Hello students [ctrl+d] 1 2 15

When it is usec with arguments ,filename is passed as argument and wc takes input from filename

specified.

$cat >sample Hello students [ctrl+d]

wc < sample

1 2 15  The standard output can represent three possible destinations:  The terminal, the default destination.  A file using the redirection symbols > and >>.  As input to another program using a pipeline. When the notation > filename is added to the end of a command, the output of the

command is written to the specified file name. The > symbol is known as the output redirection operator command that outputs its results to the screen can have its output sent to a file.

For example, to send the results of the who command to a file called users enter:

who > users

To see the contents of the file users, enter:

cat users

A list similar to the following appears:

denise lft/0 May 13 08:

marta pts/1 May 13 08: endrica pts/2 May 13 09:

For example, to send the current directory listing to a file, enter:

ls > dirlist

When the notation > > filename is added to the end of a command, the output of the command is

appended to the specified file name rather than writing over any existing data. The > > symbol is

known as the append redirection operator.

For example, to append file2 to file1, enter:

cat file2 > > file In addition to the standard input and standard output, commands often produce other types of output, such as error or status messages known as diagnostic output. Like standard output, standard error output is written to the screen unless redirected, when a command starts, three files are already open: stdin (standard input), stdout (standard output), and stderr (standard error). If you want to redirect standard input or standard output, you can use the <, >, or > > symbols. However, if you want to redirect standard error or other output, you must use a file descriptor. File descriptors can also be specified to redirect standard input and standard output, but are already the default values. A file descriptor is a number associated with each of the I/O files a command ordinarily uses. The following numbers are associated with standard input, output, and error:

0 Standard input (keyboard)

1 Standard output (display)

2 Standard error (display)

To redirect standard error output, type the file descriptor number 2 in front of the output or

append redirection symbols (> or > >) and a file name after the symbol. For example, the

following command takes the standard error output from the cc command where it is used to

compile testfile and appends it to the end of the ERRORS file:

cc testfile 2 > > ERRORS

Examples:

Assuming file2 doesn’t exist, the following command redirects the standard output to file myOutput and the standard error to file myError. $ls –l file1 file2 1>myOutput 2>myError

Connecting Commands: Pipe

With piping, the output of a command can be used as input (piped) to a subsequent command. $ command1 | command Output from command1 is piped into input for command2. This is equivalent to, but more efficient than: $ command1 > temp $ command2 < temp $ rm temp Examples: $ ls -l | wc –l Displays number of file in current directory $ who | wc –l Displays number of currently logged in users

grep: Searching for a pattern

You often need to search a file for a pattern, either to see the lines containing ( or not containing) it or to have it replaced with something else. This chapter discusses two important filters that are specially suited for these tasks- grep and sed. This chapter also takes up one of the fascinating features of UNIX – regular expressions (RE). To discuss all the examples in this chapter we use following emp as the reference file.

Examples:

  1. Ignoring case (-i): When you look for a name but are not sure of the case, use the -i (ignore) option.

  2. Deleting Lines (-v): The -v option selects all the lines except those containing the pattern. It can play an inverse role by selecting lines that does not containing the pattern.

3 Line Numbers (-n): The -n(number) option displays the line numbers containing the pattern, along with the lines.

  1. Counting lines containing Pattern (-c): How many directors are there in the file emp? The -c(count) option counts the number of lines containing the pattern.

]5. Matching Multiple Patterns (-e): With the -e option, you can match the three agarwals by using the grep like this:

  1. Taking patterns from a file (-f): You can place all the patterns in a separate file, one pattern per line. Grep uses -f option to take patterns from a file: $ cat patterns sales marketing $ grep -f patterns emp 9876|jai sharma |director |production |12/03/50| 2365|barun sengupta |director |personnel |11/05/47|
  • 5423|n. k. gupta |chairman |admin |30/08/56|
  • 1006|chanchal singhvi |director |sales |03/09/38|
  • 1265|s. n. dasgupta |manager |sales |12/09/63|
  • 2476|anil aggarwal |manager |sales |01/05/59|
  • 6521|lalit chowdury |director |marketing |26/09/45|
Was this document helpful?

Unix Modul 2 - Ntg

Course: Software Engineering (CS530)

376 Documents
Students shared 376 documents in this course
Was this document helpful?
Module 02
Chapter 01
File Attributes & Permissions
The ls command with options:
The ls command is to obtain a list of all file names in the current directory. ls with options:
Option Description
-x Multicolumnar Output
-F Marks executables with *, directories with / and symbolic link with @
-a Shows all filenames beginning with a dot including . and ..
-R Recursive list
-r Sorts filenames in reverse order(ASCII collating sequence by default)
-l Long listing in ASCII collating sequence seven attributes of a file.
-d dirname Lists only dirname if dirname is a directory.
-t Sort filenames by last modification time.
-lt Sorts listing by last modification time.
-u Sorts filenames by last access time.
-lu Sorts by ASCII collating sequence but listing shows last access time.
-i Displays inode number.
Listing File Attributes(ls –l):
ls command is used to obtain a list of all filenames in the current directory. The output in
UNIX lingo is often referred to as the listing. Sometimes we combine this option with other
options for displaying other attributes, or ordering the list in a different sequence. ls look up the
file‘s inode to fetch its attributes. It lists seven attributes of all files in the current directory and
they:
1. File type & Permission: The first column shows the type and permissions associated with
each file. The first character in this column is mostly a -, which indicates that file is an ordinary
one.
2. Links: The second column indicates the number of links associated with the file. This is
actually the number of filenames maintained by the system of that file.
3. Ownership:When you create a file, you automatically become its owner. The third column
shows kumar is the owner of all of these files. The owner has full authority to tamper with a
files contents and permissions—a privilege not available with others except the root user.
4. Group Ownership: The fourth column represents the group owner of the file. Every user is
attached to a group owner. Every member of that group can access the file depending on the
permission assigned.
5. File size: File size in bytes is displayed. It is the number of character in the file rather than
the actual size occupied on disk.
6. Last modification time: Last modification time is the next field. If you change only the
permissions or ownership of the file, the modification time remains unchanged. If at least one
character is added or removed from the file then this field will be updated.
7. File name: The last column displays the filenames arranged in ASCII collating sequence.