A directory is a file whose sole task is to store the name and related information about the file. All files, be they ordinary files, special files, or folders, are kept in folders.
UNIX uses a hierarchical structure to organize files and directories. This structure is often referred to as a directory tree. This tree has a root node, a slash (/) character, and all the directories contained under it.
The root directory (Home) in Unix / Linux
The folder you see right when you first log in is the main folder.
You will do a lot of work in the main directory and sub folders where you create to organize your files.
You can get to the home directory at any time by using the following command:
$cd ~ $
Here the ~ symbol indicates the main directory. If you want to get into the home directory of any other user, use the following command:
$cd ~username $
To enter the final directory, you can use the following command:
$cd - $
Absolute / relative pathname in Unix / Linux
Folders are arranged in a hierarchy with root (/) at the top. The location of any file in this system is described by its pathname.
Pathname elements are separated by a (/) sign. A pathname is the absolute value if it is described in relation to the root, so the absolute pathname usually begins with an ampersand (/).
Here are examples of absolute filenames.
/etc/passwd /users/sjones/chem/notes /dev/rdsk/Os3
A pathname may be in relation to your current working directory. The relative pathname never begins with (/). Regarding the amrood home directory, there are several pathnames that look like this:
To determine where you are in the file hierarchy at any time, enter the command pwd to print the current working directory.
$pwd /user0/home/amrood $
Lists directories under Unix / Linux
To list the directories, you can use the following syntax:
Below is an example to list all files contained in / usr / local directory.
$ls /usr/local X11 bin gimp jikes sbin ace doc include lib share atalk etc info man ami
Create directories in Unix / Linux
The directory is created with the following command:
Here, the directory is the absolute pathname or relationship you want to create. For example, the following command:
$mkdir mydir $
Create the mkdir directory in the current directory. Here is an example:
$mkdir /tmp/test-dir $
This command creates the test-dir directory in / tmp directory. The mkdir command will not produce output if it successfully creates the required directory.
If you provide more than one directory on the command line, mkdir creates each of the directories. For example:
$mkdir docs pub $
It creates docs and pub directories under the current directory.
Create parent directories in Unix / Linux
Sometimes you want to create a directory, the home directory or its directories don’t exist. In this case, the command mkdir Issue an error message as follows:
$mkdir /tmp/amrood/test mkdir: Failed to make directory "/tmp/amrood/test"; No such file or directory $
In the above case, you can specify the option -p to the command mkdir. It creates all the necessary directories for you. For example:
$mkdir -p /tmp/amrood/test $
The above command creates all the necessary directories.
Delete the directories in Unix / Linux
The directory can be deleted using the command rmdir as follows:
$rmdir dirname $
Note: To delete a directory, you should make sure it does not contain any important data, that is, do not have any files or subfolders inside this folder.
You can delete multiple folders at the same time as follows:
$rmdir dirname1 dirname2 dirname3 $
The above command removes directories dirname1, dirname2 and dirname3 if they are empty. The rmdir command will not generate output if it executes successfully.
Change directories in Unix / Linux
You can use the cd command to make many changes to the home directory. You can use it to change any directory by specifying an absolute path and a valid relationship. The syntax is as follows:
$cd dirname $
Here, dirname is the name of the directory where you want to make the changes. For example, the command:
$cd /usr/local/bin $
Make changes to / usr / local / bin. From this directory you can cd the / usr / home / amrood directory using the following relational path:
$cd ../../home/amrood $
Rename folders in Unix / Linux
The mv command can be used to rename a directory. The syntax is as follows:
$mv olddir newdir $
You can rename a folder mydir Fort yourdir as follows:
$mv mydir yourdir $
The. (Dot) and .. (dot dot) directories in Unix / Linux
The file name is. (Dot) representing the current working directory; and the file name is .. (dot dot) represents the folder at the top of the current working directory, usually the main directory.
If we enter this command to indicate a list of current working directories and use the option -a to list all files and options -l provide a long list type, this will produce the following result:
$ls -la drwxrwxr-x 4 teacher class 2048 Jul 16 17.56 . drwxr-xr-x 60 root 1536 Jul 13 14:18 .. ---------- 1 teacher class 4210 May 1 08:27 .profile -rwxr-xr-x 1 teacher class 1948 May 12 13:42 memo $
Previous lesson: File Management in Unix / Linux
Next: File access rights / modes in Unix