How to Navigate in Linux/Unix:
Navigation is important in any Operating System whether it is Windows , Mac or Linux. To understand the concept of Navigation , first we have to understand the concept of Relative and Absolute path.
Relative Path :
To be simple Relative path are all those path which doesn't start with / (root ). Relative path is used when we want to access file or directory which is present in current directory from current directory.
Example
cd program
This command means go to program directory which is already present in current directory.
$ pwd
/home/sandeep
$ cd Desktop
$ pwd
/home/sandeep/Desktop
$
/home/sandeep
$ cd Desktop
$ pwd
/home/sandeep/Desktop
$
Absolute Path:
what if we want open a file which is in /etc directory and we are in /home directory? Then we have to use Absolute path . Absolute path are all those path which start with /(root ). Absolute path is used when we want to access file or directory which is not present in current directory from current directory.
Example
Lets say we want to open a hosts file which is present in /etc directory from /home directory.
cat /etc/hosts
$ pwd
/home/sandeep
$ cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 sandeep-desktop
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
$ pwd
/home/sandeep
$
/home/sandeep
$ cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 sandeep-desktop
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
$ pwd
/home/sandeep
$
* Notice that we have used Absolute path as it started from /.
Some symbols which is useful during Navigation
1. /: It represent root directory or top most directory of directory structure.
2. ~ : It represent HOME directory of current user. It is Equivalent to /home
3. .(dot) : represent current directory.
4. ..(dot dot) : represent parent directory.
* if ~sandeep is used then it represent HOME directory of user sandeep. It is equivalent to /home/sandeep.
-> cd (change directory or change current directory ) is used to navigate in Linux/Unix.
keep in mind Relative and Absolute path , lets do some example
1. We want to go to Desktop directory and we are in /home directory.
$ pwd
/home/sandeep
$ cd Desktop
$ ls
google-chrome.desktop opera-browser.desktop
linux-dir-tree1.jpg(linuxconfig.org) prada.jpg
myphoto.xpm.gz Sunset_city_wallpaper_by_Methevas.jpg
new file tron_legacy
$
/home/sandeep
$ cd Desktop
$ ls
google-chrome.desktop opera-browser.desktop
linux-dir-tree1.jpg(linuxconfig.org) prada.jpg
myphoto.xpm.gz Sunset_city_wallpaper_by_Methevas.jpg
new file tron_legacy
$
2. Now we want to go to parent directory of Desktop , how can we achieve that ? you guess it right we have to do
cd ..
$ pwd
/home/sandeep/Desktop
$ cd ..
$ pwd
/home/sandeep
$
/home/sandeep/Desktop
$ cd ..
$ pwd
/home/sandeep
$
*if we write cd . . You will see nothing different , because .(dot) is for current directory so it simply change to current directory .
$ pwd
/home/sandeep
$ cd .
$ pwd
/home/sandeep
$
/home/sandeep
$ cd .
$ pwd
/home/sandeep
$
3. Lets see the usage of Absolute path .
a. we want to go to the HOME directory of user user1 , we can do it in two ways
--> cd /home/user1
--> cd ~user1
$ cd /home/user1
$ pwd
/home/user1
$ cd
$ pwd
/home/sandeep
$ cd ~user1
$ pwd
/home/user1
$
$ pwd
/home/user1
$ cd
$ pwd
/home/sandeep
$ cd ~user1
$ pwd
/home/user1
$
* cd (without any path) is used to move to HOME directory of login user ,no matter in which directory user is.
4. we can do something like that also cd ../.. to move two level up.
$ pwd
/home/sandeep
$ cd Desktop
$ cd tron_legacy
$ pwd
/home/sandeep/Desktop/tron_legacy
$ cd ../..
$ pwd
/home/sandeep
$
/home/sandeep
$ cd Desktop
$ cd tron_legacy
$ pwd
/home/sandeep/Desktop/tron_legacy
$ cd ../..
$ pwd
/home/sandeep
$
Tricks you can use with cd command
1. cd * : This command works if first file is directory in your current directory.
$ ls
abc.lst def.lst examples.desktop js Pictures sandeep2.lst Videos
$ cd *
bash: cd: abc.lst: Not a directory
abc.lst def.lst examples.desktop js Pictures sandeep2.lst Videos
$ cd *
bash: cd: abc.lst: Not a directory
*In the above example first file is a normal file abc.lst , so cd * command does not work here.
$ mkdir abc
$ ls
abc def.lst file1 Music sandeep Videos
$ cd *
$ pwd
/home/sandeep/abc
$ ls
abc def.lst file1 Music sandeep Videos
$ cd *
$ pwd
/home/sandeep/abc
*In above example we have created directory abc , now this is the first file in the current directory , so if we write cd * then we move to abc directory.
2.cd - : To move to the last directory you were in.
$ pwd
/home/sandeep
$ cd /
$ pwd
/
$ cd -
/home/sandeep
If You Liked This Post Please Take a Time To Share This Post
0 comments:
Post a Comment