# Command Line Interface

The command line interface is one of the way to interact with the operating system using special keywords known as commands. By using the commands one can perform all the operations needed skipping the Graphical User Interface.

The interface is provided by a software known as shell. Most used shell is bash.

Common Commands to get started with CLI are as follows:

1. **touch** : This command creates new file in the current directory. Another way of creating file would be by specifying path "touch /folder1/folder2/fileName.txt".
    
    ```bash
    touch fileName.txt
    ```
    
2. **mkdir** : This command creates new directory or folder in the current path unless a specific path is followed by the command.
    
    ```bash
    mkdir newFolder
    ```
    
3. **cd** : This command stands for change directory. Used to navigate between the files and folders.
    
    ```bash
    # change to existing file
    cd Document
    ```
    
    ```bash
    # change directory to specfic path
    cd /path/to/directory
    ```
    
    ```bash
    # change directory to home directory
    cd
    ```
    
    ```bash
    #change directory to parent
    cd ..
    ```
    
4. **mv** : This command is used to move the files. This can be also used to rename files.
    
    ```bash
    mv fileName /destination/path
    ```
    
    ```bash
    mv fileName newFileName
    ```
    
5. **cp** : This command is used to copy files from source path to destination.
    
    * \-r : This option allows to copy recursively all the files.
        
    * \-a : This option allows to preserve the attributes and permissions of the copied files.
        
    * \-f : This option skips the prompts and overwrites the files if they exist.
        
    * \-i : This option prompts if the files exist.
        
    
    ```bash
    cp fileTobeCopied /destination/path
    ```
    
6. **ls** : This command is used to list directory content.
    
    * \-r : reverse sort files.
        
    * \-a : list all files including hidden files.cp fileTobeCopied /destination/path ls : This command is used to list directory content.
        
    * \-t : sort files.
        
    * \-h : human readable format.
        
    * \-l : long list format.
        
    
    ```bash
    ls -altrh # combination of all flags
    ```
    
7. **pwd** : It stands for print working directory.This command displays current path.
    
    ```bash
    pwd
    ```
    
8. **rm** : This command is used to remove files
    
    * \-r : recursively remove all the files.
        
    * \-f : forcefully remove all the files without prompting for permission of the owner.
        
    * \-i : prompts before removing each file.
        
    
    ```bash
    rm fileName
    ```
    
9. **chmod** : 'chmod' stands for change mode. This command is used to modify the file permissions. This can be done numerically or by specifying particular change of permission.
    
    * Numeric : The sum of the values of permissions is the given to owner, others, group form a 3 digit numeric code.
        
        * Read (r) has value 4.
            
        * Write (w) has value 2.
            
        * Execute (x) has value 1.
            
        
        ```bash
        chmod 755 fileName
        # owner = 7 => r=4 + w=2 + x=1
        # group = 5 => r=4 + x=1
        # others = 5 => r=4 + x=1
        ```
        
    * Symbolic : Each symbol is specified with + or - symbols to add or remove the permissions.
        
        ```bash
        chmod u=rwx,g=r,o=r fileName
        chmod +r fileName
        chmod -x fileName
        ```
        
10. **chown** : This command is used to change the owner.
    
    ```bash
    chown newOwnerName fileName
    ```
    
11. **cat** : This command is used to view the file contents.
    
    ```bash
    cat fileName
    ```
    
12. **grep**: This command is used to search for patterns in a file.
    
    * \-o : displays pattern if found in new line each time.
        
    * \-w : finds the exact pattern.
        
    * \-i : ignores cases while searching.
        
    * \-c : counts number of lines containing the pattern.
        
    
    ```bash
    grep "patternToSearch" fileName
    ```
    
13. **find** : This command is used to find files in the directory specified or the working directory.
    
    ```bash
    find /path/to/search/ -name "fileName.txt"  # search by name
    find /path/to/search/ -name "*.txt"  # search by extension
    find /path/to/search/ -type d # finds the directories only
    find /path/to/search/ -type f # finds files only
    ```
    
14. **wc :** This command is used to count words. It stands for word count.
    
    ```bash
    wc -l fileName # counts lines in the file
    wc -c fileName # counts bytes in the file
    wc -w fileName # counts words in the file
    ```
    
15. **sort** : This commands sorts alphabetically
    
    ```bash
    sort fileName
    sort -n fileName # sorts numerically
    sort -r fileName # sorts in reverse
    ```
    
16. **nano** : This opens the specified file in the editor
    
    ```bash
    nano fileName
    ```
    
17. **awk :** Used to manipulate and analyse data files. Usually piped with other command.
    
    ```bash
    # This prints line if the line starts with the word Harry
    awk '$1 == "Harry" { print $0 }' data.txt 
    ```
    
18. **sed** : 'sed' stands for stream editor. This command is used as a text processing tool.
    
    ```bash
    sed 's/old/new/' file.txt # substitutes old with new in file
    sed -n '/pattern/p' file.txt # prints lines if pattern is found
    sed '10,20d' file.txt # deletes lines from 10 to 20 from file
    sed '10,20p' file.txt # prints lines from 10 to 20 from file
    ```
    
19. **ping** : Used to test reachability of the host.
    
    ```bash
    ping -c 5 www.example.com  
    ```
    
20. **ps** : 'ps' is a versatile tool for monitoring and managing processes.
    
    ```bash
    ps # lists all the processes
    ps -aux # full info about the process
    ps -o pid,ppid,user,cmd # custom output of process list
    ```
