Linux Filesystem and Directories
Please Select a Licence from the LICENCE_HEADERS page |
And place at top of your page |
If no Licence is Selected/Appended, Default will be CC0 Default Licence IF there is no Licence placed below this notice!
When you edit this page, you agree to release your contribution under the CC0 Licence LICENCE:
More information about the cc0 licence can be found here: You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission. Licence: Statement of Purpose The laws of most jurisdictions throughout the world automatically confer exclusive Copyright and Related Rights (defined below) upon the creator and subsequent owner(s) (each and all, an "owner") of an original work of authorship and/or a database (each, a "Work"). Certain owners wish to permanently relinquish those rights to a Work for the purpose of contributing to a commons of creative, cultural and scientific works ("Commons") that the public can reliably and without fear of later claims of infringement build upon, modify, incorporate in other works, reuse and redistribute as freely as possible in any form whatsoever and for any purposes, including without limitation commercial purposes. These owners may contribute to the Commons to promote the ideal of a free culture and the further production of creative, cultural and scientific works, or to gain reputation or greater distribution for their Work in part through the use and efforts of others. For these and/or other purposes and motivations, and without any expectation of additional consideration or compensation, the person associating CC0 with a Work (the "Affirmer"), to the extent that he or she is an owner of Copyright and Related Rights in the Work, voluntarily elects to apply CC0 to the Work and publicly distribute the Work under its terms, with knowledge of his or her Copyright and Related Rights in the Work and the meaning and intended legal effect of CC0 on those rights. 1. Copyright and Related Rights. A Work made available under CC0 may be protected by copyright and related or neighboring rights ("Copyright and Related Rights"). Copyright and Related Rights include, but are not limited to, the following: the right to reproduce, adapt, distribute, perform, display, communicate, and translate a Work; moral rights retained by the original author(s) and/or performer(s); publicity and privacy rights pertaining to a person's image or likeness depicted in a Work; rights protecting against unfair competition in regards to a Work, subject to the limitations in paragraph 4(a), below; rights protecting the extraction, dissemination, use and reuse of data in a Work; database rights (such as those arising under Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, and under any national implementation thereof, including any amended or successor version of such directive); and other similar, equivalent or corresponding rights throughout the world based on applicable law or treaty, and any national implementations thereof. 2. Waiver. To the greatest extent permitted by, but not in contravention of, applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and unconditionally waives, abandons, and surrenders all of Affirmer's Copyright and Related Rights and associated claims and causes of action, whether now known or unknown (including existing as well as future claims and causes of action), in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each member of the public at large and to the detriment of Affirmer's heirs and successors, fully intending that such Waiver shall not be subject to revocation, rescission, cancellation, termination, or any other legal or equitable action to disrupt the quiet enjoyment of the Work by the public as contemplated by Affirmer's express Statement of Purpose. 3. Public License Fallback. Should any part of the Waiver for any reason be judged legally invalid or ineffective under applicable law, then the Waiver shall be preserved to the maximum extent permitted taking into account Affirmer's express Statement of Purpose. In addition, to the extent the Waiver is so judged Affirmer hereby grants to each affected person a royalty-free, non transferable, non sublicensable, non exclusive, irrevocable and unconditional license to exercise Affirmer's Copyright and Related Rights in the Work (i) in all territories worldwide, (ii) for the maximum duration provided by applicable law or treaty (including future time extensions), (iii) in any current or future medium and for any number of copies, and (iv) for any purpose whatsoever, including without limitation commercial, advertising or promotional purposes (the "License"). The License shall be deemed effective as of the date CC0 was applied by Affirmer to the Work. Should any part of the License for any reason be judged legally invalid or ineffective under applicable law, such partial invalidity or ineffectiveness shall not invalidate the remainder of the License, and in such case Affirmer hereby affirms that he or she will not (i) exercise any of his or her remaining Copyright and Related Rights in the Work or (ii) assert any associated claims and causes of action with respect to the Work, in either case contrary to Affirmer's express Statement of Purpose. 4. Limitations and Disclaimers. No trademark or patent rights held by Affirmer are waived, abandoned, surrendered, licensed or otherwise affected by this document. Affirmer offers the Work as-is and makes no representations or warranties of any kind concerning the Work, express, implied, statutory or otherwise, including without limitation warranties of title, merchantability, fitness for a particular purpose, non infringement, or the absence of latent or other defects, accuracy, or the present or absence of errors, whether or not discoverable, all to the greatest extent permissible under applicable law. Affirmer disclaims responsibility for clearing rights of other persons that may apply to the Work or any use thereof, including without limitation any person's Copyright and Related Rights in the Work. Further, Affirmer disclaims responsibility for obtaining any necessary consents, permissions or other rights required for any use of the Work. Affirmer understands and acknowledges that Creative Commons is not a party to this document and has no duty or obligation with respect to this CC0 or use of the Work. |
Basic Linux Filesystem and Directories
Linux filesystem hierarchy
The Linux filesystem is organized in a hierarchical structure, similar to a tree. At the top of this tree is the root directory, denoted by a forward slash (/), and all other directories and files branch off from the root directory. Understanding the purpose of common directories in the Linux filesystem is essential for navigating and managing files effectively.
Here are some of the key directories in a typical Linux filesystem:
- / (Root): The root directory is the starting point of the filesystem hierarchy. All files and directories in the system are located under the root directory.
- /bin: This directory contains essential binary executables, such as basic commands used by all users, including the shell and common utilities like 'ls', 'cp', 'mv', and 'rm'.
- /sbin: The 'sbin' directory stores system binary executables, which are essential for system maintenance and administration tasks. These commands are generally used by the root user, for example, 'fdisk', 'ifconfig', and 'shutdown'.
- /boot: The 'boot' directory contains the files needed to boot the system, such as the Linux kernel, the initial RAM disk (initrd), and the bootloader configuration files (e.g., GRUB).
- /dev: The 'dev' directory contains device files that represent various hardware devices on the system, such as hard drives, printers, and terminals. In Linux, devices are treated as files, allowing users to interact with them using file manipulation commands.
- /etc: The 'etc' directory holds system-wide configuration files and scripts. These files are generally plain text and can be edited to configure various aspects of the system, such as network settings, user accounts, and installed services.
- /home: The 'home' directory contains individual user directories, where users store their personal files and settings. Each user has their own directory under '/home', named after their username (e.g., /home/johndoe).
- /lib, /lib64: These directories store shared library files required by the executables in '/bin' and '/sbin'. Libraries contain common code used by multiple programs, reducing duplication and conserving system resources.
- /media: The 'media' directory is used to mount removable media, such as USB flash drives, CDs, and DVDs. When you connect or insert removable media, the system typically creates a subdirectory under '/media' for accessing the contents.
- /mnt: The 'mnt' directory serves as a temporary mount point for filesystems, such as network drives or additional hard drives. Administrators can manually mount devices to this directory to access their contents.
- /opt: The 'opt' directory is designated for optional or third-party software installations that are not managed by the system's package manager. This directory helps keep manually installed software separate from the rest of the system.
- /proc: The 'proc' directory is a virtual filesystem that provides an interface to kernel data structures. It contains information about running processes and system resources, presented as files and directories.
- /root: The 'root' directory is the home directory for the root user. This directory is separate from '/home' to ensure the root user always has access to essential files, even if the '/home' partition becomes full or unavailable.
- /tmp: The 'tmp' directory is used to store temporary files created by the system and applications. These files are typically deleted upon system reboot or after a specified period.
- /usr: The 'usr' directory contains user-related programs, libraries, and documentation. It is further organized into subdirectories such as '/usr/bin', '/usr/sbin', '/usr/lib', and '/usr/share'. The '/usr' directory is usually read-only and is meant for system-wide files that are not frequently modified:
- /usr/bin: This directory holds non-essential user binary executables, such as command-line tools and applications. These files are accessible to all users on the system.
- /usr/sbin: Similar to '/usr/bin', this directory contains non-essential system binary executables, intended for system maintenance and administration tasks.
- /usr/lib, /usr/lib64: These directories store shared libraries used by the executables in '/usr/bin' and '/usr/sbin'.
- /usr/share: The 'usr/share' directory contains architecture-independent data, such as documentation, icons, fonts, and configuration files for various applications.
- /var: The 'var' directory is used to store variable data, such as logs, mail, and web content. It contains subdirectories for different types of data that may change frequently or grow in size over time:
- /var/log: This directory holds log files generated by various system processes and applications. Log files are essential for monitoring system activity and troubleshooting issues.
- /var/mail: The 'var/mail' directory stores user mailboxes, containing incoming and outgoing email messages for each user on the system.
- /var/spool: This directory is used to store queued or temporary files, such as print jobs waiting to be processed or emails waiting to be sent.
- /var/www: In systems running a web server, the 'var/www' directory typically serves as the default location for web content, such as HTML files, images, and scripts.
In summary, the Linux filesystem is organized into a hierarchical structure with specific directories serving distinct purposes. Familiarity with these directories and their functions is crucial for effective file management, system administration, and troubleshooting tasks on a Linux system.
File types and permissions
In Linux, files have types and permissions that determine their nature and dictate how users can interact with them. Understanding file types and permissions is crucial for maintaining the security and integrity of a Linux system.
File Types
Linux supports several file types, each with its specific purpose and structure:
- Regular files (indicated by '-'): These are the most common file type, including text files, images, executables, and data files. They can be read, written, or executed based on their permissions.
- Directories (indicated by 'd'): Directories are special files that serve as containers for other files and directories. They are used to organize the filesystem in a hierarchical structure.
- Symbolic links (indicated by 'l'): Symbolic links are pointers to other files or directories. They serve as shortcuts or aliases, allowing you to access the target file or directory without specifying its full path.
- Character device files (indicated by 'c'): These files represent devices, such as a mouse or keyboard, that transmit data one character at a time. They facilitate communication between the hardware and the system.
- Block device files (indicated by 'b'): Block device files represent devices that handle data in blocks, such as hard drives, CD-ROM drives, or USB storage devices.
- Sockets (indicated by 's'): Sockets are special files that facilitate inter-process communication (IPC) between processes on the same or different systems.
- Named pipes (indicated by 'p'): Named pipes, also known as FIFOs (first in, first out), are similar to sockets but only allow communication between processes on the same system.
File Permissions
In Linux, file permissions determine the level of access granted to the owner of the file, the group associated with the file, and other users. There are three basic permission types:
- Read (r): The read permission allows users to view the file's contents or list the contents of a directory.
- Write (w): The write permission allows users to modify a file's contents or create, delete, or rename files within a directory.
- Execute (x): The execute permission allows users to run a file as a program or script or to access the contents of a directory.
File permissions are displayed using a combination of letters and dashes (e.g., 'rw-r--r--'). The first set of permissions (rw-) applies to the owner of the file, the second set (r--) applies to the group, and the third set (r--) applies to other users.
- owner first set of permissions
- group second set of permissions
- other users third set of permissions
To view the permissions of files and directories, use the 'ls -l' command in the terminal. The output will show the file type and permissions, along with other information such as the owner, group, size, and modification date.
To change file permissions, use the 'chmod' command. Permissions can be modified using either alphanumeric notation (e.g., 'chmod u+x myfile') or octal notation (e.g., 'chmod 755 myfile'). The alphanumeric notation uses letters to represent the user (u), group (g), and other (o) categories, while the octal notation uses numbers (0-7) to represent the permissions.
In Linux, file and directory permissions are represented using a numerical system, with 0, 1, 2, and 4 being the base values. These numbers correspond to specific permissions:
- 0: No permission
- 1: Execute permission
- 2: Write permission
- 4: Read permission
To set multiple permissions, you can combine these base values. For example, if you want to grant both read and execute permissions, you would add their respective values (4 for read and 1 for execute) to get a combined value of 5.
Here are the possible combinations and their meanings:
- 0: No permissions (---)
- 1: Execute only (--x)
- 2: Write only (-w-)
- 3: Write and execute (-wx)
- 4: Read only (r--)
- 5: Read and execute (r-x)
- 6: Read and write (rw-)
- 7: Read, write, and execute (rwx)
These numerical permissions are applied in sets of three digits, representing the owner, group, and others (everyone else) permissions, respectively. For example, chmod 750 file would grant the following permissions:
- 7 (first digit): Owner has read (4), write (2), and execute (1) permissions (4+2+1=7)
- 5 (second digit): Group has read (4) and execute (1) permissions (4+1=5)
- 0 (third digit): Others have no permissions (0)
This would result in the following permissions: -rwxr-x---. The owner has full access, group members have read and execute access, and others have no access to the file.
four-digit number
When you see a four-digit number used with the chmod command, such as chmod 7777 file
, it includes an additional set of permissions known as the "setuid", "setgid", and "sticky" bits. These special permissions are represented by the first digit, while the remaining three digits represent the owner, group, and other permissions, respectively.
Here's a breakdown of the special permissions represented by the first digit:
- setuid (4): When the setuid (set user ID) bit is set on an executable file, the process runs with the effective user ID of the file owner rather than the user who executed the file. This is useful for allowing users to run certain programs with elevated privileges, such as the passwd command, which needs root access to modify the /etc/shadow file.
- setgid (2): When the setgid (set group ID) bit is set on an executable file, the process runs with the effective group ID of the file group rather than the user's primary group. This is helpful in shared environments where multiple users need to access the same files with a common group. Additionally, when setgid is set on a directory, newly created files and directories within that directory inherit the group ownership from the parent directory.
- sticky (1): The sticky bit, when set on a directory, restricts file deletion within that directory. Only the owner of the file, the owner of the directory, or a user with root privileges can delete a file with the sticky bit set. This is commonly used on shared directories, such as /tmp, to prevent users from accidentally or maliciously deleting files owned by other users.
In the case of chmod 7777 file, the permissions are as follows:
- 7 (first digit): Setuid (4), setgid (2), and sticky bit (1) are all set (4+2+1=7).
- 7 (second digit): The owner has read (4), write (2), and execute (1) permissions (4+2+1=7).
- 7 (third digit): The group has read (4), write (2), and execute (1) permissions (4+2+1=7).
- 7 (fourth digit): Other users have read (4), write (2), and execute (1) permissions (4+2+1=7).
It's important to note that setting permissions to 7777 is generally not recommended, as it grants all users full access to the file, including the ability to read, write, and execute it, and may also enable the setuid, setgid, and sticky bits, potentially leading to security vulnerabilities. Use caution when setting permissions and ensure you understand the implications of the values you assign.
In summary, file types and permissions in Linux play a crucial role in maintaining the security and organization of a system. Understanding these concepts allows administrators and users to manage access to files and directories effectively and protect sensitive data from unauthorized access.
Navigating directories using the command line is an essential skill when working with a Linux system. Here's an overview of some basic commands to help you navigate directories:
- pwd (Print Working Directory): This command displays the current working directory, which is the directory you are currently in.
$ pwd /home/username
- ls (List): This command lists the contents of the current directory or a specified directory.
$ ls Desktop Documents Downloads Music Pictures Videos
To list the contents of a specific directory, use ls followed by the directory path:
$ ls /var/www html logs
- cd (Change Directory): This command is used to change the current working directory. To navigate to a specific directory, use cd followed by the directory path:
$ cd Documents
To move up one directory level, use cd ..:
$ cd ..
To go to your home directory, simply use cd without any arguments:
$ cd
- mkdir (Make Directory): This command creates a new directory. To create a new directory, use mkdir followed by the directory name:
$ mkdir NewDirectory
- rmdir (Remove Directory): This command removes an empty directory. To remove an empty directory, use rmdir followed by the directory name:
$ rmdir NewDirectory
Note: If the directory is not empty, you will need to use rm -r to remove the directory and its contents:
$ rm -r DirectoryWithContents
By using these basic commands, you can navigate directories, view their contents, and create or remove directories using the command line in Linux. Practice these commands to familiarize yourself with the Linux file system and improve your command-line skills.
Manipulating files and directories
Manipulating files and directories is an important aspect of working with Linux. Here's an overview of basic commands to manage files and directories using the command line:
- touch (Create a new file): This command creates an empty file. To create a new file, use touch followed by the file name:
$ touch newfile.txt
- cp (Copy): This command copies a file or directory. To copy a file, use cp followed by the source file and the destination:
$ cp sourcefile.txt destinationfile.txt
To copy a directory and its contents, use the -r (recursive) option:
$ cp -r sourcedirectory destinationdirectory
- mv (Move/Rename): This command moves or renames a file or directory. To move a file, use mv followed by the source file and the destination:
$ mv sourcefile.txt destinationfile.txt
To rename a file, use mv followed by the old file name and the new file name:
$ mv oldname.txt newname.txt
- rm (Remove): This command removes a file or directory. To remove a file, use rm followed by the file name:
$ rm filetoremove.txt
To remove a directory and its contents, use the -r (recursive) option:
$ rm -r directorytoremove
- cat (Concatenate): This command displays the contents of a file or concatenates multiple files. To display the contents of a file, use cat followed by the file name:
$ cat file.txt
To concatenate multiple files, use cat followed by the file names:
$ cat file1.txt file2.txt > combined.txt
- ln (Link): This command creates a link (hard or symbolic) between files. To create a hard link, use ln followed by the source file and the link name:
$ ln sourcefile.txt linkname.txt
To create a symbolic link, use the -s option:
$ ln -s sourcefile.txt symlinkname.txt
ln
hard vs symbolic link:
In Linux, a link is a reference to a file or directory. There are two types of links: hard links and symbolic (or soft) links. Each type of link has its own characteristics and use cases.
Hard Links:
A hard link is a direct reference to the actual file or directory on the storage device. It essentially creates another name for the same file, sharing the same inode (a unique identifier that represents the file on the filesystem). Here are some key points about hard links:
- Hard links can only be created for files, not directories.
- Hard links must be on the same filesystem as the original file.
- Deleting a hard link doesn't affect the original file or other hard links to the file. The file is only deleted when all hard links to it are removed.
- A hard link doesn't occupy additional disk space, as it points directly to the same data as the original file.
Symbolic (Soft) Links:
A symbolic link, also known as a soft link, is an indirect reference to a file or directory. Instead of pointing to the actual file data, it points to the file's pathname. Here are some key points about symbolic links:
- Symbolic links can be created for both files and directories.
- Symbolic links can span across different filesystems.
- Deleting a symbolic link doesn't affect the original file or directory, but if the target is removed, the symbolic link becomes a "dangling" or "broken" link, pointing to a non-existent file.
- A symbolic link occupies a small amount of disk space, as it stores the target file's pathname as a separate entity.
In summary, hard links are direct references to file data and can only be created for files within the same filesystem, while symbolic links are indirect references that can be created for both files and directories across different filesystems. Hard links are generally more efficient in terms of disk space usage, but symbolic links offer more flexibility and can be used in a wider range of scenarios.
These commands provide the basic functionality to manipulate files and directories in Linux. Familiarize yourself with these commands to improve your efficiency and productivity when working with the Linux command line.
File searching and comparisons
File searching and comparisons are essential tasks when working with files and directories in a Linux environment. They help you find specific files or directories based on certain criteria or compare the content of two or more files. Here's an overview of some commonly used commands for file searching and comparisons:
Find
- find: The find command is a powerful tool for searching files and directories based on various criteria, such as name, size, modification time, and type. The basic syntax is:
find [path] [expression]
For example, to find all files with the .txt extension in the current directory and its subdirectories, you would use:
find . -name "*.txt"
- locate: The locate command searches for files and directories based on their names using a pre-built database. It is generally faster than find but may not have the most up-to-date information. The basic syntax is:
locate [options] pattern
For example, to find all files and directories with the word "example" in their name:
locate example
Grep
- grep: The grep command searches for a specific pattern in the content of one or more files. It's a versatile tool for filtering and searching text. The basic syntax is:
grep [options] pattern [files]
For example, to search for the word "error" in all log files in the current directory:
grep 'error' *.log
Search through files in recursive directories to find a string of text.
grep -SWITCHES "TEXT2FIND" /PATH/TO/DIRECTORY
Example:
grep -iRl "text" ./
- -i - Ignore case distinctions in patterns and input data, so that characters that differ only in case match each other.
- -R - Read all files under each directory, recursively. Follow all symbolic links, unlike -r.
- -l - Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match.
- -n - return/display the line number.
- -w - Select only those lines containing matches that form whole words.
More about switches can be found in man grep
Diff
- diff: The diff command is used to compare the content of two files line by line, showing the differences between them. It's useful for identifying changes between file versions or finding discrepancies. The basic syntax is:
diff [options] file1 file2
For example, to compare the content of file1.txt and file2.txt:
diff file1.txt file2.txt
Comm
- comm: The comm command is another tool for comparing two sorted files, showing the lines that are unique to each file and the lines that are common to both. The basic syntax is:
comm [options] file1 file2
For example, to compare the content of two sorted text files file1.txt and file2.txt:
comm file1.txt file2.txt
These commands are just the tip of the iceberg when it comes to file searching and comparisons in Linux. There are many more options and tools available to help you find and manage your files efficiently.