Wednesday 5 October 2011

Change Directory and pwd

% cd <directory name>: Changes the current working directory to the specified directory.
Example:
jp@ubuntu:~/demo$ pwd (pwd=>Print working directory)
/home/jp/demo
jp@ubuntu:~/demo$ ls
dir1  dir2  dir3  dir4  dir5
jp@ubuntu:~/demo$ cd dir1
jp@ubuntu:~/demo/dir1$ ls
jp@ubuntu:~/demo/dir1$ pwd
/home/jp/demo/dir1
jp@ubuntu:~/demo/dir1$

% pwd: Print name of current/ working directory.
jp@ubuntu:~/demo/dir1$ pwd
/home/jp/demo/dir1

% cd . : It will make you to stay in the current working directory.  (.) means current working directory and (..) means previous directory.
Example:
jp@ubuntu:~/demo/dir1$ pwd
/home/jp/demo/dir1
jp@ubuntu:~/demo/dir1$ ls
jp@ubuntu:~/demo/dir1$ cd .
jp@ubuntu:~/demo/dir1$ pwd
/home/jp/demo/dir1

% cd .. : It will take you to your home directory.
Example:
jp@ubuntu:~/demo/dir1$ pwd
/home/jp/demo/dir1
jp@ubuntu:~/demo/dir1$ ls
jp@ubuntu:~/demo/dir1$ cd ../
jp@ubuntu:~/demo$ pwd
/home/jp/demo
jp@ubuntu:~/demo$ ls
dir1  dir2  dir3  dir4  dir5
jp@ubuntu:~/demo$

% cd ~: Changes your current working directory to home directory.
Example:
jp@ubuntu:~/demo$ pwd
/home/jp/demo
jp@ubuntu:~/demo$ ls
dir1  dir2  dir3  dir4  dir5
jp@ubuntu:~/demo$ cd ~
jp@ubuntu:~$ pwd
/home/jp
jp@ubuntu:~$ ls
cpgms  demo  Desktop

% cd : Changes your current working directory to home directory.
Example:
jp@ubuntu:~/demo/dir1$ pwd
/home/jp/demo/dir1
jp@ubuntu:~/demo/dir1$ ls
jp@ubuntu:~/demo/dir1$ cd
jp@ubuntu:~$ pwd
/home/jp
jp@ubuntu:~$ ls
cpgms  demo  Desktop

% cd / : Changes your current working directory to root directory.
Example:
jp@ubuntu:~$ pwd
/home/jp
jp@ubuntu:~$ cd /
jp@ubuntu:/$ pwd
/
jp@ubuntu:/$

Make directories

% mkdir <directory name>:  used to create a new directory.
Example:
jp@ubuntu:~/demo$ mkdir dir1
jp@ubuntu:~/demo$ ls
dir1

% mkdir -p <dir name>: Creates all directories leading up to the given directory that do not exist previously.  No error if existing.
Example:
jp@ubuntu:~/demo$ pwd
/home/jp/demo
jp@ubuntu:~/demo$ ls
dir1
jp@ubuntu:~/demo$ mkdir -p dir2/sub1/sub2/
jp@ubuntu:~/demo$ ls
dir1  dir2
jp@ubuntu:~/demo$ ls -R
.:
dir1  dir2
./dir1:
./dir2:
sub1
./dir2/sub1:
sub2
./dir2/sub1/sub2:

% mkdir -v <dir name>:  Creates  directory and prints message for each created directory.
Example:
jp@ubuntu:~/demo$ ls
dir1  dir2
jp@ubuntu:~/demo$ mkdir -v dir3
mkdir: created directory `dir3'
jp@ubuntu:~/demo$ ls
dir1  dir2  dir3

% mkdir -m <dir name>: Creates directory and sets file mode(as in chmod).
Example:
jp@ubuntu:~/demo$ ls -l
total 12
drwxr-xr-x 2 jp jp 4096 2011-10-05 23:14 dir1
drwxr-xr-x 3 jp jp 4096 2011-10-05 23:21 dir2
drwxr-xr-x 2 jp jp 4096 2011-10-05 23:26 dir3
jp@ubuntu:~/demo$ mkdir -m 0007 dir4
jp@ubuntu:~/demo$ ls
dir1  dir2  dir3  dir4
jp@ubuntu:~/demo$ ls -l
total 16
drwxr-xr-x 2 jp jp 4096 2011-10-05 23:14 dir1
drwxr-xr-x 3 jp jp 4096 2011-10-05 23:21 dir2
drwxr-xr-x 2 jp jp 4096 2011-10-05 23:26 dir3
d------rwx 2 jp jp 4096 2011-10-05 23:29 dir4
jp@ubuntu:~/demo$ ls dir4
ls: cannot open directory dir4: Permission denied
Only others (has read, write and execute permission) can access the directory "dir4".  Owner and user cannot access it, since the chmod for them is set to zero.


List Directory Contents

ls - command used to list contents of our current directory.
Example:
jp@ubuntu:~$ ls
cpgms      Downloads         Music      Public     Untitled Document 1
Desktop    examples.desktop  Pictures   Templates  Videos
Documents  jp.txt            print.pdf  test.c
(Directory names are in Bold characters and remaining are file names)


% ls <Directory name>: Lists all directory contents.  If directory name is not given, it will list the contents of current directory.
Example:
jp@ubuntu:~$ ls cpgms
anand  dennis  ex01.c  ex02.c  fib.c  jp.c  lib  lssort.c  mat.c  pointers  rhombus.c


% ls -a <directory name>:  Lists all the contents in a directory, including hidden files whose name begins with dot.
Example:
jp@ubuntu:~$ ls -a cpgms
.  ..  anand  dennis  ex01.c  ex02.c  fib.c  jp.c  lib  lssort.c  mat.c  pointers  rhombus.c


% ls  -l <directory name>: Lists contents of the directory in long format, which includes permissions, hard link count, owner, group,  modification date and file name.
Example:
jp@ubuntu:~$ ls -l cpgms
total 44
drwxr-xr-x 3 jp jp 4096 2011-08-13 09:54 anand
drwxr-xr-x 3 jp jp 4096 2011-01-22 22:50 dennis
-rw-r--r--    1 jp jp     72 2009-10-24 07:15 ex01.c
-rw-r--r--    1 jp jp     91 2009-10-26 06:45 ex02.c
-rw-r--r--    1 jp jp   185 2009-10-25 13:06 fib.c
-rw-r--r--    1 jp jp     80 2011-02-15 20:50 jp.c
drwxr-xr-x 2 jp jp 4096 2011-02-15 20:50 lib
-rw-r--r--    1 jp jp 2249 2009-10-28 19:02 lssort.c
-rw-r--r--    1 jp jp  288 2009-10-25 13:18 mat.c
drwxr-xr-x 2 jp jp 4096 2011-08-07 11:13 pointers
-rw-r--r--    1 jp jp   179 2009-11-02 19:26 rhombus.c


% ls -R <directory name>: Recursively lists sub directories.
Example:
jp@ubuntu:~$ ls demo
ls  rm
jp@ubuntu:~$ ls -R demo
demo:
ls  rm
demo/ls:
a.c
demo/rm:
a.c