File structure, File permission and important Symbols,

File structure, File permission and important Symbols,

·

7 min read

File Structure in Linux

In Linux, the file structure follows a hierarchical directory structure known as the Filesystem Hierarchy Standard (FHS). This standard defines the directory structure and the arrangement of files and directories in a Linux system.

The root directory ("/") is the top-most directory in the file system hierarchy. All other directories and files in the system are located under the root directory. Here's a breakdown of some of the important directories in the Linux file system:

  • /bin: This directory contains executable binary files that are essential for the system to function properly, such as system utilities like ls, cp, mv, and others.

  • /sbin: Similar to /bin, this directory contains executable binary files that are essential for system administration, such as system daemons and services.

  • /usr: This directory contains user-related files and programs such as applications, libraries, documentation, and source code.

  • /etc: This directory contains configuration files for the system and installed applications.

  • /var: This directory contains variable data files that are expected to grow over time, such as logs, spool files, and temporary files.

  • /home: This directory contains home directories for all users on the system.

  • /root: This is the home directory for the root user.

Still, we have more directories but the above directories are important to know.

Now let's move to our next topic.

File permission

File permissions control who can read, write, and execute files on a system. There are three levels of permissions: owner, group, and others.

The file permissions are represented by a string of 10 characters. The first character represents the file type (e.g., "- " for a regular file, "d" for a directory), while the remaining nine characters represent the permissions for the owner, group, and others.

The permissions are divided into three parts: read, write, and execute.

The read permission (represented by "r") allows a user to view the contents of a file or directory.

The write permission (represented by "w") allows a user to modify or delete a file or directory.

The execute permission (represented by "x") allows a user to execute a file or enter a directory.

They also have absolute values and you can use these values instead of rwx refer below table.

Symbolic

Mode

Absolute Mode

r

-read

4

w

-write

2

x

-execute

1

(-)

Null

0

If you run below command

$ ls -ltr

You will the below result.

-rw-r--r-- 1 root root 5185 Mar 25 14:51 tet.txt
drwxr-xr-x 3 saif saif 4096 Mar 19 00:30 Documents

In this example, the first character "-" indicates that it is a regular file. The next three characters "rw-" represent the permissions for the owner (read, write, Null), the following three characters "r--" represent the permissions for the group (read only), and the last three characters "r--" represent the permissions for others (read only). The number "1" represents the number of hard links to the file, "root" is the name of the owner, "root" is the name of the group, "5185" is the size of the file in bytes, and "Mar 25 14:51" is the date and time the file was last modified.

To modify file permissions, you can use the "chmod" command followed by the permission string and the file name. For example, to give the owner of a file read, write, and execute permissions, you can use the following command:

chmod u+rwx tet.txt   //user permission change
chmod g+rwx tet.txt  //group permission change
chmod o+rwx tet.txt  //other permission change

You also can change the ownership of the user,group, other with the help of the below set of commands.

  • "chown" is used to change the ownership permission of a file or directory.

  • chgrp" is used to change the group permission of a file or directory.

  • "chmod" is used to change the other user's permissions of a file or directory.

Here is my current file ownership.
$ ls -ltr tet.txt
-rw-r--r-- 1 root root 5185 Mar 25 14:51 tet.txt

Now let's do practical with "chown"

$ sudo chown saif tet.txt
saif@saif-HP-Pavilion-g6-Notebook-PC:~$ ls -ltr tet.txt
-rw-r--r-- 1 saif root 5185 Mar 25 14:51 tet.txt 
// Here you can see user has changed from root to saif

Let's Practice "chgrp" command now.

$ sudo chgrp saif tet.txt
saif@saif-HP-Pavilion-g6-Notebook-PC:~$ ls -ltr tet.txt
-rw-r--r-- 1 saif saif 5185 Mar 25 14:51 tet.txt
// Here you can see group has changed from root to saif

The last permission is "other" use by "chmod" command

sudo chmod 751 tet.txt 
saif@saif-HP-Pavilion-g6-Notebook-PC:~$ ls -ltr tet.txt
-rwxr-x--x 1 saif saif 5185 Mar 25 14:51 tet.txt
// Here you can see other user have just 1(excute) permission.

Now jump to our next topic.

Important Symbols in Linux

Dot (.)Represents the current directory in the filesystem.
Dot-dot (..)Represents one level above the current directory.
Forward slash (/)Represents the "root" of the filesystem. (Every directory/file in the Linux filesystem is nested under the root/directory.)
Tilde (~)Represents the home directory of the currently logged-in user.
The dash (-)Navigates back to the previous working directory, similar to how you can navigate to your user home directory with ~. If you need to go back to our deeply nested directory under your user home directory
*A symbol stands for "everything". Let's say you want to remove all the .jpg files from your Downloads folder which have their name starting with the "E" character, then you can use this symbol to represent all the other letters except E. See the example.
&Run a command in the background. It will return the PID of the new running process to you and won't show you the output.
&&These symbols are written together to stand for "and". So if you want to run 2 commands together, you can use it.
\Allows you to continue writing commands/Bash syntax in the new line.
#Everything after this symbol in the same line is considered to be a comment, so it won't be processed by the shell.
>Take the output of a command and redirect it into a file (will overwrite the whole file).
<Read the contents of a file into the input of a command.
>>Append a text or a command output into the last line of a file.

Here I have a bonus topic that is not mentioned in the title.

Access control list

ACL stands for Access Control List, which is a feature in Linux that provides a more fine-grained access control mechanism for files and directories beyond the traditional UNIX file permissions.

ACLs allow you to grant or restrict access to a file or directory to specific users and groups beyond the file's owner and group. With ACLs, you can assign different permissions to different users and groups, giving you greater control over who can access and modify files and directories.

In Linux, ACLs are implemented using the "setfacl" and "getfacl" commands. The "setfacl" command is used to set or modify the ACLs for a file or directory, while the "getfacl" command is used to display the current ACLs for a file or directory.

Here are the examples.

setfacl -m u:jane:rwx myfile.txt

In this example, the "setfacl" command is used to grant "jane" read, write, and execute permissions to "myfile.txt". The "-m" option specifies that we want to modify the ACL for the file, "u:jane" specifies that we want to modify the ACL for the user "jane", and "rwx" specifies the permissions we want to grant.

Let's say you want to view the ACLs for a file called important_file.txt. You can use the following command:

getfacl important_file.txt

This command will display the ACLs for important_file.txt, including the permissions assigned to the owner, group, and any additional users or groups.

# file: important_file.txt
# owner: user1
# group: group1
user::rwx
group::r-x
other::r--
user:user2:rwx
group:group2:r-x

This output shows that the owner of important_file.txt has read, write, and execute permissions, the group has read and execute permissions, and others have read-only access.

Additionally, the ACLs also show that user2 has read, write, and execute permissions, and group2 has read and execute permissions.

By using the getfacl command, you can quickly and easily view the ACLs for any file or directory in Linux, allowing you to better manage access control for your system.

Thank you

Happy Learning

Saif Ali