FILE MANIPULATION-PRACTICAL





Hello friends i hope you have read my previous article..


                      MANIPULATING THE FILE AND DIRECTORIES

and it is compulsory to understand the concepts of that article....
in this article we will be learning the practical of the linux commanding manipulation :- files and directories...
So Lets start...
Since we are going to do some real file manipulation, let's build a safe place to “play” with our file manipulation commands. First we need a directory to work in. We'll create one in our home directory and call it “game”.

                          Creating Directories

The mkdir command is used to create a directory. To create our game directory we will first make sure we are in our home directory and then we will create  a new directory:

command :


mkdir game



To make our playground a little more interesting, let's create a couple of directories inside game directory and name it as “dir1” and “dir2”. To do this, we will change our current working directory to "game" and execute another command mkdir:

commamd :
ubuntu@ubuntu:~$ cd game
ubuntu@ubuntu:~/game$ pwd
/home/ubuntu/game
ubuntu@ubuntu:~/game$ mkdir dir1
ubuntu@ubuntu:~/game$ mkdir dir2
ubuntu@ubuntu:~/game$ ls
dir1  dir2
ubuntu@ubuntu:~/game$ 



               or

ubuntu@ubuntu:~$ cd game
ubuntu@ubuntu:~/game$ pwd
/home/ubuntu/game
ubuntu@ubuntu:~/game$ mkdir dir1 dir2
ubuntu@ubuntu:~/game$ ls
dir1  dir2
ubuntu@ubuntu:~/game$ clear


Notice that the mkdir command will accept multiple arguments allowing us to create both directories with a single command.

                      Copying Files

Next, let's get some data into our playground. We'll do this by copying a file. Using the cp command, we'll copy the passwd file from the /etc directory to the current working directory:

command :

ubuntu@ubuntu:/etc$ cp /etc/passwd /home/ubuntu/game
ubuntu@ubuntu:/etc$ cd ~/game
ubuntu@ubuntu:~/game$ ls
dir1  dir2  passwd
ubuntu@ubuntu:~/game$ 



          or 
ubuntu@ubuntu:~$ cd game
ubuntu@ubuntu:~/game$ pwd
/home/ubuntu/game
ubuntu@ubuntu:~/game$ cp /etc/passwd .
ubuntu@ubuntu:~/game$ ls
dir1  dir2  passwd
ubuntu@ubuntu:~/game$ 

NOTE: in above example the main command is 


"cp <file name to copy> <destination where to copy>"


in first example you can understand very well that "/etc/passwd" is the file 
which have to copy to the directory or destination "/home/ubuntu/game" that is the destination file.

but in the second example first i have change directory to the destination that is "game" then i used command " cp /etc/passwd ." here '.' mean that "current working directory"...remember i already taught you about this in the navigation system  (relative pathname or absolute pathname)about command ".." and "." 

Notice how we used the shorthand for the current working directory, the single trailing period. So now if we perform an ls, we will see our file:

command :

ubuntu@ubuntu:~/game$ ls -l
total 12
drwxrwxr-x 2 ubuntu ubuntu 4096 Nov 11 05:21 dir1
drwxrwxr-x 2 ubuntu ubuntu 4096 Nov 11 05:21 dir2
-rw-r--r-- 1 ubuntu ubuntu 2737 Nov 11 05:36 passwd
ubuntu@ubuntu:~/game$ 


Now, just for fun, let's repeat the copy using the “-v” option (verbose) to see what it does:

command :

ubuntu@ubuntu:~/game$ ls
dir1  dir2  passwd
ubuntu@ubuntu:~/game$ cp -v /etc/passwd .
‘/etc/passwd’ -> ‘./passwd’
ubuntu@ubuntu:~/game$ ls
dir1  dir2  passwd
ubuntu@ubuntu:~/game$ 


The cp command performed the copy again, but this time displayed a concise message indicating what operation it was performing. Notice that cp overwrote the first copy without any warning.

to display the warning massage we have to use the '-i(interactive)'
with 'cp' command.

command :

ubuntu@ubuntu:~/game$ ls
dir1  dir2  passwd
ubuntu@ubuntu:~/game$ cp -i /etc/passwd .
cp: overwrite ‘./passwd’? y
ubuntu@ubuntu:~/game$ ls
dir1  dir2  passwd
ubuntu@ubuntu:~/game$ 

Responding to the prompt by entering a “y” will cause the file to be overwritten, any other character (for example, “n”) will cause cp to leave the file alone.

                            Moving And Renaming Files

Now, the name “passwd” doesn't seem very playful and this is a playground, so let's change it to something else:

ubuntu@ubuntu:~/game$ ls
dir1  dir2  passwd
ubuntu@ubuntu:~/game$ mv -i passwd fun
ubuntu@ubuntu:~/game$ ls
dir1  dir2  fun
ubuntu@ubuntu:~/game$ 


so here you can clearly see that the file name 'passwd' change to file name 'fun'.

let's make some other command ..play the game....

move the rename 'passwd' file 'fun' to another directory 'dir1'.

command :

ubuntu@ubuntu:~/game$ ls
dir1  dir2  fun
ubuntu@ubuntu:~/game$ mv fun dir1
ubuntu@ubuntu:~/game$ ls
dir1  dir2
ubuntu@ubuntu:~/game$ cd dir1
ubuntu@ubuntu:~/game/dir1$ ls
fun
ubuntu@ubuntu:~/game/dir1$ 


so , through this it is clear to you how it is moved from parent directory 'game' to the sub-directory'dir1'.

some other play..just watch it..carefully...

let's move 'fun' file from dir1 to dir2..watch carefully..i have used so many command here..

command:

ubuntu@ubuntu:~/game$ ls
dir1  dir2
ubuntu@ubuntu:~/game$ cd dir1
ubuntu@ubuntu:~/game/dir1$ ls
fun
ubuntu@ubuntu:~/game/dir1$ cd ..
ubuntu@ubuntu:~/game$ mv dir1/fun dir2
ubuntu@ubuntu:~/game$ ls
dir1  dir2
ubuntu@ubuntu:~/game$ cd dir2
ubuntu@ubuntu:~/game/dir2$ ls
fun
ubuntu@ubuntu:~/game/dir2$ cd ../dir1
ubuntu@ubuntu:~/game/dir1$ ls
ubuntu@ubuntu:~/game/dir1$ 




try to manipulate by yourself...if you have any doubt comment ...below

now let's again move 'fun' file to the main directory...

comment :

ubuntu@ubuntu:~/game$ ls
dir1  dir2
ubuntu@ubuntu:~/game$ mv dir2/fun .
ubuntu@ubuntu:~/game$ ls
dir1  dir2  fun
ubuntu@ubuntu:~/game$ cd dir2
ubuntu@ubuntu:~/game/dir2$ ls
ubuntu@ubuntu:~/game/dir2$ 


ok..ok..it was quiet easy to understood...let's play some other..game stuff..

1. move 'fun' file to directory 'dir1'
2.then move directory 'dor1' to directory 'dir2'.
3.now explore 'dir2' by command ls

watch carefully i have done all these step in the single time..try to understand the commands ..recall all previou learn commands..

command:

ubuntu@ubuntu:~/game$ ls
dir1  dir2  fun
ubuntu@ubuntu:~/game$ mv fun dir1
ubuntu@ubuntu:~/game$ ls
dir1  dir2
ubuntu@ubuntu:~/game$ mv dir1 dir2
ubuntu@ubuntu:~/game$ ls
dir2
ubuntu@ubuntu:~/game$ cd dir2
ubuntu@ubuntu:~/game/dir2$ ls
dir1
ubuntu@ubuntu:~/game/dir2$ cd dir1
ubuntu@ubuntu:~/game/dir2/dir1$ ls
fun
ubuntu@ubuntu:~/game/dir2/dir1$ 


so..in this play we just put every thing in the directory dir2...and file 'fun' in the dir1...
so now it is time to put all the thing as previous one ...i hope you got pretty good idea how to do this ..through command...but let do it together 'with me'..
ohh it recall you 'john snow'...leave it ..it only for the GOT lover's..ya off-course me too...

ok...let's do it..

command:

ubuntu@ubuntu:~/game$ ls
dir2
ubuntu@ubuntu:~/game$ mv dir2/dir1 .
ubuntu@ubuntu:~/game$ ls
dir1  dir2
ubuntu@ubuntu:~/game$ mv dir1/fun .
ubuntu@ubuntu:~/game$ ls
dir1  dir2  fun
ubuntu@ubuntu:~/game$ ohh..it's seem that every things are done..




so, you done it carefully..


let come to upgrade your-self.....
start new topic about how to make link through the CLI(command lne interface)..

but before we start this let have a look what is link..? why we need this..?what are the importance of this feature in the unix-linux based systems..?

actually..In most Unix-like systems it is possible to have a file referenced by multiple names.While the value of this may not be obvious, this is the link...and off-course it is really a useful feature in the unix system....

their is two type of 'link'...

1.soft-link or sys-link or Symbolic Links :

As we look around, we are likely to see a directory listing with an entry like this:

lrwxrwxrwx 1 root root 11 2007-08-11 07:34 libc.so.6 -> libc-2.6.so


look it ..if you dont get any thing then you have to keep patience...

          Picture this scenario: 

A program requires the use of a shared resource of some kind contained in a file named “foo,” but “foo” has frequent version changes. It would be good to include the version number in the filename so the administrator or other interested party could see what version of “foo” is installed. This presents a problem. If we change the name of the shared resource, we have to track down every program that might use it and change it to look for a new resource name every time a new version of the resource is installed. That doesn't sound like fun at all.

so what is the solution of this big problem...actually this is an big bug for an world strongest operating system..linux

Here is where symbolic links save the day. Let's say we install version 2.6 of “foo,” which has the filename “foo-2.6” and then create a symbolic link simply called “foo” that points to “foo-2.6.” This means that when a program opens the file “foo”, it is actually opening the file “foo-2.6”. Now everybody is happy. The programs that rely on “foo” can find it and we can still see what actual version is installed. When it is time to upgrade to “foo-2.7,” we just add the file to our system, delete the symbolic link “foo” and create a
new one that points to the new version. Not only does this solve the problem of the version upgrade, but it also allows us to keep both versions on our machine. Imagine that “foo-2.7” has a bug (damn those developers!) and we need to revert to the old version. Again, we just delete the symbolic link pointing to the new version and create a new symbolic link pointing to the old version.

the directory listing above shows a symbolic link called “libc.so.6” that points to a shared library file called “libc-2.6.so.” This means that programs looking for “libc.so.6” will actually get the file “libc-2.6.so.”

ok that's it i feel diggy-biggy by explainig this lot of theory let's see it face to fece...
after that you wil understood very well...

                             Creating Hard Links

Now we'll try some links. First the hard links. We’ll create some links to our data file like so:

see here is an example ..in this command..how to make link of 'fun' document in all directory..'game' ,'dir1' ,'dir2'..

command :

ubuntu@ubuntu:~$ cd game
ubuntu@ubuntu:~/game$ ln fun funhard
ubuntu@ubuntu:~/game$ ld
ld: no input files
ubuntu@ubuntu:~/game$ ls
dir1  dir2  fun  funhard
ubuntu@ubuntu:~/game$ less funhard
ubuntu@ubuntu:~/game$ less fun
ubuntu@ubuntu:~/game$ ln fun dir1/funhard
ubuntu@ubuntu:~/game$ cd dir1
ubuntu@ubuntu:~/game/dir1$ ls
funhard
ubuntu@ubuntu:~/game/dir1$ cd ../
ubuntu@ubuntu:~/game$ ln fun dir2/funhard
ubuntu@ubuntu:~/game$ cd dir2
ubuntu@ubuntu:~/game/dir2$ ls
funhard
ubuntu@ubuntu:~/game/dir2$ cd ..
ubuntu@ubuntu:~/game$ ls
dir1  dir2  fun  funhard
ubuntu@ubuntu:~/game$ 


NOTICE :  So now we have four instances of the file “fun”. Let's take a look our playground directory:

ubuntu@ubuntu:~/game$ ls -l
total 16
drwxrwxr-x 2 ubuntu ubuntu 4096 Nov 11 08:04 dir1
drwxrwxr-x 2 ubuntu ubuntu 4096 Nov 11 08:05 dir2
-rw-r--r-- 4 ubuntu ubuntu 2737 Nov 11 05:48 fun
-rw-r--r-- 4 ubuntu ubuntu 2737 Nov 11 05:48 funhard
ubuntu@ubuntu:~/game$ 


One thing you will notice is that the second field in the listing for fun and fun-hard both contain a “4” which is the number of hard links that now exist for the file. You'll remember that a file will aways have at least one link because the file's name is created by a link. So, how do we know that fun and fun-hard are, in fact, the same file? In this case, ls is not very helpful. While we can see that fun and fun-hard are both the same size (field 5(2737 byte)), our listing provides no way to be sure. To solve this problem, we're
going to have to dig a little deeper. 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.

The ls command has a way to reveal this information. It is invoked with the “-i” option:
just try to differentiate ..both file..in below commands perform..

command :

ubuntu@ubuntu:~/game$ ls -li
total 16
29393001 drwxrwxr-x 2 ubuntu ubuntu 4096 Nov 11 08:04 dir1
29393002 drwxrwxr-x 2 ubuntu ubuntu 4096 Nov 11 08:05 dir2
29389569 -rw-r--r-- 4 ubuntu ubuntu 2737 Nov 11 05:48 fun
29389569 -rw-r--r-- 4 ubuntu ubuntu 2737 Nov 11 05:48 funhard
ubuntu@ubuntu:~/game$ ls -l
total 16
drwxrwxr-x 2 ubuntu ubuntu 4096 Nov 11 08:04 dir1
drwxrwxr-x 2 ubuntu ubuntu 4096 Nov 11 08:05 dir2
-rw-r--r-- 4 ubuntu ubuntu 2737 Nov 11 05:48 fun
-rw-r--r-- 4 ubuntu ubuntu 2737 Nov 11 05:48 funhard
ubuntu@ubuntu:~/game$ 


you see that in the first command "ls -li" which reaveal the number called 'ionide'..both of these number are similar tha's mean both file contain the same document.

                     Creating Symbolic Links

Symbolic links were created to overcome the two disadvantages of hard links: Hard links cannot span physical devices and hard links cannot reference directories, only files. Symbolic links are a special type of file that contains a text pointer to the target file or directory. Creating symbolic links is similar to creating hard links:

command:

ubuntu@ubuntu:~/game$ ls
dir1  dir2  fun  funhard
ubuntu@ubuntu:~/game$ ln -s fun fun-sym
ubuntu@ubuntu:~/game$ ls -l
total 16
drwxrwxr-x 2 ubuntu ubuntu 4096 Nov 11 08:04 dir1
drwxrwxr-x 2 ubuntu ubuntu 4096 Nov 11 08:05 dir2
-rw-r--r-- 4 ubuntu ubuntu 2737 Nov 11 05:48 fun
-rw-r--r-- 4 ubuntu ubuntu 2737 Nov 11 05:48 funhard
lrwxrwxrwx 1 ubuntu ubuntu    3 Nov 11 08:26 fun-sym -> fun
ubuntu@ubuntu:~/game$ 


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:

ubuntu@ubuntu:~$ cd game
ubuntu@ubuntu:~/game$ ls -l dir1
total 4
-rw-r--r-- 4 ubuntu ubuntu 2737 Nov 11 05:48 funhard
lrwxrwxrwx 1 ubuntu ubuntu    6 Nov 11 08:28 fun-sym -> ../fun
ubuntu@ubuntu:~/game$ 




The listing for fun-sym in dir1 shows that it is a symbolic link by the leading “l” in the first field and that it points to “../fun”, which is correct. Relative to the location of fun-sym, fun is in the directory above it. Notice too, that the length of the symbolic link file is 6, the number of characters in the string “../fun” rather than the length of the file to which it is pointing. 

When creating symbolic links, you can either use absolute pathnames:

ln -s /home/me/playground/fun dir1/fun-sym

or relative pathnames, as we did in our earlier example. Using relative pathnames is more desirable because it allows a directory containing symbolic links to be renamed and/or moved without breaking the links.

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

commands :

ubuntu@ubuntu:~/game$ ls
dir1  dir2  fun  funhard  fun-sym
ubuntu@ubuntu:~/game$ ln -s dir1 dir1-sym
ubuntu@ubuntu:~/game$ ls
dir1  dir1-sym  dir2  fun  funhard  fun-sym
ubuntu@ubuntu:~/game$ ls -l
total 16
drwxrwxr-x 2 ubuntu ubuntu 4096 Nov 11 13:51 dir1
lrwxrwxrwx 1 ubuntu ubuntu    4 Nov 11 13:52 dir1-sym -> dir1
drwxrwxr-x 2 ubuntu ubuntu 4096 Nov 11 08:30 dir2
-rw-r--r-- 4 ubuntu ubuntu 2737 Nov 11 05:48 fun
-rw-r--r-- 4 ubuntu ubuntu 2737 Nov 11 05:48 funhard
lrwxrwxrwx 1 ubuntu ubuntu    3 Nov 11 08:26 fun-sym -> fun
ubuntu@ubuntu:~/game$ 



                      
                           Removing Files And Directories

As we covered earlier, the rm command is used to delete files and directories. We are going to use it to clean up our playground a little bit. First, let's delete one of our hard links:

command :

ubuntu@ubuntu:~/game$ ls
dir1  dir1-sym  dir2  fun  funhard  fun-sym
ubuntu@ubuntu:~/game$ rm funhard
ubuntu@ubuntu:~/game$ ls
dir1  dir1-sym  dir2  fun  fun-sym
ubuntu@ubuntu:~/game$ 
ubuntu@ubuntu:~/game$ ls -l
total 12
drwxrwxr-x 2 ubuntu ubuntu 4096 Nov 11 13:51 dir1
lrwxrwxrwx 1 ubuntu ubuntu    4 Nov 11 13:52 dir1-sym -> dir1
drwxrwxr-x 2 ubuntu ubuntu 4096 Nov 11 08:30 dir2
-rw-r--r-- 3 ubuntu ubuntu 2737 Nov 11 05:48 fun
lrwxrwxrwx 1 ubuntu ubuntu    3 Nov 11 08:26 fun-sym -> fun




That worked as expected. The file fun-hard is gone and the link count shown for fun is reduced from four to three, as indicated in the second field of the directory listing. Next, we'll delete the file fun, and just for enjoyment, we'll include the “-i” option to
show what that does:

commmand :

ubuntu@ubuntu:~/game$ ls
dir1  dir1-sym  dir2  fun  fun-sym
ubuntu@ubuntu:~/game$ rm -i fun
rm: remove regular file ‘fun’? y
ubuntu@ubuntu:~/game$ ls -l
total 8
drwxrwxr-x 2 ubuntu ubuntu 4096 Nov 11 13:51 dir1
lrwxrwxrwx 1 ubuntu ubuntu    4 Nov 11 13:52 dir1-sym -> dir1
drwxrwxr-x 2 ubuntu ubuntu 4096 Nov 11 08:30 dir2
lrwxrwxrwx 1 ubuntu ubuntu    3 Nov 11 08:26 fun-sym -> fun
ubuntu@ubuntu:~/game$


Most Linux distributions configure ls to display broken links. On a UBUNTU, broken links are displayed in blinking red text! The presence of a broken link is not.

Let's clean up a little. We'll delete the symbolic links:
remove dir1-sym and fun-sym from the game directory..by using the -i(intractive command)...

command :

ubuntu@ubuntu:~/game$ ls
dir1  dir1-sym  dir2  fun-sym
ubuntu@ubuntu:~/game$ rm -i dir1-sym fun-sym
rm: remove symbolic link ‘dir1-sym’? y
rm: remove symbolic link ‘fun-sym’? y
ubuntu@ubuntu:~/game$ ls -l
total 8
drwxrwxr-x 2 ubuntu ubuntu 4096 Nov 11 13:51 dir1
drwxrwxr-x 2 ubuntu ubuntu 4096 Nov 11 08:30 dir2
ubuntu@ubuntu:~/game$ 


NOTE :

One thing to remember about symbolic links is that most file operations are carried out on the link's target, not the link itself. rm is an exception. When you delete a link, it is the link that is deleted, not the target.

Finally, we will remove our playground directory "game". To do this, we will return to our home directory and use rm with the recursive option (-r) to delete playground and all of its contents, including its subdirectories:

command :

ubuntu@ubuntu:~/game$ pwd
/home/ubuntu/game
ubuntu@ubuntu:~/game$ cd ..
ubuntu@ubuntu:~$ pwd
/home/ubuntu
ubuntu@ubuntu:~$ rm -r game
ubuntu@ubuntu:~$ cd game
bash: cd: game: No such file or directory
ubuntu@ubuntu:~$ 


now you can see that after removing the game directory ...command 'cd game '..will show an error massage that "No such file or directory"..
so finally we all delete the directory what we was make for our practicle purpose..i hope you get this very well through this example...

off-course it was quiet long to explain ...but to sum-up in brief i will upload an video for it....but to understand that very well you have to read all this theory very carefully...

ohh..today we learn a lot..that's it for today in this article...for any confusion or any query feel free to contact me through the comment box or you can join me in hacker-heaven facebook page..or you can directly contact me in facebook..you can find my facebook profile in this page...

read my new article...

              MANIPULATING THE COMMANDS

have nice day  ..and thank you for reading this article..

until next article this is..


                          ZEROCOOL
                          SIGN OUT

Popular Posts