Linux Directory Structure & Management Commands
In Linux, a directory is a structure used to organize and manage files. A directory can contain files and other directories, which together form the Filesystem Hierarchy Standard (FHS).
The directory structure in Linux consists of a series of hierarchical directories, each with a unique path name that identifies its location within the filesystem. The root directory (/) is the starting point of the entire filesystem hierarchy; all other directories are paths beginning from the root directory.
FHS
FHS (Filesystem Hierarchy Standard) is a standard used to define the layout and organization of file systems in Linux and other Unix-like operating systems. It specifies where files and directories should be placed within the Linux operating system and what files and subdirectories those locations should contain.
The purpose of FHS is to ensure consistency in filesystem organization across different Linux distributions, making it easier for applications and system administrators to migrate and manage systems between different Linux platforms.
FHS defines the layout and organization of file systems in Linux and other Unix-like operating systems, defining many directories, including:
/: The root directory, containing all files and directories of the entire filesystem./bin: Contains basic commands required for system startup and operation (e.g.,ls,cp,mv)./boot: Contains all files required to boot Linux, such as kernel images and bootloaders./dev: Contains device files, such as hard drives, keyboards, etc./etc: Contains system configuration files, such as network settings, user accounts, and passwords./home: Contains home directories for all regular users./lib: Contains shared library files for system programs and libraries./media: Used to mount removable devices, such as CD-ROMs and USB drives./mnt: Used to mount other filesystems or network filesystems./opt: Used to install optional applications and other software packages./proc: A virtual filesystem containing information about currently running processes and system status./root: The home directory for the root user./run: Contains runtime files, such as PID files and socket files./sbin: Contains system management commands that can only be run by superusers (e.g.,iptables,ifconfig)./srv: Used to store data for specific services./sys: Contains information about system hardware devices and drivers./tmp: Contains temporary files, which may be deleted after a system reboot./usr: Contains system-wide applications and files, typically mounted as a read-only filesystem./var: Contains files that change with system usage, such as log files, mail, and databases.
These directories and files are crucial for system administration and application development; therefore, understanding the FHS standard helps administrators manage Linux systems more effectively.
Relative Paths and Absolute Paths
In Linux, a path refers to the location of a file or directory within the filesystem. Paths can be represented using relative paths or absolute paths.
A relative path is a path relative to the current working directory, starting with . or ... Here, . represents the current directory, and .. represents the parent directory. For example, if the current working directory is /home/user, then the relative path ../etc refers to the /etc directory in the parent directory.
An absolute path is a complete path starting from the root directory, beginning with /. For example, the absolute path /usr/share/doc refers to the /usr/share/doc directory under the root directory of the filesystem.
Path switching refers to changing the current working directory in the command line. This can be achieved using the cd command. Here is an example of path switching:
- Assume the current working directory is
/home/user. - Enter the command
cd /usr/share/docto switch to the/usr/share/docdirectory. This is an absolute path. - Enter the command
cd ..to switch to the parent directory/usr/share. - Enter the command
cd userto switch to the/usr/share/userdirectory. This is a relative path.
When performing path switching, you can use either relative or absolute paths. When using a relative path, be mindful of the current working directory's location to avoid errors.
Directory-Related Commands
Here are some commonly used directory-related commands in Linux:
ls: Lists files and subdirectories in a directory. For example, list all files and subdirectories in the current directory:lscd: Switches the current working directory. For example, switch to the/home/userdirectory:cd /home/usermkdir: Creates a new directory. For example, create a new directory namedtest:mkdir testrmdir: Removes an empty directory. For example, remove the empty directory namedtest:rmdir testcp: Copies a file or directory. For example, copy the filefile1into the/home/user/backupdirectory:cp file1 /home/user/backupmv: Moves a file or directory. For example, move the filefile1into the/home/user/backupdirectory:mv file1 /home/user/backuprm: Deletes a file or directory. For example, delete the file namedfile1:rm file1pwd: Displays the path of the current working directory. For example, display the path of the current working directory:pwdfind: Searches for files and directories in the filesystem. For example, find all files ending with.txt:find / -name "*.txt"du: Displays disk usage of directories and files. For example, display the disk usage of all files and subdirectories in the current directory:dutar: Used to create, compress, and decompress archive files, capable of packaging multiple files and directories. For example, package and compress all files and subdirectories under/home/userinto a tar file:tar -czvf backup.tar.gz /home/userchown: Changes the owner of a file or directory. For example, change the owner of the filefile1touser1:chown user1 file1chmod: Changes the access permissions of a file or directory. For example, set the read/write permissions of the filefile1so that only the owner has permission:chmod 600 file1ln: Creates hard links or symbolic links. For example, create a symbolic link namedlink1pointing to the filefile1:ln -s file1 link1tree: Displays the structure of directories and subdirectories in a tree format. For example, display the tree structure of the current directory and all its subdirectories and files:tree
These commands allow users to manage and operate directories within the filesystem, performing various tasks such as creating, deleting, moving, copying, and searching for files and directories.