|
|
Creating New
Directories
Linux Tips by Burleson Consulting |
The mkdir (make directory)
command is used to create a new directory. The directory to be created can be
referenced via an absolute path or via a relative path starting with the current
working directory.
* Make a directory using an absolute path:
$ ls
examples
$ mkdir /home/tclark/new_dir1
$ ls
examples new_dir1
* Make a directory using a
relative path:
$ mkdir new_dir2
$ ls
examples new_dir1 new_dir2
The ?p (parent) option
allows creation of a complete directory branch when parent directories do not
yet exist. This will actually create both the parent directory and subdirectory
specified. In our example that means the new_dir3 and sub_dir3 are both created
with a single command.
* Make a new directory with a subdirectory using the ?p
option:
$ mkdir -p new_dir3/sub_dir3
$ ls
examples new_dir1 new_dir2
new_dir3
$ ls new_dir3
sub_dir3
Deleting Directories
The rmdir (remove directory) command is used to delete
an empty directory. The directory to be deleted can be referenced via an
absolute path or via a relative path starting with the current working
directory. The following are some examples of each method:
$ rmdir /home/tclark/new_dir1
$ ls
examples new_dir2 new_dir3
$ rmdir new_dir2
$ ls
examples new_dir3
A directory cannot be removed with the rmdir command
unless it is completely empty. To remove a directory and it's contents you can
use the rm ?r command which will be covered later in this chapter. Be careful
with this as you can easily delete more than you had intended!
Renaming directories
Directories can be renamed with the mv command. Here's
an example of renaming a directory:
$ mv new_dir3 another_dir
$ ls
examples another_dir
The mv command can also be used to move a directory to
another location. This will be covered more later in this chapter when we talk
about moving and renaming files.
Navigating the Directory Tree
The cd (change directory) command is used to change from
one directory to another. As with creating and deleting directories, it is
possible to change to another directory by using either an absolute path or by
using a relative path referenced from the current working directory.
There are also some special shortcuts that can be used
with the cd command. For instance, .. refers to the parent directory of the
current working directory, ~ refers to the home directory of the current user
and - refers to the last directory from which the user changed. The following
are some examples of directory navigation.
To demonstrate this without having to type pwd after
each cd we have added the working directory to the prompt on a separate line.
$ PS1="Directory:\w\n\\$ "
Directory:/usr/bin
$ cd /usr/bin
Directory:/usr/bin
$ cd
Directory:~
$ cd /
Directory:/
$ cd ~
Directory:~
$ cd examples
Directory:~/examples
$ cd /tmp
Directory:/tmp
$ cd -
/home/tclark/examples
Directory:~/examples
$ cd /home/tclark
Directory:~
You might have noticed that the second cd command had no
directory listed. This will take you back to your home directory from
anywhere. This is the exact same result as the cd ~ command. It is also worth
mentioning that when we used the cd ? combination to take us to the previous
directory it printed the current directory before returning us to the previous
one.
Listing Directory Contents
The ls command is used to list the contents,
subdirectories and files, within a directory. There are several options to the
ls command that determine both the content and order of the listing displayed.
Without specifying any options, ls displays subdirectory and file names.
$ cd ~/examples
$ ls
declaration.txt gettysburg.txt preamble.txt
Table 4.2 contains descriptions of some of the options
that are commonly used to display additional information:
Option |
Purpose |
-a |
Display all files including those
which begin with a period (.) which would otherwise not be shown |
-l |
Display long information include
permissions, ownership, size, modification date/time, and file name in the
display |
-d |
Display information about a directory
rather than the contents of that directory |
-t |
Sort the display by date/time |
Table 4.2: ls Command Options
The following are some examples of the ls command using
the above options:
$ cd ~/examples
$ ls
declaration.txt gettysburg.txt preamble.txt
$ ls -al
total 20
drwxrwxr-x 2 tclark tclark 4096 Jan 13 17:48 .
drwx------ 4 tclark tclark 4096 Jan 13 18:29 ..
-rw-rw-r-- 1 tclark tclark 2230 Jan 13 17:47 declaration.txt
-rw-rw-r-- 1 tclark tclark 1310 Jan 13 17:48 gettysburg.txt
-rw-rw-r-- 1 tclark tclark 360 Jan 13 17:48 preamble.txt
$ ls -alt
total 20
drwx------ 4 tclark tclark 4096 Jan 13 18:29 ..
drwxrwxr-x 2 tclark tclark 4096 Jan 13 17:48 .
-rw-rw-r-- 1 tclark tclark 360 Jan 13 17:48 preamble.txt
-rw-rw-r-- 1 tclark tclark 1310 Jan 13 17:48 gettysburg.txt
-rw-rw-r-- 1 tclark tclark 2230 Jan 13 17:47 declaration.txt
Now that we're comfortable managing and navigating
around directories let's take a look at some file commands.
This is an excerpt from "Easy
Linux Commands" by Linux guru Jon Emmons. You can purchase it for only
$19.95 (30%-off) at
this link.