Friday, July 5, 2013

Copy/Make/Move/Remove/Create Links-2 in Ubuntu/Linux[Learn ubuntu/Linux with me -Day 2

This is the continuation of my previous post

Click on the images for a better enlarged view.

How to remove files and directories from Terminal?

rm <file_name>
The above command deletes the file.

rm -r <dir_name>

The above command removes the directory.

I said i'll be telling you about links...remember hard & Symbolic links

first let's see the syntax for creating Links.

Before running down to the theory which i copied from a pdf i suggest you to go to these links,because you can get short and sweet answers.




Hard Links:

Hard links are the original Unix way of creating links, compared to symbolic links, which
are more modern. By default, every file has a single hard link that gives the file its name.
When we create a hard link, we create an additional directory entry for a file. Hard links
have two important limitations:
1. A hard link cannot reference a file outside its own file system. This means a link
may not reference a file that is not on the same disk partition as the link itself.
2. A hard link may not reference a directory.
A hard link is indistinguishable from the file itself. Unlike a symbolic link, when you list
a directory containing a hard link you will see no special indication of the link. When a
hard link is deleted, the link is removed but the contents of the file itself continue to exist
(that is, its space is not deallocated) until all links to the file are deleted.
It is important to be aware of hard links because you might encounter them from time to
time, but modern practice prefers symbolic links, which we will cover next.

Symbolic Links:

Symbolic links were created to overcome the limitations of hard links. Symbolic links
work by creating a special type of file that contains a text pointer to the referenced file or
directory. In this regard, they operate in much the same way as a Windows shortcut
though of course, they predate the Windows feature by many years ;-)
A file pointed to by a symbolic link, and the symbolic link itself are largely
indistinguishable from one another. For example, if you write some something to the
symbolic link, the referenced file is also written to. However when you delete a symbolic
link, only the link is deleted, not the file itself. If the file is deleted before the symbolic
link, the link will continue to exist, but will point to nothing. In this case, the link is said
to be broken. In many implementations, the ls command will display broken links in a
distinguishing color, such as red, to reveal their presence.
The concept of links can seem very confusing, but hang in there. We're going to try all
this stuff and it will, hopefully, become clear.

Example for Hard Links:

When thinking about hard links, it is helpful to imagine that files are made up of two
parts: the data part containing the file's contents and the name part which holds the file's
name. When we create hard links, we are actually creating additional name parts that all
refer to the same data part. The system assigns a chain of disk blocks to what is called an
inode, which is then associated with the name part. Each hard link therefore refers to a
specific inode containing the file's contents

On my desktop I've got a directory named ajay in which there are two directories named dir1 ,dir2 and a passwd file.View the above image in full screen.I've created hard links.
ls -l will give the number 4 to both passwd and passwd-hard file but to check wether they are different files or links we can use ls -li which gives us inode number.This number is same as you can see in the output.

Example for Symbolic Link:

The first example is pretty straightforward, we simply add the “-s” option to create a
symbolic link rather than a hard link. But what about the next two? Remember, when we
create a symbolic link, we are creating a text description of where the target file is
relative to the symbolic link. It's easier to see if we look at the ls output:
The listing for passwd-sym in dir1 shows that is it a symbolic link by the leading “l” in
the first field and that it points to “../passwd”, which is correct. Relative to the location of passwd-sym, passwd is in the directory above it. Notice too, that the length of the symbolic
link file is 9, the number of characters in the string “../passwd” rather than the length of the file to which it is pointing.
When creating symbolic links, you can either use absolute pathnames or relative path names.

In addition to regular files, symbolic links can also reference directories.

Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.

No comments:

Post a Comment