Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 35

UNIT-II

STANDARD INPUT OUPUT/ REDIRCTION

Redirection is a feature in Linux such that when executing a command, you can change the standard
input/output devices. The basic workflow of any Linux command is that it takes an input and give an
output.

 The standard input (stdin) device is the keyboard.


 The standard output (stdout) device is the screen.

Output Redirection

The output from a command normally intended for standard output can be easily
diverted to a file instead the terminal. This capability is known as output redirection.
If the notation > file is appended to any command that normally writes its output to
standard output, the output of that command will be written to file instead of your
terminal.
Check the following wc command which redirects the complete output of the command
in the users file.
$ WC > Filename
Input Redirection

Just as the output of a command can be redirected to a file, so can the input of a
command be redirected from a file. As the greater-than character > is used for output
redirection, the less-than character < is used to redirect the input of a command.
The commands that normally take their input from the standard input can have their
input redirected from a file in this manner. For example, to count the number of lines in
the file users generated above, you can execute the command as follows −
$ wc< Filename
The ‘<‘ symbol is used for input(STDIN) redirection

Example: The mail program in Linux can help you send emails from the Terminal.

You can type the contents of the email using the standard device keyboard. But if you
want to attach a File to email you can use the input re-direction operator in the following
format.

Mail -s "Subject" to- address < Filename

This would attach the file with the email, and it would be sent to the recipient.
Redirection Commands

1. pgm > file Output of pgm is redirected to file


2. pgm < file Program pgm reads its input from file
3. n >> file Output from stream with descriptor n appended to file
4. << tag Standard input comes from here through next tag at the start of line

Piping.
Piping involves passing output from one command as input to another.

The operator used is the vertical bar on your keyboard,


the | symbol. To "pipe" one command to another, separate the
commands with this operator. It looks like this:
$ command1 | command2 | command3

An example

cat text1.txt | wc

From the above command we concatenate contents of text1.txt, the output of this
command is then piped to the wc command.
We can also combine multiple pipes as follows

Environment Variables and Local Variables

Environment Variables are dynamic values which affect the processes or programs on a computer. They
exist in every operating system, but types may vary. Environment variables can be created, edited, saved,
and deleted and give information about the system behavior. They are declared as global i.e. visible to
everyone. For example, the environment variable can store information about the default text editor or
browser, the path to executable files, or the system location and keyboard layout settings.

Commands to be used for Environment variables


 $ env shows all the environment variables. It is an external command and runs in a child process.
It thus lists only those variables that it has inherited from its parent. By convention environment
variable names are defined in UPPER CASE.
 The set statement displays all variables available in the current shell but the env command
displays only environment variable.

COMMON ENVIRONMENT VARIABLES

HOME
The system shall initialize this variable at the time of login to be a pathname of the user's home
directory.
LOGNAME
The system shall initialize this variable at the time of login to be the user's login name. For a value
of LOGNAME to be portable across implementations of IEEE Std 1003.1-2001, the value should
be composed of characters from the portable filename character set.
PATH
This variable shall represent the sequence of path prefixes that certain functions and utilities apply
in searching for an executable file known only by a filename. The prefixes shall be separated by a
colon ( ':' ). When a non-zero-length prefix is applied to this filename, a slash shall be inserted
between the prefix and the filename. A zero-length prefix is a legacy feature that indicates the
current working directory. It appears as two adjacent colons ( "::" ), as an initial colon preceding
the rest of the list, or as a trailing colon following the rest of the list. A strictly conforming
application shall use an actual pathname (such as .) to represent the current working directory in
PATH . The list shall be searched from beginning to end, applying the filename to each prefix,
until an executable file with the specified name and appropriate execution permissions is found. If
the pathname being sought contains a slash, the search through the path prefixes shall not be
performed. If the pathname begins with a slash, the specified path is resolved. If PATH is unset or
is set to null, the path search is implementation-defined.
PWD
This variable shall represent an absolute pathname of the current working directory. It shall not
contain any filename components of dot or dot-dot. The value is set by the cd utility.
SHELL
This variable shall represent a pathname of the user's preferred command language interpreter. If
this interpreter does not conform to the Shell Command Language in the Shell and Utilities
volume of IEEE.
TERM
This variable shall represent the terminal type for which output is to be prepared. This information
is used by utilities and application programs wishing to exploit special capabilities specific to a
terminal. The format and allowable values of this environment variable are unspecified.

Local Variables: They are more restricted in the scope. A variable declared as local is one that is visible
only within the block of code in which it appears. It has local "scope". A local variable can be converted
to an environment variable using shell export statement.

The user defined variables are by default local in scope.


$ Variable_name = value
is used to create and initialize the variable. Variable can be made global by exporting it like:
$ export Variable_name
Whenever a variable is exported it becomes the part of the environment.

File security
Linux is, the multi-user operating system which can be
accessed by many users simultaneously. Linux can also be used
in mainframes and servers without any modifications. But this
raises security concerns as an unsolicited or malign user can
corrupt, change or remove crucial data. For effective security,
Linux divides authorization into 2 levels.

1. Ownership
2. Permission

Ownership of Linux files

Every file and directory on our Linux system is having 3 types of


owner.

1. User

A user is the owner of the file. By default, the person who created a
file becomes its owner.

2. Group

A user- group can contain multiple users. All users belonging to a


group will have the same access permissions to the file.

3. All

All user who has access to a file. This person has neither created
the file, nor he belongs to a usergroup who could own the file.
Practically, it means everybody else. Hence, when you set the
permission for all, it is also referred as set permissions for the
world.

Now, the big question arises how does Linux distinguish


between these three user types so that a user 'A' cannot affect a
file which contains some other user 'B's' vital information/data.

Permissions

Every file and directory in your Linux system has 3 permissions

 Read: This permission gives you the authority to open


and read a file. Read permission on a directory gives you
the ability to lists it’s content.

 Write: The write permission gives you the authority to


modify the contents of a file. The write permission on a
directory gives you the authority to add, remove and
rename files stored in the directory.

 Consider a scenario where you have to write permission


on file but do not have write permission on the directory
where the file is stored. You will be able to modify the file
contents. But you will not be able to rename, move or
remove the file from the directory.
 Execute: In Windows, an executable program usually has
an extension ".exe" and which you can easily run. In
Unix/Linux, you cannot run a program unless the execute
permission is set. If the
execute permission is not set, you might still be able to
see/modify the program code(provided read & write
permissions are set), but not run it.

Here, the first '-' implies that we have selected a file.p>

Else, if it were a directory, d would have been shown.

r = read permission
w = write permission
x = execute permission
- = no permission

The first part of the code is 'rw-'. This suggests that the owner 'Home' can:

 Read the file


 Write or edit the file
 He cannot execute the file since the execute bit is set to '-'.
 Read the file
Chmod command

We can use the 'chmod' command which stands for 'change mode'. Using the command, we can set
permissions (read, write, execute) on a file/directory for the owner, group and the world. Syntax:

chmod permissions filename

Two types of mode

1. Symbolic mode
2. Absolute mode

Symbolic Mode
In the symbolic mode, you can modify permissions of a specific owner. It makes use of
mathematical symbols to modify the file permissions.

Operator Description

+ Adds a permission to a file or


directory

- Removes the permission

= Sets the permission and overrides the


permissions set earlier.

Example :-
Absolute (Numeric) Mode
In this mode, file permissions are not represented as characters but a three-digit octal
number.

Number Permission Type Symbol

0 No Permission ---

1 Execute --x

2 Write -w-

3 Execute + Write -wx

4 Read r--

5 Read + Execute r-x

6 Read +Write rw-

7 Read + Write +Execute rwx

Let's see the chmod command in action.


764' absolute code Means :-

 Owner can read, write and execute


 Usergroup can read and write
 World can only read

Points to be Remember:-
 Linux being a multi-user system uses permissions and ownership for security.
 There are three user types on a Linux system viz. User, Group and All.
 Linux divides the file permissions into read, write and execute denoted by r,w, and x
 The permissions on a file can be changed by 'chmod' command which can be further divided
into Absolute and Symbolic mode
 The 'chown' command can change the ownership of a file/directory. Use the following commands:
chown user file or chown user:group file

Umask
umask command is used to set default file and folder permission in Linux operating system. File and
folder permissions are important because the permission enables or deny different users, groups and
others to access, read, write and execute of the given file.

Number Permission

4 Read

2 Write
1 Execute

Permission Values

We have learned the default user and permission usage for Linux operating systems. umask command
uses these users and permission a bit different. Actually, permission values are working a bit different.
We do a bitwise operation on the permission values in order to use with umask. For example, in order to
set 775 for default permission, we need to provide 002 to the umask command. This can create some
-S
problems and errors during umask command usage but we can check with option the regular
the permission values of the current files and folders.
$ umask

Set Permission
Now we will set the file and folder default permissions with the umask command. As learned previously we will
use umask type permissions. In this example, we will set the permission user read+write+execute , group
read+execute and others none.
$ umask 027

$ umask –S

umask Permission Digit Presentation

umask default file default directory


digit permissions permissions

0 Rw Rwx

1 Rw Rw

2 R Rx

3 R R

4 W Wx

5 W W

6 X X
Vi editor
The vi editor is elaborated as visual editor. It is installed in every Unix system. In other words,
it is available in all Linux system. It is user-friendly and works same on different platforms. It
is a very powerful application. An improved version of vi editor is vim.

The vi editor has two modes:

O Command Mode: In command mode, actions are taken on the file. The vi editor starts
in command mode. Here, the typed words will act as commands in vi editor. To pass a
command, you need to be in command mode.
O Insert Mode: In insert mode, entered text will be inserted into the file. The Esc key
will take you to the command mode from insert mode.

By default, the vi editor starts in command mode. To enter text, you have to be in insert
mode, just type 'i' and you'll be in insert mode. Although, after typing i nothing will
appear on the screen but you'll be in insert mode. Now you can type anything.

To exit from insert mode press Esc key.

Command mode:
 The vi editor opens in this mode, and it only understands commands
 In this mode, you can, move the cursor and cut, copy, paste the text
 This mode also saves the changes you have made to the file
 Commands are case sensitive. You should use the right letter case.

Insert mode:
 This mode is for inserting text in the file.
 You can switch to the Insert mode from the command mode by pressing 'i' on the keyboard
 Once you are in Insert mode, any key would be taken as an input for the file on which you
are currently working.
 To return to the command mode and save the changes you have made you need to press
the Esc key

Starting the vi editor


To launch the VI Editor -Open the Terminal (CLI) and type

vi <filename_NEW> or <filename_EXISTING>

And if you specify an existing file, then the editor would open it for you to edit. Else, you can create a new
file.
VI Editing commands
 i - Insert at cursor (goes into insert mode)
 a - Write after cursor (goes into insert mode)
 A - Write at the end of line (goes into insert mode)
 ESC - Terminate insert mode
 u - Undo last change
 U - Undo all changes to the entire line
 o - Open a new line (goes into insert mode)
 dd - Delete line
 3dd - Delete 3 lines.
 D - Delete contents of line after the cursor
 C - Delete contents of a line after the cursor and insert new text. Press ESC key to end insertion.
 dw - Delete word
 4dw - Delete 4 words
 cw - Change word
 x - Delete character at the cursor
 r - Replace character
 R - Overwrite characters from cursor onward
 s - Substitute one character under cursor continue to insert
 S - Substitute entire line and begin to insert at the beginning of the line

Saving and Closing the file


Shift+zz - Save the file and quit
:w - Save the file but keep it open
:q - Quit without saving
:wq - Save the file and quit
Vi Commands
Linux vi editor is different from other editors. You have to use
different keys to use different functions. Although, it's quite easy and
interesting to use vi editor.
The vi editor commands are case sensitive.
Have a look at the vi commands in the following table.

To switch from command to insert mode:

Command Action

I Start typing before the current character

I Start typing at the start of current line

A Start typing after the current character

A Start typing at the end of current line

O Start typing on a new line after the current line


O Start typing on a new line before the current line

Command to cut, copy and paste:

Commands Action

Dd Delete a line

Yy (yank yank) copy a line

P Paste after the current line

P Paste before the current line

Search a string:

Commands Action

/string Forward search for given string

?string Backward search for given string

/^string Forward search string at beginning of a line

/string$ Forward search string at end of a line

N Go to next occurrence of searched string

/\<he\> Search for the word he (and not for there, here, etc.)

/pl[abc]ce Search for place, plbce, and plcce

WORD COUNT(WC)
wc stands for word count. As the name implies, it is mainly used for counting purpose.
 It is used to find out number of lines, word count, byte and characters
count in the files specified in the file arguments.
 By default it displays four-columnar output.
 First column shows number of lines present in a file specified, second column
shows number of words present in the file, third column shows number of
characters present in file and fourth column itself is the file name which are
given as argument.
 Options:
1. -l: This option prints the number of lines present in a file. With this option wc
command displays two-columnar output, 1st column shows number of lines present
in a file and 2nd itself represent the file name.
With one file name
$ wc -l state.txt
5 state.txt

With more than one file name


$ wc -l state.txt capital.txt
5 state.txt
5 capital.txt
10 total
2. -w: This option prints the number of words present in a file. With this option wc
command displays two-columnar output, 1st column shows number of words
present in a file and 2nd is the file name.
With one file name
$ wc -w state.txt
7 state.txt

With more than one file name


$ wc -w state.txt capital.txt
7 state.txt
5 capital.txt
12 total
3. -c: This option displays count of bytes present in a file. With this option it
display two-columnar output, 1st column shows number of bytes present in a file
and 2nd is the file name.
With one file name
$ wc -c state.txt
63 state.txt

With more than one file name


$ wc -c state.txt capital.txt
63 state.txt
45 capital.txt
108 total
4. -m: Using -m option ‘wc’ command displays count of characters from a file.
With one file name
$ wc -m state.txt
63 state.txt

With more than one file name


$ wc -m state.txt capital.txt
63 state.txt
45 capital.txt
108 total

tee command
tee command reads the standard input and writes it to both the standard
output and one or more files. The command is named after the T-splitter
used in plumbing. It basically breaks the output of a program so that it can
be both displayed and saved in a file. It does both the tasks simultaneously,
copies the result into the specified files or variables and also display the
result.
Syntax:

1. cat or tac <fileName> | tee <newFile> | cat or tac |.....


2. tac weeks.txt | tee new.txt | cat
Basic Usage
The basic usage of tee it’s:

tee [ -a ] [ -i ] [ File
... ]
The 2 options of tee means:

 -a Appends the output to the end of File instead of writing over it.
-i Ignores interrupts.
 % the current file
After this command you’ll have saved successfully your file and can exit safely from the editor.

Options :
1.-a Option : It basically do not overwrite the file but append to the given file.
Suppose we have file1.txt
Input: geek
for
geeks
and file2.txt
Input:geeks
for
geeks
SYNTAX :
geek@HP:~$ wc -l file1.txt|tee -a file2.txt
OUTPUT :
3 file1.txt
$cat file2.txt
OUTPUT:
geeks
for
geeks
3 file1.txt
EXAMPLE
Application
Suppose we want to count number of characters in our file and also want to save the
output to new text file so to do both activities at same time, we use tee command.
geek@HP:~$ wc -l file1.txt| tee file2.txt
OUTPUT:
geek@HP:~$15 file1.txt
Here we have file1 with 15 characters, so the output will be 15 and the output will be
stored to file2. In order to check the output we use :
geek@HP:~$ cat file2.txt
OUTPUT:
geek@HP:~$15 file1.txt
Concept of Inodes
An Inode number is a uniquely existing number for all the files in Linux and all Unix type
systems. When a file is created on a system, a file name and Inode number is assigned to it.

Generally, to access a file, a user uses the file name but internally file name is first mapped with
respective Inode number stored in a table.

Inode doesn't contain the file name. Reason for this is to maintain hard-links for the files. When all the
other information is separated from the file name then only we can have various file names pointing to the
same Inode.

An Inode is a data structure containing metadata about the files.

o User ID of file
o Group ID of file
o Device ID
o File size
o Date of creation
o Permission
o Owner of the file
o File protection flag
o Link counter to determine number of hard links

inode number

Inode number is also known as index number. An inode is a unique number assigned to files and
directories while it is created. The inode number will be unique to entire filesystem.

An inode is a data structure on a traditional Unix-style file system such as ext3 or ext4. storing the
properties of a file and directories.

Linux extended filesystems such as ext3 or ext4 maintain an array of these inodes called the inode table.
This table contains list of all files in that filesystem. The individual inodes in inode table have a unique
number (unique to that filesystem) called the inode number.

The following information is stored in inode:

 File type: regular file, directory, pipe etc.


 Permissions to that file: read, write, execute
 Link count: The number of hard link relative to an inode
 User ID: owner of file
 Group ID: group owner
 Size of file: or major/minor number in case of some special files
 Time stamp: access time, modification time and (inode) change time
 Attributes: immutable' for example
 Access control list: permissions for special users/groups
 Link to location of file
 Other metadata about the file

Using stat command


Another method of viewing a file’s inode is to use the stat command. This
method is generally used against a single file, while the ls command is used
against a list of files.

The example will stat the html directory seen above.


 # stat hello
 File: ‘hello’
 Size: 66 Blocks: 8 IO Block: 4096 regular file
 Device: fd01h/64769d Inode: 530461 Links: 2
 Access: (0774/-rwxrwxr--) Uid: ( 0/ root) Gid: ( 0/ root)
 Access: 2017-05-15 20:12:32.540352591 +0000
 Modify: 2017-05-15 20:12:16.901527357 +0000
 Change: 2017-05-19 17:41:37.394470321 +0000
 Birth: -

Inode structure for directory


As stated above, the directories in Linux are also treated as files. Directory is special file that maps a file
name to its inode number (this mapping is called directory entry or dentry). So when we say that a
directory contains files and other directories, we mean that this directory is mapping those files and
directories (directories are special files, so they also need mapping to their inode numbers) to their inode
numbers.

As a file is mapped to its inode by its parent directory, then how is top most directory, (i.e. /
directory ) mapped to its inode? The inode number of/ directory is fixed, and is always 2.

# stat /
File: '/'
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 806h/2054d Inode: 2 Links: 27
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-05-20 01:40:01.565097799 +0100
Modify: 2017-05-20 01:27:33.651924301 +0100
Change: 2017-05-20 01:27:33.651924301 +0100
Birth: -

Links and index number in Linux


In the output of ls -l , the column following the permissions and before owner is the link count. Link
count is the number of hard links to a file. To understand hard links, we begin with links. A link is a pointer
to another file.

In Linux world, two types of links exist:

 Hard links. You can think a hard link as an additional name for an existing file.
Hard links are associating two or more file names with the same inode . You
can create one or more hard links for a single file. Hard links cannot be created
for directories and files on a different filesystem or partition.
 Soft links. A soft link is something like a shortcut in Windows. It is an indirect
pointer to a file or directory. Unlike a hard link, a symbolic link can point to a
file or a directory on a different filesystem or partition.

How to Use the ln Comm

a. Symbolic links (or soft links)


The symbolic link is a separate file whose contents points to the linked-to file. To create a symbolic
link, use the ln command with the option -s . When using the ln command, make sure that you first
refer to the name of the original file and then to the name of the link you want to create.
ln [OPTION]... [-T] TARGET LINK_NAME (1st form)
ln [OPTION]... TARGET... DIRECTORY (2nd form)
ln [OPTION]... -t DIRECTORY TARGET... (3rd form)
1ST Form

2nd Form
3rd Form

ln -s source_file symbolic_link

Replace source_file with the name of the existing file for which you want to create the
symbolic link and symbolic_link with the name of the symbolic link.

The symbolic_link parameter is optional. If you do not specify the symbolic link,
the ln command will create a new link in your current directory:

In the following example, we are creating a symbolic link named my_link.txt to a file
named my_file.txt:

To verify that the symlink was successfully created, use the ls command:

ls -l my_link.txt

The output will look something like this:

lrwxrwxrwx 1 linuxize users 4 Nov 2 23:03 my_link.txt -> my_file.txt


Copy
# ln -s /home/bobbin/sync.sh filesync

The l character is a file type flag that represents a symbolic link. The -> symbol shows
the file the symlink points to.

Here 'filesync' is a symbolic link to 'sync.sh'. Think about it as a shortcut. Editing 'filesync' is like directly
edit the original file but it's really what happen. If we delete or move the original file, the link will be broken
and our 'filesync' file will not be longer available.

The ls -l command shows that the resulting file is a symbolic link. This is indicated by the letter l in the
first position of the ls -l output and also by the arrow at the end of the listing, which indicates the file
the name is referring to.

# ls -l filesync
lrwxrwxrwx 1 root root 20 Apr 7 06:08 filesync -> /home/bobbin/sync.sh

The contents of a symbolic link are the name of target file only. You can see that the permissions on the
symbolic link are completely open. This is because the permissions are not managed\

When comparing the symbolic link and the original file, you will notice a clear difference between them.
# ls -il /home/bobbin/sync.sh filesync
258674 lrwxrwxrwx 1 root root 20 Apr 7 06:08 filesync -> /home/bobbin/sync.sh
517333 -rw-r----- 1 root root 5 Apr 7 06:09 /home/bobbin/sync.sh

The original file is just a name that is connected directly to the inode, and the symbolic link refers to the
name. The size of the symbolic link is the number of bytes in the name of the file it refers to, because no
other information is available in the symbolic link.

Removing Symlinks
To delete/remove symbolic links use either the unlink or rm command.

The syntax of the unlink is very simple:

Unlink symlink_to_remove
Copy

Removing a symbolic link using the rm command is the same as when removing a file:

rm symlink_to_remove
Copy

No matter which command you use, when removing a symbolic link not append the / trailing slash at the
end of its name.

If you delete or move the source file to a different location, the symbolic file will be left dangling
(broken) and should be removed.

Applications of Symbolic Links


 Symbolic links are most useful for avoiding complicated paths. Don’t want to have
to remember a path that is 73 characters long? No problem, just create a symbolic
link to remember where it is.
 Symbolic links make it easy to make some set of files appear to exist in multiple
locations without having to make separate copies.
 Hard links are most useful for keeping file content in a single location by avoiding
duplication of what might be a very large amount of data.
Example:1

b. Hard links
To get an idea of what a hard link is, it is important to understand that the identity of a file is its inode
number, not its name. A hard link is a name that references an inode. It means that if 'file1' has a hard link
named 'file2', then both of these files refer to same inode. So, when you create a hard link for a file, all
you really do is add a new name to an inode.

To do this, use the ln command without option.

# ls -l /home/bobbin/sync.sh
-rw-r----- 1 root root 5 Apr 7 06:09 /home/bobbin/sync.sh
# ln /home/bobbin/sync.sh synchro

Now let's compare the two files

# ls -il /home/bobbin/sync.sh synchro


517333 -rw-r----- 2 root root 5 Apr 7 06:09 /home/bobbin/sync.sh
517333 -rw-r----- 2 root root 5 Apr 7 06:09 synchro

The interesting thing about hard links is that there is no difference between the original file and
the link: they are just two names connected to the same inode.

As you must have noted, unlike soft links, hard links are no special files. Now, link count is the
number a file has been hard linked. So a link count increases after creating a hard link as you
can see in the above figure. These hard links have two limitations:

 The directories cannot be hard linked. Linux does not permit this to maintain the acyclic
tree structure of directories.
 A hard link cannot be created across filesystems. Both the files must be on the same
filesystems, because different filesystems have different independent inode tables (two
files on different filesystems, but with same inode number will be different).
Comparision Hard Links and Soft Links
Hard Links Soft Links
Each hard linked file is assigned the same Inode A soft link is similar to the file shortcut feature
value as the original, therefore they reference the which is used in Windows Operating systems.
same physical file location. Each soft linked file contains a separate Inode
Hard links more flexible and remain linked even if value that points to the original file. As similar
the original or linked files are moved throughout to hard links, any changes to the data in either
the file system, although hard links are unable to file is reflected in the other.
cross different file systems. Soft links can be linked across different file
systems, although if the original file is deleted
or moved, the soft linked file will not work
correctly (called hanging link).
ls -l command shows all the links with the link ls -l command shows all links with first column
column shows number of links. value l? and the link points to original file.
Links have actual file contents. Soft Link contains the path for original file and
If original file is removed then the link will still not the contents.
show the content of the file.
Removing any link, just reduces the link count, Removing soft link doesn’t affect anything but
but doesn’t affect other links. removing original file, the link becomes
“dangling” link which points to nonexistent
file.
 Even if we change the filename of the original file If we change the name of the original file then
then also the hard links properly work. all the soft links for that file become dangling
 We cannot create a hard link for a directory to avoid i.e. they are worthless now.
recursive loops. A soft link can link to a directory.

The size of any of the hard link file is same as the Size of a soft link is equal to the name of the
original file and if we change the content in any of file for which the soft link is created. E.g If
the hard links then size of all hard link files are name of file is file1 then size of it’s soft link
updated. will be 5 bytes which is equal to size of name
of original file.
The disadvantage of hard links is that it cannot be Link across file systems: If you want to link
created for files on different file systems and it files across the file systems, you can only use
cannot be created for special files or directories. symlinks/soft links.

Operations with files and its relation with inodes


Most of the operations (such as copy) performed on soft links will affect the actual linked file (with the
exception of rm or mv commands, which remove (or move) soft link itself)

Here are some file operations in which inodes play their vital role:

a. copy files
When we copy a file, a new file with a new inode is created.
# cp myfile ..
# ls -li myfile ../myfile
2501 -rw------- 1 raghu raghu 36 Jun 25 20:12 myfile
3746 -rw------- 1 raghu raghu 36 Jan 11 12:05 ../myfile

b. move files
When moving across file systems, mv command proceeds as cp command above, with the exception that
the original file is removed from its location. But when moving within a filesystem, the inode does not
change, only the directory mapping of the inode is changed, the actual data on the hard disk (contents of
the file) does not move.

# ls -li samplefile.txt
2497 -rw------- 1 raghu raghu 22 Jun 25 20:12 samplefile.txt

Now let's move the file and check the result

# mv samplefile.txt ..
# ls -li ../samplefile.txt
2497 -rw------- 1 raghu raghu 22 Jun 25 20:12 ../samplefile.txt

c. remove files

When rm command is issued, first it checks the link count of the file. If the link count is greater than 1
, then it removes that directory entry and decreases the link count. Still, data is present, nor is the
inode affected. And when link count is 1 , the inode is deleted from the inode table, inode number
becomes free, and the data blocks that this file was occupying are added to the free data block list.

# ls -li myfile myfile.hardlink


2501 -rw------- 2 raghu raghu 36 Jun 25 2012 myfile
2501 -rw------- 2 raghu raghu 36 Jun 25 2012 myfile.hardlink

OD command for OCYAL display

The od (stands for octal dump) command displays a file in octal (base 8) format by default. It is often used for
visualizing data that isn’t in a human-readable format, like the executable code of a program.

To display the file in hexadecimal format, use the -x option:


To display the file in decimal format, use the -i option:
To display the file in character format, use the -c option:

Internal and External Commands


The UNIX system is command-based i.e things happen because of the commands that you key in. All
UNIX commands are seldom more than four characters long.
They are grouped into two categories:
 Internal Commands : Commands which are built into the shell. For all the shell built-in
commands, execution of the same is fast in the sense that the shell doesn’t have to search the given
path for them in the PATH variable, and also no process needs to be spawned for executing it.
Examples: source, cd, fg, etc.
 External Commands : Commands which aren’t built into the shell. When an
external command has to be executed, the shell looks for its path given in the PATH
variable, and also a new process has to be spawned and the command gets
executed. They are usually located in /bin or /usr/bin. For example, when you
execute the “cat” command, which usually is at /usr/bin, the executable /usr/bin/cat
gets executed.
Examples: ls, cat etc.
If you know about UNIX commands, you must have heard about the ls command.
Since ls is a program or file having an independent existence in the /bin directory(or
/usr/bin), it is branded as an external command that actually means that the ls
command is not built into the shell and these are executables present in a separate
file. In simple words, when you will key in the ls command, to be executed it will be
found in /bin. Most commands are external in nature, but there are some which are not
really found anywhere, and some which are normally not executed even if they are in
one of the directories specified by PATH. For instance, take echo command:
$type echo
echo is a shell built in
echo isn’t an external command in the sense that, when you type echo, the shell won’t
look in its PATH to locate it(even if it is there in /bin). Rather, it will execute it from its
own set of built-in commands that are not stored as separate files. These built-in
commands, of which echo is a member, are known as internal commands.
You now might have noticed that it’s the shell that actually does all these works. This
program starts running as soon as the user log in and dies when the user logs out. The
shell is an external command with a difference, it possesses its own set of internal
commands. So, if a command exists both as an internal command of the shell as well
as external one(in /bin or /usr/bib), the shell will accord top priority to its own internal
command of the same name.

Getting the list of Internal Commands


If you are using bash shell you can get the list of shell built-in commands
with help command :
$help

// this will list all


the shell built-in commands //

How to find out whether a command is internal or external?


In addition to this you can also find out about a particular command i.e whether it is
internal or external with the help of type command :
$type cat
cat is /bin/cat

//specifying that cat is


external type//

$type cd
cd is a shell builtin

//specifying that cd is
internal type//

UNIQ Command
The uniq command in Linux is a command line utility that reports or filters out the
repeated lines in a file.
In simple words, uniq is the tool that helps to detect the adjacent duplicate lines and
also deletes the duplicate lines. uniq filters out the adjacent matching lines from the
input file(that is required as an argument) and writes the filtered data to the output file .
Syntax of uniq Command :
//...syntax of uniq...//
$uniq [OPTION] [INPUT[OUTPUT]]
The syntax of this is quite easy to understand. Here, INPUT refers to the input file in
which repeated lines need to be filtered out and if INPUT isn’t specified
then uniq reads from the standard input. OUTPUT refers to the output file in which you
can store the filtered output generated by uniq command and as in case of INPUT if
OUTPUT isn’t specified then uniq writes to the standard output.
Options For uniq Command:
1. -c – -count : It tells how many times a line was repeated by displaying a number as
a prefix with the line.
2. -d – -repeated : It only prints the repeated lines and not the lines which aren’t
repeated.
3. -D – -all-repeated[=METHOD] : It prints all duplicate lines and METHOD can be
any of the following:
 none : Do not delimit duplicate lines at all. This is the default.
 prepend : Insert a blank line before each set of duplicated lines.
 separate : Insert a blank line between each set of duplicated lines.
Sorting

You might also like