Let's Learn more about Linux/Unix Commands
Tags: linux, ubuntu, bash, unix
Published: 2021-01-10 17:21:53

Hi there!, Lets learn few more about Linux/Unix commands.




**1.chmod:**
**chmod** stands for change mode. It's used to change access of a file. Linux has total 27 kinds of file permissions. listing few of them.

> Please don't apply this command outside of your home directory. If you will change access on *System Directory* with *sudo* command, you may have to reinstall your OS again.

**Syntax:**
```
chmod [references][operator][modes] file...
```
we can also say that, this syntax as **who**(user) as **reference**,**what**(what permission) as **operator**, **mode** as **which**(which permission).

**[reference]:** It represents that whom to give permission.

| Reference | Class   |	Description  |
| --------- |:-------:| ------------:|  
| u	    |  user   |	File’s owner |
| g	    |  group  |	Users who are Members of the file’s group| 
| o	    |  others | User who are neither the file’s owner nor members of the file’s group |
| a	    |  all    |	All three of the above, same as ugo |

**[operator]:** It represents what permission we are making. Are we adding or removing or setting new permission?

| Operator  | Description       |
| --------- |:----------------: |
| +	    | add the permission. |
| -	    | remove the permission. | 
| =	    | set the permission and wipe out previous permissions. | 

**[modes]:** It represents which permission to be granted.

| Modes | Names   | Description  |
| ----- |:-------:| ------------:|  
| r	|  read   | read permission to a file or a directory  |
| w     |  write  | write permission to a file or a directory | 
| x	|  execute| execute permission to a file |

**Example:**

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/114ogi7wrpjmi1v5tmut.PNG)

> Please see left sided values in above example ( like drwxrwxr-x or -rw-r--r-- etc), we called these symbolic value. These sections divided into 4 parts- File types, owner, group and other.

**Let's make this example simple to see what permission given to which file. I am taking few examples from the above screenshot.**

| File types | owner permission | group permission  | other permission  |  [hard link](https://en.wikipedia.org/wiki/Hard_link)| file/directory name
| ----- |:-----:| -----:|------:|-------:|--------------:|
| d     | rwx   | r-x   | r-x   | 2      | Desktop       |
| -	| rw-   | r--   | r--   | 1      | get-docker.sh |
| d     | rwx   | rwx   | r-x   | 2      | payal         |
| -     | r-x   | r--   | r--   | 1      | test.sh       | 


**File type:** It has two types
**d:** for directory and
**- :** for regular file.

**owner permission:** we can see different owner permissions. 
**rwx:** For Desktop & payal directories, the directory's owner can create files within it, can list its contents and descend into it.

**rw-:** For get-docker.sh file, the file owner can read from the file and write to the file but can't execute the file as "-" ( which means no permission) provided in execute position.
**r-x:** For test.sh file, the file owner can read from the file and execute the file.

**group permission:** we can see different group permission.
**r-x:** For Desktop directory, the member of directory's group can read and descend into it. 
**r--:** For get-docker.sh & test.sh files, the members of the file's group can read from the file but can't write and execute as "-" provided for both places.
**rwx:** For payal directory, the members of the directory's group can read, write and descend into it.

**other permission:** 
**r-x:** For Desktop and payal directories, the other users can read and descend into it.
**r--:** For get-docker.sh & test.sh files, other users can only read from the file .

**File Permission can also be given through octa values:**
 Let's take an example from above example for **Desktop** directory. 
```
 rwx  r-x  r-x
  u    g    o
```

| Permission| Binary| Octal | User | Group | Other |
| ---------:|:-----:|------:|-----:|------:|------:|  
|   r	    |  100  |  4    |  4   |  4    |  4    |
|   w       |  010  |  2    |  2   |  0    |  0    |
|   x	    |  001  |  1    |  1   |  1    |  1    |
|total number|      |       |  7   |  6    |  6    |
     
**Now we can change the permission using below ways:**
**1.** 
```
chmod u+rwx g+rx o+rx <file>
```
2.
```
chmod 766 <file>
```
So both of the commands will give same permission to the file.


**2. cal:** 
**cal** stands for calendar i.e. use to see calendar in terminal.

**Syntax:**
```
cal [-jy] [[month] year]
```
**-j:** Show julian dates (days one-based, numbered from January 1)
**-y:** Show calendar from the current year

**Example:**

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/e5w89uz8bfe4xmuvr7yg.PNG)

**Example:**

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ac6ha2rk3ado8ebxjg22.PNG)

**3. grep:**
**grep** stands for **Global Regular Expression Print**. It is used to search for a particular pattern of a character and print all the contain that pattern. 

**Syntax:**
```
 grep [OPTIONs]... PATTERN [FILE]..
```
We can use **grep** command in so many ways. Given few examples.

**grep [options] Pattern [file]:** Search the file with **Options**. There are so many options, I provided few as examples. 
   * **grep -c "string-to-search" [FILE] :** It find the number of lines that matches the pattern.

   **Example:**

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0od8yzewzsdzmk0gxoqs.PNG)

   * **grep -i "string-to-search" [FILE] :** It search string case insensitively. 

      **Example:**
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6hmwxygixo3bq1nmlycr.PNG)

**NOTE:**
Do **grep --help** to know more about **Options**.

**grep "string-to-search" [file] :** Search the word in a single file.

**Example:**

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/e50tnbtmajeye9m4qetb.PNG)

**grep "string-to-search" [file1] [file2] [file3] [file4]:** Search the word in a multiple files. 

**Example:**

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/oszvf2zcgcu6iy1m8rxp.PNG)

**[some-commands] | grep "string-to-search" :** Put some command and search.

**Example:**

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/m2b0dzrktoxjuwyyc26r.PNG)

**cat [file] | grep "string-to-search" :** While concatenate the file and search particular.
 
**Example:**

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/b3b05s1u62afd1wm3atk.PNG)

**4. zip:**
**zip** command used for compressed files with .zip extension. We can zip files and directories in linux/unix. zip files use in Linux for many reasons. Even we can share the zip file with Window users instead of *.tar* extension as it won't be compatible.

**Syntax:**

```
zip [OPTIONS] filename.zip [list of files]
```
* **zip filename.zip [file1] [file2] :** Creating **zip** with files:

**Example:**

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/aymrndrqyknk0jv8nvd1.PNG)

* **zip filename.zip [list of files] [list of directories] :** Creating **zip** with files and directories.

**Example:**

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gycr2fsjbrnxqma83ozw.PNG)

* **zip -e filename.zip [list of files] :** Creating password protecting zip.

**Example:**

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dh9nq969izplbgs4hdv0.PNG)

**5. unzip:**
**unzip** command used for unzipping the zip files.

**Syntax:**
```
unzip [OPTIONS] [filename.zip]
```

* **unzip [filename.zip] :** Unzipping the above created zip file combined with files and directories.

**Example:**

 ![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/g6ehd0ipqvzqsex3r3f2.PNG)

* **unzip -P [password] [filename.zip] :** Unzipping password protected zip file.

**Example:**

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mc6xi5ktow65haia0res.PNG)

**NOTE:** Providing password in command line is not secure and it should be avoided. We can overcome this situation just providing the below command without providing the *password*. So, if the zip file is *encrypted* then unzip command will prompt with window to enter the password.

``` 
unzip [filename.zip] 
```
**Example:**

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2jfb0sstn4wtv4h3hfa0.PNG)