May 23, 2021 UNIX Getting started
A directory is a file that stores the name of the file and related information. All files, whether normal, special, or directories, are included in the directory.
UNIX uses hierarchies to organize files and directories. T
his structure is often referred to as a tree.
There is a root node on the tree, the slash character
(/)
and all other directories are contained below it.
The home directory is the directory that you were in when you first logged on.
Most of your work will be done in the home directory and in your custom subdirect directory.
You can switch to the home directory at any time by executing the following commands in any directory:
$cd ~
$
Here
~
home directory is represented by .
If you want to jump to any other user's home directory, you can use the following command:
$cd ~username
$
You can use the following commands to jump to your nearest directory:
$cd -
$
The directories are organized hierarchically, with the root
(/)
The location of any file within the hierarchy is described by its path.
The path is
/
by / .
The path name is absolute if it is described as a relationship with the root, so the absolute path name always starts
/
.
These are some examples of absolute file names.
/etc/passwd
/users/sjones/chem/notes
/dev/rdsk/Os3
The path can also be relative to your current working directory. T
he relative path never
/
.
Some paths may look like this relative to the user's amrood home directory:
chem/notes
personal/res
Whenever you want to determine the file system hierarchy you are in, enter the command pwd to print the current working directory:
$pwd
/user0/home/amrood
$
To list files in a directory, you can use the following syntax:
$ls dirname
Here is an example that
/usr/local
directory:
$ls /usr/local
X11 bin gimp jikes sbin
ace doc includelib share
atalk etc info man ami
Create a directory with the following command:
$mkdir dirname
Here, dirname is the absolute or relative path name of the directory you want to create. For example, the command:
$mkdir mydir
$
Create a directory mydir in the current directory. Here's another example:
$mkdir /tmp/test-dir
$
This command creates a directory test-dir in the
/tmp
directory.
The
command mkdir does
not produce any output if it successfully creates the requested directory.
If you give more than one directory on the command line, mkdir creates each directory. For example:
$mkdir docs pub
$
Create directory docs and pubs under the current directory.
Sometimes when you want to create a directory, its parent directory may not exist. In this case, mkdir issues an error message that looks like this:
$mkdir /tmp/amrood/test
mkdir: Failed to make directory "/tmp/amrood/test";
No such file or directory
$
In this case, you can specify the -p option for the mkdir command. I t creates all the necessary directories for you. For example:
$mkdir -p /tmp/amrood/test
$
The command above creates the desired parent directory.
You can delete the directory using the rmdir command as follows:
$rmdir dirname
$
Note: Make sure that the directory is empty when you delete it, which means that there should not be any files or subdirections in the directory.
You can delete more than one directory at a time as follows:
$rmdir dirname1 dirname2 dirname3
$
The above commands remove the directories dirname1, dirname2, and dirname2, provided that they are empty. If deleted successfully, the rmdir command does not generate any output.
You can use the cd command to do more than change the home directory: you can use it to jump to any directory with a valid absolute or relative path. The syntax looks like this:
$cd dirname
$
Here, dirname is the name of the directory you want to jump to. For example, the command:
$cd /usr/local/bin
$
Change
/usr/local/bin
From this directory, you can jump to the
/usr/home/amrood
path:
$cd ../../home/amrood
$
The mv (move) command can also be used to rename the directory. The syntax looks like this:
$mv olddir newdir
$
You can rename the directory mydir as yourdir, as follows:
$mv mydir yourdir
$
The file
.
. . ( point ) represents the current working directory, and the file name . . . ( point
..
represents the next level of the current working directory, often referred to as the parent directory.
If we enter a list of current working directory files to display,
-a
all files with the
-l
provide a long list, which is the result.
$ls -la
drwxrwxr-x4teacher class 2048 Jul 16 17.56 .
drwxr-xr-x60 root 1536 Jul 13 14:18 ..
----------1teacher class 4210 May 1 08:27 .profile
-rwxr-xr-x1teacher class 1948 May 12 13:42 memo
$