[Unix-Linux] Directory Management

Date:     Updated:

Categories:

Tags:

📋 This is my note-taking from what I learned in the UNIX/LINUX Tutorial!


Directory Management in Unix

A directory is a file the solo job of which is to store the file names and the related information. All the files, whether ordinary, special, or directory, are contained in directories.

Unix uses a hierarchical structure for organizing files and directories. This structure is often referred to as a directory tree. The tree has a single root node, the slash character (/), and all other directories are contained below it.

Home Directory

Go to Home directory

cd ~

Suppose you have to go in any other user’s home directory

cd ~username

To go in your last directory, Use the following command

cd -

Absolute/Relative Pathnames

Absolute Pathnames

Directories are arranged in a hierarchy with root (/) at the top. The position of any file within the hierarchy is described by its pathname.

Elements of a pathname are separated by a /. A pathname is absolute, if it is described in relation to root, thus absolute pathnames always begin with a /.

Following are some examples of absolute filenames.

/etc/passwd
/users/sjones/chem/notes
/dev/rdsk/Os3

Relative Pathnames

A pathname can also be relative to your current working directory. Relative pathnames never begin with /. Relative to user amrood’s home directory, some pathnames might look like this.

chem/notes
personal/res

Listing Directories

List the files in a directory

ls dirname

List all the files contained in /usr/local directory

ls /usr/local

Creating Directories

Create directory

mkdir dirname

Create test-dir directory in the /tmp directory

mkdir /tmp/test-dir

Create docs and pub directories

mkdir docs pub

Creating Parent Directories

If there is no parent directory, mkdir issues 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, Specify the -p option to the mkdir command

mkdir -p /tmp/amrood/test

//Create all the required parent directories

Removing Directories

Remove directories

rmdir dirname

Remove multiple directories

rmdir dirname1 dirname2 dirname3

Changing Directories

Change directories

cd dirname

From this directory, cd to the directory /usr/home/seyeon

cd ../../home/seyeon

Renaming Directories

mv(move) command can be used to rename a directory

mv olddir newdir

The directories . (dot) and .. (dot dot)

The filename . (dot) represents the current working directory; and the filename .. (dot dot) represents the directory one level above the current working directory, often referred to as the parent directory.

If we enter the command to show a listing of the current working directories/files and use the -a option to list all the files and the -l option to provide the long listing, we will receive 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




Back to Top

See other articles in Category Unix-Linux

Leave a comment