Quantcast
Channel: Linux Candy » homepost
Viewing all articles
Browse latest Browse all 10

Shelling out some $hell $cripts! Part 3

$
0
0

Welcome back, guys ‘n’ gals! This is the third part of the shell scripting series at LinuxCandy, and I hope this part will be just as useful to you as the last one!

Today, we will learn about terminal based directory navigation. This is a small and relatively simple aspect, but nonetheless very useful.
:)

In UNIX/Linux, MAC, BSD and many other Operating Systems, you will find a directory structure similar to the one shown below:

This hierarchical structure is more or less the most common structure. The root directory, or “/” (pronounced as ‘slash’) acts as the main parent for all other directories. External filesystems are “mounted” to some sub directory of “/”, such as /mnt, or /media.

Let us now get to some basics about navigation via the terminal.

Accessing a directory

For the sake of understanding, the examples I have used in the rest of the post use the following structure. You can refer to it for clarity

Imagine that you want to run a shell script via the terminal. You can do this in 2 ways:

  • Using an Absolute Path:
    To access a directory, we can use the cd (change directory) command, and provide the full path starting from the root, i.e, from “/” .
    For example, lets say that we need to access a directory called “candy”  which is inside your home directory. Lets say that your username is “sid” (:D)
    If you want to access the directory “candy” using absolute path, you need to provide the full path to the directory, like so:
    cd /home/sid/candy
    This command works irrespective of the present working directory, but is lengthy, and sometimes boring to navigate through. However, here’s a small tip for you : using
    key wile typing out your pathnames can help lessen the keystrokes. Its like predictive text- Based on the starting keywords you typed, and based on the names of the directories present in the current directory, the shell will fill out the rest of the name for you.
    For example, if we have only “cedar” and “candy” in the home directory, typing :
    cd /home/sid/ca

    will automatically result in:
    cd /home/sid/candy/
  • Using a Relative Path:
    Relative  pathnames are dependent upon the present working directory. Here, the navigation happens from the present working directory, and not from the root directory. Lets have an example.
    If our present working directory is /home/sid/candy. and we need to navigate into “bin” which is inside the directory called “candy”, we can simply use cd like so:
    cd bin
    Here, note that we have not used “/” before bin. if the directory change is happening within the present working directory, adding slash in the beginning will mislead the shell into thinking that the cd command is using absolute pathname.
    What about navigating to other directories, and upwards?
    Let us say that our present working directory is /home/sid/candy and we need to switch to /home/sid/cedar
    We can use relative path method to switch to it, by typing:
    cd ../cedar
    Here, the two dots before the slash, ask the shell to go up one directory level. Similarly, using more double-dots, we can go up more levels at once. For example, we could navigate from /home/sid/candy to /usr/local/share, by typing:
    cd ../../../usr/local/share/
    Note that you can use the
    key for auto completing names of directories or files here as well.
Some other useful things: Shell Environment Variables

Apart from the variables you will create in your shell scripts, there are some predefined variables present in the shell. These variables hold information that can be very useful, when the shell script needs to access data without knowing complete information. You will learn of this in coming posts. You can find out the contents of a shell environment variable, just like any script variable: using the echo command:
echo $VAR 
Replace VAR with any environment variable. Some are given below. Here the $ symbol acts like a pointer – It points the shell to the data held by the variable VAR. Environment variables are usually i n capitals. The most common Environment Variables are:

  • PATH: This variable holds the different paths to the directories that the shell will check when you attempt to run a program, without specifying its location. It usually looks something like this:
    echo $PATH
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
  • USER: This variable holds the user name of the currently logged in user
  • HOME: This points to the home directory of the current user
  • SHELL: This shows the current running shell – usually Bash, in GNU/Linux
  • LOGNAME : same as USER

Note that you can use these environment variables with cd as well!
cd $HOME
cd ~
cd  
All these commands do the same thing : change directory to the current user’s home directory.

Thats all for now. More to come!

Pic Courtesy: UNIX DIR structure


Viewing all articles
Browse latest Browse all 10

Trending Articles