- Tram Ho
After a Tet holiday during the last pandemic. There are no more playing sessions to celebrate New Year. Stay home and give out the next number for you guys.
Let’s continue the series of 20% of command line most used. You can see the previous section here.
Below are the two commands that are most used when using files. Related permissions: chown – chmod.
Every file / directory in an Operating System like Linux, macOS (and every UNIX system in general) has the concept of owner-owner with that file / directory. Owner can do anything with that file, with full authority. The owner (or root user) can change the owner of the file / directory using the chown command:
chown <owner> <file>
When the owner has not been changed, the file below will be the owner and cannot be inputted.
Use chown with the owner of nghiemtuan
The above usage allows the owner of the directory, but only changes the owner of that directory. To change the owner of directories / sub-files in that directory use the -R flag
chown -R <owner> <file>
Files / directories not only have owners but also groups. Change the group by adding the group to convert: <group>
chown -R <owner>:<group> <file>
Or you can just change the group with the command:
chgrp <group> <filename>
Every file / directory in an operating system such as Linux, macOS (and all UNIX systems in general) has 3 permissions: Read, Write, Execute. Use the command ls -al or ll to see all information of the file / folder.
The weird strings -rw-rw-r– specify the file / directory permissions. Specifically:
- The first character has a known type: – is the file, d is the directory, l is the link
- Each next 3-character cluster (rw-): specifies the rights that can be executed to this file in order of 3 clusters representing owner – group – other rights. Each cluster is made up of 3 characters rwx (Read-Write-Execute).
To change this permission use the command
chmod, There are 2 ways to define permissions:
- Using symbolic arguments: syntax
<chuoi>is a combination of
a – all
u – user
g – group
o – others
- less permisstion
+ more permisstion
chmod a+r filename #everyone can now read
chmod a+rw filename #everyone can now read and write
chmod o-rwx filename #others (not the owner, not in group)
chmod og-r filename #other and group can't read any
- Using numeric arguments: use this method faster by using clusters of 3 numbers, each number representing each individual, maximum is 7 and minimum is 0. Where:
1 – Execute
2 – Write
4 – Read
=> There are types of combinations:
0 no permissions
1 can execute
2 can write
3 can write + execute (2 + 1)
4 can read
5 can read + execute (4 + 1)
6 can read + write (4 + 2)
7 can read, write and execute (1 + 2 + 4)
We will use 3-digit pairs to represent owner – group – other: eg
chmod 777 filename
chmod 755 filename
chmod 644 filename
When you are standing in a certain directory and creating a new file, then you do not need to define permissions first. Existing permissions, obtained from the parent folder’s permissions for the newly created file. To display information of the current permission if creating a file using the following command:
002. You try to take the 777 – 775 and see if there is anything special? oh. The parent directory (test) has rights to 775, so umask (the opposite of chmod) is to take 777 to subtract the current permissions. If confusing use additional arguments
-S don’t have to think.
This command will calculate the size of the current directory.
The number 8 here is the size of the directory in bytes.
du * to calculate the size of the files contained in the directory.
There are also other options such as:
du -m – MegaBytes units,
du -g – Units GigaBytes,
This command is used to get usage disk information. Its basic form will print information about mounted disks:
Use with option
-h to make it easier to read.
Basename & Dirname
Get the path information as name with the syntax:
Done. This is part 4 offline. I will back this series later. You welcome to read part 5 link here. Thank you for your interest.