working with directory



Working with Directory


In Linux, everything is a file.
So what is a directory?

A directory is one (of many) type of special file.

You cannot have a directory and a file of the same name (regardless of extension), just like you cannot have two files of the same name.

A directory is just a file, too.

The difference is that a directory can contain other files. It does not matter how it is called, it can have an extension (or two). What makes it a directory is that it was created as such.

Directory doesn't contain data. Instead, it contains pointers to all of the files that are contained within the directory.

Before working with directories, let’s see some of the basic command which will guide you to read manual and get help.

1.     Help – get help

Purpose

This command is defined internally and is used to get help.


And you can use this command whenever you will need any help about any command. We will see how we can get help in this chapter only.




2.     pwd -  print working directory

Purpose

This command prints present working directory .i.e. current working directory.


3.     cd – change directory

Purpose

You can change your current directory with the cd command


Some Shortcuts:


·         cd ~


Purpose

The cd is also a shortcut to get back into your home directory. Just typing cd without a target
directory, will put you in your home directory. Typing cd ~ has the same effect.


·         cd -


Purpose

Another useful shortcut with cd is to just type cd - to go to the previous directory.




·         cd..


Purpose

To go to the parent directory (the one just above your current directory in the directory
tree), type cd ..



Note: To stay in the current directory, type cd .



Path


We should understand about path concept first in order to work with directories. There are 2 paths: -
·Absolute path
·Relative path

Absolute path

Alternatively referred to as the file path and full path, the absolute path contains the root directory and all other subdirectories in which a file or folder is contained.

Some examples of absolute path:

/var/ftp/pub
/etc/samba.smb.conf
/boot/grub/grub.conf

Relative path


A relative path is a way to specify the location of a directory relative to another directory.  Itis defined as path related to the present working directory(pwd).

When you type a path starting with a slash (/), then the root of the file tree is assumed. It is considered as absolute path. If you don't start your path with a slash, then the current directory is the assumed starting point.



The screenshot below first shows the current directory /home/ravi. From within this
directory, you have to type cd /home instead of cd home to go to the /home directory.


When inside /home, you have to type cd ravi instead of cd  /ravi to enter the subdirectory
paul of the current directory /home.


Path completion


The tab key can help you in typing a path without errors.

For example:

Typing cd /et followed by the tab key will expand the command line to cd /etc/.

When typing cd /Et followed by the tab key, nothing will happen because you typed the wrong path (upper case E).



Try it yourself.







4.     man – Manual page


Purpose

 It shows the manual page for command.




We will learn how to see manual page in later of this chapter.

5.     ls

Purpose

You can list the contents of a directory with ls.


Now see the manual page for ls


you will get a manual page screen. I am sharing a part of this.

Here we will discuss some of the frequently used options in ls

·        ls –a


Purpose

It is used to show all files. Showing all files means including the hidden files.

Note: When a file name on a Linux file system starts with a dot, it is considered a hidden file and      
           it doesn't show up in regular file listings.


·         ls -l


Purpose

Typing ls -l (that is a letter L, not the number 1) gives you a long listing.








·         ls -h


Purpose

It shows the numbers (file sizes) in a more human readable format. You can use different combination as well. E.g. lh,hl, -h –l, -h.



Text Box: Type and notice the output of the above command.
 



Below i am showing you an example:


We will explain the details of the output later in this book.

6.     mkdir – make directory


Purpose

It creates a directory only if it does not already exist.


Text Box: man mkdir
 
Here is the manual page of mkdir. Have a look at this by yourself by using                     command.

Now make a directory


Screenshot_3.png


And if you want to create a sub directory whose parent directory does not exists.




To solve this we have to use -p option in mkdir







Think before you type a leading / .




7.     rmdir: remove directory

Purpose

When a directory is empty, you can use rmdir to remove the directory.

Here is the manual page of mkdir. Have a look at this by yourself.



Now try to remove directory which you have created using mkdir.



In first command, we have used ls command to list all the directory in our working directory. In second command, we have tried to delete test-mkdir but we can not do that because that directory is not empty. And using the third command we have successfully deleted test directory which is empty.

you want to delete test-mkdir along with test which is in test-mkdir then you have to use -p option as it recursively delete directories.



Note:  'rmdir -p a/b/c' is similar to 'rmdir a/b/c a/b a'


Comments

Related Posts Plugin for WordPress, Blogger...