Linux - File System Hierarchy

Linux - File System Hierarchy

Linux Directory & File Commands

In Linux, everything is represented as a file, including hardware programs, which are stored in a directory. Each directory contains a file in a tree structure format, which is known as File System Hierarchy.

Linux uses a single, inverted tree-like structure to represent File system hierarchy as shown below:

  1. / - The base of the Linux directory is the root which is the starting point of the file system hierarchy. The root directory is represented with / ( forward slash). It is a top-level directory.

  2. /root - Home directory for the root user (superuser).

  3. /mnt (Mount Directory) - Used to mount a file system temporarily.

  4. /home (Home Directory) - Contains the secondary users.

  5. /media (Removable Media Devices) - Contains subdirectories where removable media devices ( USB, CD) inserted into the computer are mounted.

  6. /bin (User Binaries) - Contains the binary executable, common Linux commands you need to use in single-user mode is located under this directory.

  7. /usr (User Binaries) - Contains applications and files used by users.

  8. /sbin (System Binaries) - Just like /bin, /sbin also contains binary executables but the Linux commands located under this directory are used typically by system administrators for system maintenance purposes. for example fdisk, ifconfig, reboot, swap-on, and IP tables.

  9. /etc (Configuration Files) - Contains all core configuration files of the server. It controls the behavior of an operating system (OS) or application. It also contains startup and shutdown program scripts to start and stop individual programs.

  10. /dev (Device Files) - Contains hardware device files that include terminal devices, USB, or any other device attached to the system. for example /dev/tty1, /dev/usbmon0, etc.

  11. /opt (Optional Applications) - Used for installing the application software from third-party vendors that are not available in Linux distribution. Usually, the software code is stored in the opt directory and binary code is linked to the bin directory so that all users can run that software.

  12. /var (Variable files) - File contents that tend to grow are located in this directory such as log files.

    for example -

    i. /var/log - system log files generated by OS and other applications.

    ii. /var/lib - contains database and package files.

    iii. /var/mail - contains emails.

    iv. /var/tmp - contains temporary files needed for a reboot.

Linux directory & File commands :

  1. mkdir: It is used to create your directories in the system.

     #Create a single directory [syntax: mkdir <dir_name>] 
     ubuntu@ip-172-31-93-81:~$ mkdir dev 
     # Create multiple directories [syntax: mkdir <dir1_name> <dir2_name> <dirn_name>]
     ubuntu@ip-172-31-93-81:~$ mkdir qa preprod prod 
     # Create a directory path [syntax: mkdir -p <path_name>]
     ubuntu@ip-172-31-93-81:~$ mkdir -p test/maven/results
     # Create an n number of directories [syntax: mkdir <nameofdir{1..n}]
     ubuntu@ip-172-31-93-81:~$ mkdir dir{1..3}
     ubuntu@ip-172-31-93-81:~$ ls
     dev  dir1  dir2  dir3  preprod  prod  qa  test
    
  2. rmdir: It is used to remove the directories from the system.

    1.  # Remove a directory [syntax: rmdir <dir_name>]
       ubuntu@ip-172-31-93-81:~$ rmdir qa
       # Remove multiple directories [syntax: rmdir <dir1_name> <dir2_name> <dirn_name>]
       ubuntu@ip-172-31-93-81:~$ rmdir preprod prod
      
  3. touch: It is used to create an empty file.

     # Create a new empty file [syntax: touch <file_name>]
     ubuntu@ip-172-31-93-81:~$ touch newfile.txt 
     # Create multiple new empty files [syntax: touch <file1_name> <file2_name> <filen_name>]
     ubuntu@ip-172-31-93-81:~$ touch test1.txt data1.py data.sql 
     # Create an n number of files [syntax: touch <nameoffile{1..n}]
     ubuntu@ip-172-31-93-81:~$ touch new{1..5}.txt
    
  4. cp: It is used to copy and paste files or directories.

     # copy a file to the directory [syntax: cp <options> <source> <destination> { <options> could be -r for recursive , -v for verbose , -f for forcefully} ]
     ubuntu@ip-172-31-93-81:~$ cp newfile.txt dir1
     # copy directories that start with the name 'dir' to another directory 'preprod'
     ubuntu@ip-172-31-93-81:~$ cp -rvf dir* preprod 
     'dir1' -> 'preprod/dir1' 
     'dir2' -> 'preprod/dir2' 
     'dir3' -> 'preprod/dir3'
    
  5. mv: It is used to cut and paste files or directories.

     # Move a file to a directory [syntax: mv <options> <source> <destination> { <options> could be -r for recursive , -v for verbose , -f for forcefully} ]
     ubuntu@ip-172-31-93-81:~$ mv test_rename.txt test 
     # Move files that start with the name 'new' to directory 'dir2'
     ubuntu@ip-172-31-93-81:~$ mv new* dir2  
     # Rename a file from test1.txt  to test_rename.txt [syntax: mv <old_name> <new_name>]
     ubuntu@ip-172-31-93-81:~$ mv test1.txt test_rename.txt
    
  6. rm: It is used to remove files or directories.

     # Remove multiple directories recursively [syntax: rm -r <dir_name>]
     ubuntu@ip-172-31-93-81:~$ rm -r dir1 dir2 
     # Remove file and directory
     ubuntu@ip-172-31-93-81:~$ rm -rvf notes test