In this blog, I will explain directory services in Linux. Some important questions like “What are the advantages of directories in Linux?” and “What are the commands we use for directories in Linux? Some important questions related to directories in Linux will have their solutions provided at the end. So let’s get started with the blog.
Introduction to Directory in Linux
A directory in Linux (also known as a folder) is a special type of file that contains a list of files and other directories.
Directories are used to organize files and directories in a hierarchical manner.
File System Directory in Linux
Some common directories in a Linux file system include:
- / (root) – The top-level directory of the file system.
- /bin – Contains essential binary files, such as executables and utilities, that are required for the system to function.
- /sbin – Similar to /bin, but contains system-level executables and utilities.
- /etc – Contains configuration files for the system and various applications.
- /dev – Contains device files, which represent various hardware devices connected to the system.
- /lib – Contains libraries required by the executables in /bin and /sbin.
- /mnt – A directory used as a mount point for file systems that are temporarily mounted on the system.
- /home – Contains the home directories of individual users.
- /usr – Contains user-level software and data, such as applications, libraries, and documentation.
- /var – Contains variable data, such as log files and email.
These are just a few of the directories that are commonly found in a Linux file system. There may be others, depending on the specific distribution and configuration of the system.
Advantages of Directory in Linux
A) Organization
Directory in Linux offers a hierarchical method of classifying and organizing files, making it simpler to locate and access particular files.
B) File and Directory Permission
Directory objects in Linux can be assigned permissions that control who can access and modify their contents. This provides a secure way to manage access to sensitive files.
C) Space Management
Directories help to conserve disk space by allowing multiple files to be grouped together under a single directory.
D) Backup and Restoration
Backing up and restoring a directory is easier than backing up individual files. This makes it easier to recover from data loss.
E) Sharing Files
Users and groups can share directories, which makes it simple to collaborate on projects and exchange data.
Absolute and Relative Path in Linux
In Linux, there are two types of paths that you can use to reference files and directories: Absolute Path and Relative Path.
A) Absolute Path
An absolute path is a full path to a file or directory, starting from the root directory (represented by a single forward slash ‘/’).
For example, the absolute path to the ‘/etc/passwd’ file would be ‘/etc/passwd’.
B) Relative Path
A relative path, on the other hand, is a path that is relative to your current working directory.
The dot (.) and double dot (..) symbols are used to navigate the file system relative to the current working directory.
A single dot (.) represents the current directory, and a double dot (..) represents the parent directory.
For example, if your current working directory is ‘/home/user’, the relative path to the ‘/etc/passwd’ file would be ‘../../etc/passwd’.
Difference Between Absolute and Relative Path in Linux
Absolute and relative paths are used to specify the location of files or directories in a file system.
An absolute path is a complete path that starts from the root directory and specifies the exact location of a file or directory, including all of its components. Absolute paths are usually expressed as full URLs and start with the root directory symbol (e.g., “/” on Unix systems or “C:” on Windows).
A relative path, on the other hand, specifies the location of a file or directory in relation to the current working directory, instead of from the root directory. The current working directory is the directory from which a user is currently operating. Relative paths do not start with the root directory symbol and are often shorter than absolute paths.
Directory in Linux Commands List
Directory Command | Description |
pwd | It shows the user’s current working location or directory. Starting with /, the actual working path is displayed. It is a built-in command. |
ls | It shows the list of a folder. All of the files in the specified folder will be listed. |
cd | The full form of the cd is a change directory. It is used to change to the directory you want to work from the present directory. |
mkdir | mkdir creates the new directory in the system. |
rmdir | It is used to remove a directory from the system. |
Directory in Linux Commands
A) pwd
The “print working directory” command in Linux is abbreviated as “pwd.” It prints the working directory’s absolute path when it is executed.
Checking your location in the file system is a helpful command, especially when switching between folders.
pwd command is a shell built-in command which means that we can store the result of pwd commands in any variable and print the value from this variable.
Syntax:
$ pwd [options]
pwd Directory Commands | Description |
pwd | It displays the current working directory. |
pwd -L | Print the value of $PWD if it names the current workingdirectory. By default, this option runs when you type the command pwd. |
pwd -R | Print the physical directory, without any symbolic links |
$ pwd
/home/binfintech
$ cd Downloads
$ pwd
/home/binfintech/Downloads
From the above code, we can clearly conclude that the first command states that I’m in the directory /home/binfintech after that I changed my directory to Downloads and then again checked my current working directory which is /home/binfintech/Downloads
B) ls
The “ls” command in Linux is used to list the files and directories in the current working directory.
By default, it displays the contents in a vertical list format with basic information such as the file/directory name, size, and modification time.
Syntax:
$ ls [options]
Some important ls commands:
a) To list the hidden files
$ ls -a
Hidden files in Linux are those that begin with the letter “. “, and they are not displayed in the normal directory. The ls -a command will display the complete directory list, including any hidden files.
b) To list all the files and directories in Linux human-readable format
$ ls -lh
This command will display the file sizes in a way that is legible by humans. When the file size is displayed in terms of bytes, it is exceedingly challenging to read. You can get the data in terms of Mb, Gb, Tb, etc. by using the (ls -lh)command.
C) cd
The “cd” command in Linux is used to change the current working directory in the terminal.
It stands for “change directory.” You can use it with the name of a directory to change to that directory, or with “..” to move up one level in the directory hierarchy.
For example, “cd /home/user/documents” will change the current directory to the “documents” directory in the “user” directory under the “/home” directory.
Syntax:
$ cd dirname
Types we can use cd commands in Directory in Linux
A) Just change the directory
$ pwd
/home/binfintech
$ cd Downloads
$ pwd
/home/binfintech/Downloads
From the above code, we can clearly conclude that the first command states that I’m in the directory /home/binfintech after that I changed my directory with the help of the cd command to Downloads and then again checked my current working directory which is /home/binfintech/Downloads
B) Change the Directory in Linux using Absolute Path
$ pwd
/home/binfintech
$ cd /home/binfintech/Downloads
$ pwd
/home/binfintech/Downloads
From the above code, we can see that I have changed the directory from the root directory, which is /home/binfintech/Downloads, which is an absolute path.
C) Change the Directory in Linux using Relative Path
$ pwd
/home/binfintech
$ cd Downloads
$ pwd
/home/binfintech/Downloads
From the above code, we can see I just use a relative path for changing the directory that is located near the current working directory.
D) Change to Home Directory
$ pwd
/home/binfintech/Downloads/Employee/Editor
$ cd ~
$ pwd
/home/binfintech
From the above, we can conclude that my first pwd command displays the result /home/binfintech/Downloads/Employee/Editor and now I want to go to my root folder, which is /home/binfintech I just use cd ~.
E) Change to the previous directory
$ pwd
/home/binfintech/Downloads/Employee/Editor
$ cd ..
$ pwd
/home/binfintech/Downloads/Employee/
From the above example, we can conclude that my first pwd cmd displays the result /home/binfintech/Downloads/Employee/Editor and now I want to go to my previous folder which is /home/binfintech/Downloads/Employee I just use “cd ..“
F) Change to the root directory
To navigate to the entire system’s root directory from the current working directory, just use cd /.
$ pwd
/home/binfintech/Downloads/Employee
$ cd /
$ pwd
/
G) Change to another user’s home directory
$ pwd
/home/binfintech/Downloads/Employee
$ cd ~binfintech
$ pwd
/home/binfintech
Here the username is binfintech, so if anywhere in the directory and you want to navigate to the username directory just use cd ~username.
H) Change to Directory having Spaces
Suppose you have a folder name that has spaces in its name, then you should give that directory name inside the single quote ‘dirname‘.
$ pwd
/home/binfintech
$ cd Downloads/'Employee Info'
/home/binfintech/Downloads/Employee Info
I) Change up to multiple Sub Directories
We can change multiple directories by using the forward slash (/) in the command.
$ pwd
/home/binfintech
$ cd /home/binfintech/Downloads
$ pwd
/home/binfintech/Downloads
D) mkdir
The mkdir command in Linux is used to create a new directory or folder.
Syntax:
$ mkdir [options] dirname
a) Creating a directory in Linux
$ mkdir Employee
$ pwd
/home/itsvek03
$ cd Employee
$ pwd
/home/itsvek03/Employee
From the above code, we can clearly see that the employee directory was successfully created, and with the help of the cd command, we have navigated to that directory.
b) Create multiple directories in Linux
$ mkdir Employee Salary
$ pwd
/home/itsvek03
$ ls
Employee
Salary
From the above code, we can clearly see that we have created both our directories successfully. Both will be parent directories.
c) Create a parent and child directory in Linux simultaneously
$ mkdir -p Employee Alex
$ ls
Employee
$ cd Employee
$ ls
Alex
Here, Employee is the parent directory, while Alex is the child directory of Employee.
d) Set mode in the directory in Linux
$ mkdir -m=rwx dir_name
## For read, write and execute
$ mkdir -m=rw dir_name
## For read,write only
$ mkdir -m=r <file> # For read-only
$ mkdir -m=w <file> # for write only
$ mkdir -m=x <file>" # For execute only
For Example:
$ mkdir -m=rwx Copper
$ mkdir -m=rw Alex
$ mkdir -m=x Sam
$ ls -l
total 12
drw-rw-r-- 2 4096 Feb 11 19:54 Alex
drwxrwxr-x 2 4096 Feb 11 19:54 Copper
d--x--x--x 2 4096 Feb 11 19:54 Sam
E) rmdir
The “rmdir” command in Linux is used to remove (delete) empty directories.
It takes one or more directory names as arguments and removes them if they are empty. If a directory contains any files or subdirectories, rmdir will fail to remove it and produce an error.
Syntax:
$ rmdir dir_name
a) Remove the directory in Linux
$ pwd
/home/binfintech/Employee
$ rmdir Employee
$ pwd
/home/binfintech
From the above code, we can clearly see that we have deleted Employee Directory Successfully
b) Remove the parent directory in Linux
$ pwd
/home/binfintech/Employee
$ ls
Copper
$ cd Copper
$ ls
Position
$ cd ..
$ pwd
/home/binfintech/Employee
$ rmdir -p Copper/Position
From the above code, let’s first discuss the directory structure. In my employee folder, there is a parent directory named “Copper,” which has a child directory named “Position.” So, we have to delete the parent directory, If I write rmdir Copper, It will show an error because it is not an empty directory. If we have to delete the child as well as the parent directory simultaneously.
So use the command rmdir -p Copper/Position.
Note: All these notes are taken from javatpoint.
FAQ
a) A directory in Linux (also known as a folder) is a special type of file that contains a list of files and other directories.
b) Directories are used to organize files and directories in a hierarchical manner.
A) Organization
B) File and Directory Permission
C) Space Management
D) Backup and Restoration
E) Sharing Files
The commands for the directory in Linux are:
mkdir
rmdir
ls
cd
pwd
We can create directories with the help of the mkdir command.
Syntax:
mkdir dir_name
We can remove directories with the help of the rmdir command.
Syntax:
rmdir dir_name
Articles on Linux
- What is Linux Operating System | Introduction to Linux
- Explain the chmod command in Linux | Linux chmod command
- Linux User Management || User Management in Linux
- Linux Computer Network Advanced Command | Network Command
- Redirection in Linux I/O| Linux I/O Redirection
- CronTab and Job Scheduling in Linux | Make CronTab Project
- Linux Firewall Unlock Rules with Firewall-cmd Tutorial
- netstat command in Linux | Linux netstat command
- SSH Command Full Guide with Practical | Linux SSH Service
- awk command Guide | How to arrange the output of the file in Linux
- sed command Full Guide Tutorial | Linux sed Command
- Iptables commands Full Guide: How to make our own Firewall
- Kali Linux Full Tutorial Guide | How to Install in the Virtual Machine
- Package Management in Linux | Linux Package Management Tools
Articles on CyberSecurity
- What is Ethical Hacking || Introduction to Ethical Hacking
- System Security and Protection in Cybersecurity
- HIPAA (Health Insurance Portability and Accountability Act) in Cyber Security Law
- PCI DSS (Physical Card Industry and Data Security Standard) in Cyber Security Law
- What is GLBA (Gramm-Leach-Bliley Act) in Cyber Security Law?
- What is NIST (National Institute of Standards and Technology)?
- What is GDPR (General Data Protection Regulation)?
- What are ISO 27001 and CIA in Cyber Security Law?
- What is HITRUST Framework in Cyber Security Law
- Ethical Hackers, Types, and Responsibilities of Ethical Hackers
- VAPT, Benefits, and What are the Roles of VAPT in Company
- What is Pen Testing, Requirement, Types, and Roles of PenTester