The ls command with the - l parameter is used to check the file/folder permissions.
Types of Permissions:
Read (r) – The user’s capability to read the file content. The user, with reading permission, can see the content of a file, but they cannot modify it.
Write (w) – The user’s capability to write the file content. The user can modify the file or contents of the directory.
Execute (x)- The user can run/execute the file with the execute permission.
Syntex:
ls –l [file_name]/[folder_name]
Let's check the permissions of a file named new.txt.
Example:
ls -l new.txt
Output:
-rw-rw-r-- 1 user user 6 Oct 29 2021 new.txt
-> The first character - indicates that nex.txt is a file, not a directory.
-> The rw-rw-r-- indicates the permissions of the new.txt file.
In the example, the user is the owner of the nex.txt file, and user is the file's group.
-> The rw- indicates the permissions for the user. The user has permission to read and write the nex.txt file.
The next indicates characters rw- show permissions for the group, and have the permission to read and write the nex.txt file.
The last three characters r-- indicates permissions for the other users and group.
The rw- indicates the permissions for the user. The user has permission to read and write the nex.txt file.
Let's check the permissions of a directory named test.
Exmple: ls -l test
=> The chown command is used to change the ownership of a file in Linux.
In Linux, all files and directories are associated with an owner and a group.
The root user can change the user and group ownership for all the files. However, users can change the file group only if they own the file and only to a group they are a member of.
Syntex:
chown USER FILE(s)
USER – Username of the new owner of a file.
FILE – Name of file for which you are willing to change ownership.
We can see the ls -l command to find out who owns a file or what group the file belongs to.
Example: ls -l new.txt
=> Let's Change the Owner of a new.txt file.
Example:
user@server:/home/user$ chown trainee1 new.txt
The above command will change the ownership of a file new.txt from the user to the user trainee1.
=> Let's change the ownership of multiple files.
Example:
user@server:/home/user$ chown test sample2 sample3
In the above example test will be the new owner of files sample2 and sample3.
=> Change the Group of a File.
To change the group of a file, we will use the chown command followed by a colon (:) and the new group name (with no space between them) and the target file as an argument:
Syntex: chown :GROUP FILE
Example:
user@server:/home/user$ chown :test new.txt
The above command will change the group of the new.txt file from user to test.