`
Aspect | Kernel | Shell |
---|---|---|
Role | Core part of the OS, manages hardware and resources | User interface for interacting with the OS (GUI OR CLI) |
Primary Function | Resource management, process management, security | Command execution, scripting, user interaction |
Interaction | Directly interacts with hardware | Interacts with users, translates commands to OS actions |
Examples | Linux Kernel, Windows NT Kernel, macOS XNU Kernel | Bash, Windows Command Prompt |
Directory | Description |
---|---|
/ |
The root directory. The top level of the file system. |
/bin |
Essential user command binaries needed for single-user mode and for system repair. |
/boot |
Contains the Linux kernel, initial RAM disk image (for drivers needed at boot time), and boot loader configuration files. |
/dev |
Contains device files that represent hardware components. |
/etc |
Configuration files for the system and installed applications. |
/home |
User home directories, where each user stores their personal files and directories. |
/lib |
Essential shared libraries and kernel modules needed for booting and running the system. |
/media |
Mount points for removable media such as USB drives and CDs. |
/mnt |
Temporary mount points for filesystems, typically used for manual mounts. |
/opt |
Optional application software packages. example homebrew |
/proc |
Virtual filesystem providing information about system processes and other kernel information. |
/root |
Home directory for the root user. |
/sbin |
System binaries (administration tools) for the superuser. |
/tmp |
Temporary files created by users and applications. |
/var |
Variable data files like logs, databases, and mail spools. |
ls -l
CommandThe ls -l
command in Unix/Linux systems lists files and directories in a long format, providing detailed information about each item. Here’s an explanation of the output structure:
-rw-r--r-- 1 user group 1234 Jul 29 10:00 example.txt
-rw-r--r--
):
-
: The first character indicates the file type.
-
: Regular filed
: Directoryrw-r--r--
: The next nine characters represent the file permissions, divided into three sets of three characters each.
rw-
) is for the owner.
r
: Read permissionw
: Write permission-
: No execute permissionr--
) is for the group.
r
: Read permission-
: No write permission-
: No execute permissionr--
) is for others (everyone else / others).
r
: Read permission-
: No write permission-
: No execute permission1
):
user
):
group
):
1234
):
Jul 29 10:00
):
Mon Day HH:MM
or Mon Day Year
for older files (more than six months old).example.txt
):
ls -l
OutputLet’s break down another example:
drwxr-xr-x 2 riteshSir staff 4096 Jul 28 15:32 documents
d
: This is a directory.rwxr-xr-x
: The permissions are:
rwx
(owner): The owner (riteshSir
) has read, write, and execute permissions.r-x
(group): The group (staff
) has read and execute permissions.r-x
(others): Others have read and execute permissions.2
: There are two hard links to this directory (itself and its parent).alice
: The owner of the directory is riteshSir
.staff
: The group associated with the directory is staff
.4096
: The size of the directory (often 4096 bytes on many filesystems).Jul 28 15:32
: The directory was last modified on July 28 at 15:32.documents
: The name of the directory.ls -l
Output StructureField | Description |
---|---|
File Type and Permissions | Indicates file type and access permissions |
Number of Hard Links | Count of hard links to the file/directory |
Owner | Username of the file’s owner |
Group | Group name associated with the file |
File Size | Size of the file in bytes |
Modification Timestamp | Date and time of last modification |
File Name | Name of the file or directory |
The ls -l
command provides a comprehensive overview of files and directories, making it easier to manage and understand their attributes.
chmod
vs chgrp
vs chown
chmod
, chown
, and chgrp
are Unix/Linux commands used for managing file and directory permissions and ownership. Here’s a detailed comparison and explanation of each:
chmod
(Change Mode)chmod
is used to change the permissions of a file or directory.chmod [options] mode file
chmod u+x filename
After this command, if the original permissions were -rw-r--r--
, they will become -rwxr--r--
.
chmod g-w filename
After this command, if the original permissions were -rw-rw-r--
, they will become -rw-r--r--
.
chmod 755 filename
This sets the permissions to -rwxr-xr-x
.
u
(user/owner), g
(group), o
(others), a
(all)r
(read = 4), w
(write = 2), x
(execute = 1)chown
(Change Owner)chown
is used to change the ownership of a file or directory.chown [options] owner[:group] file
gauravSir
:
chown gauravSir filename
gauravSir
and group to kaksha
:
chown gauravSir:kaksha filename
chgrp
):
chown :gauravSir filename
chgrp
(Change Group)chgrp
is used to change the group ownership of a file or directory.chgrp [options] group file
UCA
:
chgrp uca filename
Command | Purpose | Affects | Syntax Example |
---|---|---|---|
chmod |
Change file/directory permissions | Permissions | chmod 755 filename |
chown |
Change file/directory owner | Owner (and optionally group) | chown gaurav:kaksha filename |
chgrp |
Change file/directory group | Group | chgrp uca filename |
chmod
: Focuses on setting the permissions (read, write, execute) for the owner, group, and others.chown
: Used to change the owner of a file/directory, optionally also changing the group.chgrp
: Specifically changes the group ownership of a file/directory.chmod
to set the appropriate permissions and chown
/chgrp
to ensure the correct ownership.chown
.chgrp
to change the group ownership of files and directories so that the new team can access them.Calculating file permissions involves understanding the numeric and symbolic representations of the read, write, and execute permissions for the owner, group, and others.
Each permission (read, write, execute) is represented by a specific number:
r
): 4w
): 2x
): 1Permissions are often combined to form a total value for each of the three groups: owner, group, and others. Here are the combinations:
---
): 0--x
): 1-w-
): 2-wx
): 3 (2 + 1)r--
): 4r-x
): 5 (4 + 1)rw-
): 6 (4 + 2)rwx
): 7 (4 + 2 + 1)Let’s calculate the permissions for a file with the following symbolic permissions: -rw-r--r--
rw-
): Read and write
r
): 4w
): 2r--
): Read only
r
): 4r--
): Read only
r
): 4So, the numeric representation of -rw-r--r--
is 644
.
chmod
You can use the chmod
command to set these permissions:
chmod 644 filename
Let’s calculate another example with rwxr-xr--
:
rwx
): Read, write, and execute
r
): 4w
): 2x
): 1r-x
): Read and execute
r
): 4x
): 1r--
): Read only
r
): 4So, the numeric representation of rwxr-xr--
is 754
.
Certainly! The symbolic mode of the chmod
command allows you to change file and directory permissions using a combination of letters and symbols. Here’s a detailed explanation:
u
: User (the file’s owner)g
: Group (the group associated with the file)o
: Others (everyone else)a
: All (user, group, and others)+
: Add the specified permissions.-
: Remove the specified permissions.=
: Set the specified permissions, replacing the existing ones.r
: Read permissionw
: Write permissionx
: Execute permissionchmod [user][operator][permissions] filename
Adding Permissions:
chmod u+rw filename
This command grants read and write permissions to the owner of the file while leaving other permissions unchanged.
chmod g+x filename
This command adds execute permission for the group.
chmod a+r filename
This command adds read permission for the owner, group, and others.
Removing Permissions:
chmod g-w filename
This command removes write permission from the group.
chmod o-x filename
This command removes execute permission for others.
chmod g= filename
This command removes all permissions (read, write, execute) for the group, effectively leaving the group with no permissions.
Setting Permissions Explicitly:
chmod u=rx,g=r filename
This command sets the permissions to:
chmod u=rwx,g=rx,o=rx filename
This command sets the permissions to:
Command | Meaning | Example |
---|---|---|
u+rw |
Add read and write permissions for the owner | chmod u+rw file.txt |
g-x |
Remove execute permission for the group | chmod g-x file.txt |
o=r |
Set read-only permissions for others | chmod o=r file.txt |
a+rx |
Add read and execute permissions for everyone | chmod a+rx file.txt |
u=rwx,g=rx |
Set read, write, and execute for owner; read and execute for group | chmod u=rwx,g=rx file.txt |
Understanding the default permissions for new file creation and how umask
affects them is crucial for managing file security and access control in Unix/Linux systems.
When you create a new file, the system assigns it a default set of permissions. By default, these permissions are typically:
666
(read and write for everyone)777
(read, write, and execute for everyone)However, these default permissions are modified by the umask
value.
umask
?The umask
(user file creation mask) is a system setting that determines which permissions are removed from the default permissions when a new file or directory is created. It essentially defines what permissions should be “masked” out.
umask
To determine the final permissions:
666
777
umask
Value:
umask
is a mask value that removes permissions. For example, a umask
of 022
removes write permissions for the group and others.umask 022
666
umask
022
:
g-w
).o-w
).Calculation:
666
(default permissions) - 022
(umask) = 644
Resulting File Permissions: 644
(rw-r–r–)
umask 027
666
umask
027
:
g-w
).o-wx
).Calculation:
666
- 027
= 639
(converted to valid permissions)Resulting File Permissions: 640
(rw-r—–)
umask 077
666
umask
077
:
Calculation:
666
- 077
= 600
Resulting File Permissions: 600
(rw——-)
umask
To see the current umask
value, use the umask
command:
umask