how to create a directories in linux?


#pwd
The you are here sign can be displayed with the pwd command (Print Working
Directory). Go ahead, try it: Open a command line interface (like gnome-terminal,
konsole, xterm, or a tty) and type pwd. The tool displays your current directory.
linux@redhat:~$ pwd
/home/paul

#cd
You can change your current directory with the cd command (Change Directory).
linux@redhat$ cd /etc
linux@redhat$ pwd
/etc
linux@redhat$ cd /bin
linux@redhat$ pwd
/bin
linux@redhat$ cd /home/paul/
linux@redhat$ pwd
/home/paul

#cd ~
You can pull off a trick with cd. Just typing cd without a target directory, will put
you in your home directory. Typing cd ~ has the same effect.
linux@redhat$ cd /etc
linux@redhat$ pwd
/etc
linux@redhat$ cd
linux@redhat$ pwd
/home/paul
linux@redhat$ cd ~
linux@redhat$ pwd
/home/paul

#cd ..
To go to the parent directory (the one just above your current directory in the
directory tree), type cd .. .
linux@redhat$ pwd
/usr/share/games
linux@redhat$ cd ..
linux@redhat$ pwd
/usr/share

#cd -
Another useful shortcut with cd is to just type cd - to go to the previous directory.
linux@redhat$ pwd
/home/paul
linux@redhat$ cd /etc
linux@redhat$ pwd
/etc
linux@redhat$ cd -
/home/paul
linux@redhat$ cd -
/etc

absolute and relative paths

You should be aware of absolute and relative paths in the file tree. When you type
a path starting with a slash (/), then the root of the file tree is assumed. 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/paul. From within this
directory, you have to type cd /home instead of cd home to go to the /home directory.
linux@redhat$ pwd
/home/paul
linux@redhat$ cd home
bash: cd: home: No such file or directory
linux@redhat$ cd /home
linux@redhat$ pwd
/home
When inside /home, you have to type cd paul instead of cd /paul to enter the
subdirectory paul of the current directory /home.
linux@redhat$ pwd
/home
linux@redhat$ cd /paul
bash: cd: /paul: No such file or directory
linux@redhat$ cd paul
linux@redhat$ pwd
/home/paul
In case your current directory is the root directory /, then both cd /home and cd
home will get you in the /home directory.
linux@redhat$ pwd
/
linux@redhat$ cd home
linux@redhat$ pwd
/home
linux@redhat$ cd /
linux@redhat$ cd /home
linux@redhat$ pwd
/home
This was the last screenshot with pwd statements. From now on, the current directory
will often be displayed in the prompt. Later in this book we will explain how the shell
variable $PS1 can be configured to show this.

path completion
The tab key can help you in typing a path without errors. 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).
You will need fewer key strokes when using the tab key, and you will be sure your
typed path is correct!

#ls
You can list the contents of a directory with ls.
paul@pasha:~$ ls
allfiles.txt dmesg.txt httpd.conf stuff summer.txt
paul@pasha:~$

#ls -a
A frequently used option with ls is -a to show all files. Showing all files means
including the hidden files. When a file name on a Unix file system starts with a dot,
it is considered a hidden file and it doesn't show up in regular file listings.
paul@pasha:~$ ls
allfiles.txt dmesg.txt httpd.conf stuff summer.txt
paul@pasha:~$ ls -a
. allfiles.txt .bash_profile dmesg.txt .lesshst stuff
.. .bash_history .bashrc httpd.conf .ssh summer.txt
paul@pasha:~$

#ls -l
Many times you will be using options with ls to display the contents of the directory
in different formats or to display different parts of the directory. Typing just ls gives
you a list of files in the directory. Typing ls -l (that is a letter L, not the number 1)
gives you a long listing.
paul@pasha:~$ ls -l
total 23992
-rw-r--r-- 1 paul paul 24506857 2006-03-30 22:53 allfiles.txt
-rw-r--r-- 1 paul paul 14744 2006-09-27 11:45 dmesg.txt
-rw-r--r-- 1 paul paul 8189 2006-03-31 14:01 httpd.conf
drwxr-xr-x 2 paul paul 4096 2007-01-08 12:22 stuff
-rw-r--r-- 1 paul paul 0 2006-03-30 22:45 summer.txt

#ls -lh
Another frequently used ls option is -h. It shows the numbers (file sizes) in a more
human readable format. Also shown below is some variation in the way you can give
the options to ls. We will explain the details of the output later in this book.
paul@pasha:~$ ls -l -h
total 24M
-rw-r--r-- 1 paul paul 24M 2006-03-30 22:53 allfiles.txt
-rw-r--r-- 1 paul paul 15K 2006-09-27 11:45 dmesg.txt
-rw-r--r-- 1 paul paul 8.0K 2006-03-31 14:01 httpd.conf
drwxr-xr-x 2 paul paul 4.0K 2007-01-08 12:22 stuff
-rw-r--r-- 1 paul paul 0 2006-03-30 22:45 summer.txt
paul@pasha:~$ ls -lh
total 24M
-rw-r--r-- 1 paul paul 24M 2006-03-30 22:53 allfiles.txt
-rw-r--r-- 1 paul paul 15K 2006-09-27 11:45 dmesg.txt
-rw-r--r-- 1 paul paul 8.0K 2006-03-31 14:01 httpd.conf
drwxr-xr-x 2 paul paul 4.0K 2007-01-08 12:22 stuff
-rw-r--r-- 1 paul paul 0 2006-03-30 22:45 summer.txt
paul@pasha:~$ ls -hl
total 24M
-rw-r--r-- 1 paul paul 24M 2006-03-30 22:53 allfiles.txt
-rw-r--r-- 1 paul paul 15K 2006-09-27 11:45 dmesg.txt
-rw-r--r-- 1 paul paul 8.0K 2006-03-31 14:01 httpd.conf
drwxr-xr-x 2 paul paul 4.0K 2007-01-08 12:22 stuff
-rw-r--r-- 1 paul paul 0 2006-03-30 22:45 summer.txt
paul@pasha:~$ ls -h -l
total 24M
-rw-r--r-- 1 paul paul 24M 2006-03-30 22:53 allfiles.txt
-rw-r--r-- 1 paul paul 15K 2006-09-27 11:45 dmesg.txt
-rw-r--r-- 1 paul paul 8.0K 2006-03-31 14:01 httpd.conf

#mkdir
Walking around the Unix file tree is fun, but it is even more fun to create your own
directories with mkdir. You have to give at least one parameter to mkdir, the name
of the new directory to be created. Think before you type a leading / .
linux@redhat:~$ mkdir MyDir
linux@redhat:~$ cd MyDir
linux@redhat:~/MyDir$ ls -al
total 8
drwxr-xr-x 2 paul paul 4096 2007-01-10 21:13 .
drwxr-xr-x 39 paul paul 4096 2007-01-10 21:13 ..
linux@redhat:~/MyDir$ mkdir stuff
linux@redhat:~/MyDir$ mkdir otherstuff
linux@redhat:~/MyDir$ ls -l
total 8
drwxr-xr-x 2 paul paul 4096 2007-01-10 21:14 otherstuff
drwxr-xr-x 2 paul paul 4096 2007-01-10 21:14 stuff

#mkdir -p
When given the option -p, then mkdir will create parent directories as needed.
linux@redhat:~$ mkdir -p MyDir2/MySubdir2/ThreeDeep
linux@redhat:~$ ls MyDir2
MySubdir2
linux@redhat:~$ ls MyDir2/MySubdir2
ThreeDeep
linux@redhat:~$ ls MyDir2/MySubdir2/ThreeDeep/

#rmdir
When a directory is empty, you can use rmdir to remove the directory.
linux@redhat:~/MyDir$ rmdir otherstuff
linux@redhat:~/MyDir$ ls
stuff
linux@redhat:~/MyDir$ cd ..
linux@redhat:~$ rmdir MyDir
rmdir: MyDir/: Directory not empty
linux@redhat:~$ rmdir MyDir/stuff
linux@redhat:~$ rmdir MyDir


#rmdir -p
And similar to the mkdir -p option, you can also use rmdir to recursively remove
directories.
linux@redhat:~$ mkdir -p dir/subdir/subdir2
linux@redhat:~$ rmdir -p dir/subdir/subdir2
linux@redhat:~$

No comments:

Post a Comment