Home Basic Linux Commands
Post
Cancel

Basic Linux Commands

  • Inside the linux terminal:-
    • the $ represents the normal user
    • the # represents the root or admin user
  • To check the hostname we can use the hostname command For Eg:-

    1
    2
    3
    4
    
      ```
      Input: hostname
      Output: {returns the hostname}
      ```
    
  • To check the current logged in user we can use the whoami command For Eg:-

    1
    2
    3
    4
    
      ```
      Input: whoami
      Output: {returns the current logged in user}
      ```
    
  • To check the IP on Linux we can use the ip addr command

    IP address is an unique address that identifies a device on the internet or a local network

    For Eg:-

    1
    2
    3
    4
    
      ```
      Input: ip addr   
      Output: {returns the ip address} 
      ```
    
  • To print out the working directory we can use the pwd command For Eg:-

    1
    2
    3
    4
    
      ```
      Input: pwd
      Output: {prints the working directory}
      ```
    
  • To make a folder on linux we can use the mkdir command For Eg:-

    1
    2
    3
    
      ```
      Input: mkdir {directory_name}
      ```
    
  • To change location or to move to another directory we can use the cd command For Eg:-

    1
    2
    3
    
      ```
      Input: cd {directory_name}
      ```
    
  • To move back to another directory we can use the cd command For Eg:-

    1
    2
    3
    
      ```
      Input: cd ..
      ```
    

    Here, the .. means that we should move one step back.

  • To clear the screen we use the clear command For Eg:-

    1
    2
    3
    
      ```
      Input: clear
      ```
    
  • To search for our folder/file inside a specific location we can use the findcommand
    • To search for folder For Eg:-

      1
      2
      3
      
        ```
        Input: find path -name {folder/directory name}
        ```
      
    • To search for file For Eg:-

      1
      2
      3
      
        ```
        Input: find . -type f -name {filename}
        ```
      

    We can also use the locate command for finding files or folders

  • To create a file we can use the touch command For Eg:-

    1
    2
    3
    
      ```
      Input: touch {file_name}
      ```
    
  • Removing a directory
    • To remove or delete a directory we can use the rmdir command For Eg:-

      1
      
        Input: rmdir {directory_name}
      
    • To remove the directory and all the other files inside of it. For Eg:-

      1
      
        Input: rm -r {directory_name}
      
  • To view more information about the files we can use the ls -ltr command For Eg:-

    1
    
      Input: ls -ltr
    

    Here, the -l means we use a long listing format. -t means we sort by time, newest first, and -r means in the reverse order while sorting.

    img

  • To view more information about the command we can use either the man command or the --help arguement For Eg:-

    1
    2
    3
    
      Input: ls --help 
              OR
      Input: man ls
    
  • To edit or write into a file we can use the vi editor. For Eg:-

    1
    
      Input: vi {file_name} 
    

    To start editing we need to go to insert mode for which we must press i key. Then we can insert the text as we like. To escape from the insert mode we can press the escape key. Now, to save the file, we can press :wq where wq means save and quit.

  • To print the file content into the shell we use cat command. For Eg:-

    1
    
      Input: cat {file_name}
    
  • To count the no.of words and lines we use the wc command.
    • To count only the no of lines we use wc -l For Eg:-

      1
      
        Input: wc -l {file_name}
      
  • To compare two files we use the diff command. For Eg:-

    1
    2
    3
    
      ```
      Input: diff {file_1} {file_2}
      ```
    
  • To compree and decompress files We use the tar do to the packaging of files. For Eg:-

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
      ```
      Input: tar {options} {tar_file_name} {file_1} {file_2}
      Eg: tar cvf files.tar file1 file2
      Output: files.tar
      ```   Now, we need to compress the files.tar using the `gzip` command    For Eg:-
    
      ```
      Input: gzip {tar_file}
      Eg:- gzip files.tar
      Output:  files.tar.gz
      ```
    

    Now, to decompress the compresseed file we use the gunzip command For Eg:-

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
      ```
      Input: gunzip {zipped_file}
      Eg:- gunzip gzip files.tar.gz
      Output: files.tar
      ```   And lastly, we need to untar the tar files   For Eg:-
    
      ```
      Input tar xvf {tar_file}
      Eg:- tar xvf files.tar
      Output: file1, file2s
      ```
    
  • To copy file rom one folder to another we use the cp command. For Eg:-

    1
    2
    
      Input: cp {source_file} {destination_path}
      Eg:- cp files.tar.gz  folder1/
    
  • To rename a file we use the mv command For Eg:-

    1
    2
    3
    4
    
      ```
      Input: mv {old_file_name} {new_file_name}
      ```
      Here, what we do is move the contents of the old file to the new file with a new name and then delete the old file. In linux, we use such indirect renaming of a file. 
    
  • To split and combine the files
    • To combine a file For Eg:- Let’s suppose we want to create a new file- filec with the contents of two different files: filea and fileb. To do this we use the > operator. For Example:-

      1
      2
      3
      
            ```
            Input: cat filea fileb > filec
            ```
      
    • To split a file For Eg:- Let’s suppose we want to split the content of a filea into two files fileb and filec then we use the split command. For Example:-

      1
      2
      3
      
        ```
        Input: split -l 1 filea
        ```
      
  • To search for words in a file and show them in a console we use the grep command For Example:-

    1
    
      Input cat {file_name} | grep {word}
    

    Here, the | is the pipe operator which is used to chain the operations. Here, the ‘cat {file_name}’ returns some output, and its output is sent as an input to the ‘grep {word}` command.

  • To read the start and end of the files we use the head and tail command respectively For Example:-

    1
    2
    3
    4
    5
    6
    
      ```
      Input: head -2 {filename}
      Output: prints the first two line from the file. 
      Input: tail -2 {filename}
      Output: prints the last two line from the file.
      ```
    
  • To sort the file we use the sort command For Example:-

    1
    
      Input: sort {file_name}
    
  • To prin only the unique value we use the uniq command For Example:-

    1
    
      Input: sort {file_name} | uniq
    
This post is licensed under CC BY 4.0 by the author.