# Linux Commands

---

## File Management Commands

### Directory Navigation:

1. pwd - Get the full path of the current working directory
    
    ```bash
    pwd
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679332444085/e6724576-a6ea-4224-b8b8-78d8e544f137.png align="left")
    
2. cd -To change the directory from the current directory
    
    ```bash
    cd
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679332503787/7f4b8baf-2adc-49cc-9026-3382731b6a55.png align="left")
    
3. cd ~ -Navigate to the current users home directory
    
    ```bash
    cd ~
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679332595766/96390927-a766-45cc-b4b0-5237dc6fdcc2.png align="left")
    
4. cd . . -Navigate one step backward from the current directory
    
    ```bash
    cd ..
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679332751338/406b7e34-e46f-4672-a1e7-07581c613c82.png align="left")
    

---

### List files:

1. ls -l -List the files and directories in the current directory in Long Format
    
    ```bash
    ls -l
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679333952347/5a4bb6ce-99d7-4a1a-a408-635b02420326.png align="center")
    
2. ls -ld &lt;Directory Name&gt; - List contents of the specific directory
    
    ```bash
    ls -ld
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679334082454/10016d2d-43ee-4efe-a5cc-cb347c3abc24.png align="center")
    
3. ls -a - List all files including hidden files
    
    ```bash
    ls -a
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679334172363/51416515-0f48-4031-8a3a-57f9de3acdc4.png align="center")
    
4. ls -F -Shows the "\*" symbol at the end of the file which means its executable
    
    ```bash
    ls -F
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679334276339/d3e986f2-2db0-4809-bb67-50543b381bd0.png align="left")
    
5. ls -lt -List the files with the most recently modified files showing at the top
    
    ```bash
    ld -lt
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679334374280/8da7a255-d5d9-48ff-a5a6-c8c8a85a5e20.png align="left")
    
6. ls -lh -List the file size in a human-readable format
    
    ```bash
    ls -lh
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679334465247/b3572c08-0495-499d-9328-cfbb389d4672.png align="center")
    
7. tree -Generates a tree representation of the file system
    
    ```bash
    tree
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679334597417/efd56ab5-4d8e-4dfa-97d4-688cdf452cec.png align="left")
    

---

### File/directory create, copy and remove:

1. mkdir - It is used to create a directory
    
    ```bash
    mkdir new_directory
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679489393569/34fb537f-8fd4-4a07-8dee-c1742a609e7d.png align="left")
    
2. touch -It is used to create files in directories
    
    ```bash
    touch new_file
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679489527212/1909f8e8-f63e-4971-8f82-a961aaaf288d.png align="left")
    
3. cat -It is used to concatenate files and it is used to view file data
    
    ```bash
    cat new_file
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679489668203/f4440e3d-2888-4d66-b8d6-21f3515401b0.png align="left")
    
4. cp -r &lt;original\_directory/file&gt; &lt;Copied\_directory/file&gt; -It is used to copy files/directories recursively
    
    ```bash
    cp -r new_directory copied_directory
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679489871535/253614cd-9a43-409e-a3e7-352f120ba1a7.png align="center")
    
5. rm -r &lt;Directory/File name&gt; -It is used to remove files/directories recursively
    
    ```bash
    rm -r copied_directory
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679490091471/32448ad3-5056-4a64-85a5-2c179ee2a052.png align="left")
    
6. mv &lt;original\_directory/filename&gt; &lt;renamed\_directory/filename&gt; -It is used to rename the files
    
    ```bash
    mv new_directory renamed_directory
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679490367462/c8c62ab8-ab1c-4959-9b71-023860d22bac.png align="left")
    

---

### Modifying File Permissions:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679479888327/7815a924-a7b1-4e75-b729-f6b304249648.png align="center")

<table><tbody><tr><td colspan="1" rowspan="1"><p><strong>Sign</strong></p></td><td colspan="1" rowspan="1"><p><strong>Function</strong></p></td></tr><tr><td colspan="1" rowspan="1"><p>+</p></td><td colspan="1" rowspan="1"><p>To add Permissions for the selected file</p></td></tr><tr><td colspan="1" rowspan="1"><p>-</p></td><td colspan="1" rowspan="1"><p>To remove Permissions for the selected file</p></td></tr></tbody></table>

1. chmod o-x &lt;File\_name&gt; - To remove other user permission
    
    ```bash
    chmod o-x chmod_file
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679491726641/fa8eef05-35d3-47be-99e3-cf7e10f3a0b5.png align="left")
    
2. chmod o+x &lt;File\_name&gt; -To add other user permission
    
    ```bash
    chmod o+x chmod_file
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679491860763/9ff44bfe-970f-4123-a34b-e200d58c1d2f.png align="left")
    
3. chmod o=\_wx &lt;File\_name&gt; -To modify Other user permissions
    
    ```bash
    chmod o=wx chmod_file
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679494048968/d95767f8-d5bf-4ffb-91ea-d4e90bc3e81c.png align="left")
    
4. chmod 777 &lt;File\_name&gt; -To modify owner, group, and other permissions using octet numbers
    
    ```bash
    chmod 777 chmod_file
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679494201007/a6594423-109b-4ab3-ad65-2144adccc239.png align="left")
    
5. chmod 456 &lt;File\_name&gt;
    
    ```bash
    chmod 456 chmod_file
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679494315994/9c4a172e-8046-4973-8078-e9bac943b9b5.png align="left")
    

---

### Managing User:

1. sudo adduser &lt;username&gt; -To create a new user
    
    ```bash
    sudo adduser dummy_user
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679494517993/66130116-02a7-451a-b5e8-6830186d88e9.png align="left")
    
2. sudo passwd &lt;username&gt; -To change the password for the user
    
    ```bash
    sudo passwd dummy_user
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679494646737/68d9e654-9d96-401b-94f2-e52649ae1c13.png align="left")
    
3. su - &lt;username&gt; -To switch user
    
    ```bash
    su - dummy_user
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679494798660/826841a2-9cb9-4ddd-ae45-d9bdfced8a99.png align="left")
    
4. sudo addgroup &lt;groupname&gt; -To create a new group
    
    ```bash
    sudo addgroup linux
    ```
    
5. sudo adduser -g &lt;groupname&gt; &lt;username&gt; - To add user to a group
    
    ```bash
    sudo adduser -g linux dummy_user
    ```
    
6. sudo adduser -G &lt;groupname\_1&gt; &lt;groupname\_2&gt; &lt;username&gt; - To add user to multiple groups
    
    ```bash
    sudo adduser -G linux linux_2 dummy_user
    ```
    
7. sudo adduser -aG &lt;newgroup&gt; &lt;username&gt; - To append new group to the existing groups
    
    ```bash
    sudo adduser -aG linux_3 dummy_user
    ```
    
8. sudo adduser -G &lt;groupname&gt; &lt;username&gt; - To add user to group while creating the group
    
    ```bash
    sudo adduser -G Git dummy_user
    ```
    

---

### Change the Owner of the file:

1. chown &lt;username&gt;: &lt;groupname&gt; &lt;filename&gt; - To change ownership of the file
    
2. chown &lt;username&gt; &lt;filename&gt; -To change only user
    
3. chgrp &lt;groupname&gt; &lt;filename&gt; -To change only group of file
    

---

### Package Manager: apt - Advanced Package Tool

1. apt search &lt;packagename&gt; - To search for the given package
    
2. apt install &lt;packagename&gt; -To install a package
    
3. apt remove &lt;packagename&gt; - To remove uninstalled packages
    

---

### Editing Files in the CLI:

1. vim &lt;filename&gt; - To edit a file
    
2. Press "i" - After entering in the Vim editor you will be in command mode to insert/modify anything press key "i"
    
3. Press "esc" -To switch back to command mode
    
4. press "dd" -To delete a particular line
    
5. Press "d10" -To delete the following 10 lines
    
6. Press "A" -To jump to the end of the line and change to insert mode
    
7. Press "o" -To jump to the start of the line
    
8. Press "11G" -Go to the 11 line
    
9. Press "/" -To search for a pattern
    
10. press "n" -To jump to the next match
    
11. press "N" -To jump to the previous match
    
12. :wq -To write file to disk and quit Vim
    
13. :q! -To Quit Vim without saving the changes
    

---

### Pipe Command: |

Pipes the output of the previous command as an input to the following command

---

### grep: Global search for regular expressions and print out

1. history | grep sudo -Basically looks for any command that history provides as an output that has sudo word init
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679495634662/9906efbb-7ade-4b8c-b4e5-2d5ae8cee5e7.png align="center")
    
    ```bash
    history | grep sudo
    ```
    

---

### Redirection: &gt;

1. history | grep cd &gt; cd\_commands.txt -It takes the output from the previous command and stores it in a file that you define
    
    ```bash
    history | grep cd > cd_commands.txt
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679495958857/d01037c4-55fc-450e-a9b2-39b565c20753.png align="center")
    
2. history | grep cd &gt;&gt; cd\_commands.txt -It appends text to the end of the file
    
    ```bash
    history | grep cd >> cd_commands.txt
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679496279684/ca71764a-972e-49cb-96a9-24da7db402b1.png align="left")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679496228699/4dce74b6-7195-45c9-8df4-89d1029e8a78.png align="left")
    

---
