Linux Basics commands  - Part 1

Linux Basics commands - Part 1

Part 1: Introduction to Basic Linux Commands

Setting Up Your Workspace

  1. Create a new directory named "devops":

     mkdir devops
    
  2. Navigate into the "devops" directory:

     cd devops
    
  3. Create a new file named "demofile.txt":

     touch demofile.txt
    
  4. List the contents of the directory to ensure "demofile.txt" is created:

     ls
    

    Output:

     demofile.txt
    
  5. Display the contents of "demofile.txt" (it should be empty):

     cat demofile.txt
    

Adding Text to a File

  1. Print a message to the terminal:

     echo "Hello Dosto"
    
  2. Write the message "Hello Dosto" to "demofile.txt":

     echo "Hello Dosto" > demofile.txt
    
  3. Verify the content of "demofile.txt":

     cat demofile.txt
    

    Output:

     Hello Dosto
    

Creating and Viewing Additional Files

  1. Create another file and add text:

     echo "Hello Dosto" > one.txt
    
  2. Display the contents of "one.txt":

    cat one.txt
    

    Output:

    Hello Dosto
    
  3. View the beginning of "one.txt":

    head one.txt
    
  4. View the end of "one.txt":

    tail one.txt
    
  5. Continuously monitor the end of "one.txt":

    tail -f one.txt
    

Using less and more Commands

  1. View "one.txt" with less:

    less one.txt
    
  2. View "one.txt" with more:

    more one.txt
    

Current Directory and File Management

  1. Display the current directory path:

    pwd
    

Copying Files and Directories

  1. Copy "one.txt" to the "devops" directory:

    cp one.txt devops/
    
  2. Copy "one.txt" within the main directory:

    cp devops/one.txt devops/copy/
    
  3. Copy a directory and its contents recursively:

    cp -r copy/ devops/
    

Moving and Renaming Files and Directories

  1. Move "demofile.txt" to the "copy" directory:

    mv demofile.txt copy/
    
  2. Rename the "Devops" directory to "linuxfordevops":

    mv Devops/ linuxfordevops
    

Listing and Counting

  1. List files in the current directory:

    ls
    
  2. Display the content of "one.txt":

    cat one.txt
    

    Output:

    Hello Dosto
    
  3. Word count of "one.txt":

    wc one.txt
    
  1. Create and add text to a new file:

    echo "hello this is softlink example." > devops-file.txt
    
  2. Display the content of "devops-file.txt":

    cat devops-file.txt
    
  3. Display the current directory path and copy it:

    pwd
    
  4. Create a soft link to "devops-file.txt":

    ln -s /home/dhruv/Devops/linuxfordevops/devops-file.txt softlink-file1
    
  5. List files in long format:

    ls -ltr
    
  6. Display the content of the soft link:

    cat softlink-file1
    
  1. Create and add text to another file:

    echo "this is harlink example" > devopsfile.txt
    
  2. List files to verify the creation:

    ls
    
  3. Display the content of "devopsfile.txt":

    cat devopsfile.txt
    
  4. Create a hard link to "devopsfile.txt":

    ln /home/dhruv/Devops/linuxfordevops/devopsfile.txt hardlink-file
    
  5. List files again to see the hard link:

    ls
    
  6. Display the content of the hard link:

    cat hardlink-file
    

Cutting and Sorting Content

  1. Cut and display the first character of "one.txt":

    cut -b 1 one.txt
    

    Output:

    H
    
  2. Cut and display the first four characters of "one.txt":

    cut -b 1-4 one.txt
    

    Output:

    Hell
    

Using tee

  1. Echo text and use tee to display and write to a file:

    echo "hey" | tee
    

    Output:

    hey
    
  2. Echo text and write to "hey.txt" using tee:

    echo "hey" | tee hey.txt
    

    Output:

    hey
    
  3. Display the content of "hey.txt":

    cat hey.txt
    

    Output:

    hey
    

Differences and Editing with vi

  1. Compare two files:

    diff one.txt hardlink-file
    
  2. Edit "one.txt" using vi:

    vi one.txt
    
    • Press i for insert mode.

    • Press Esc to exit insert mode.

    • Type :wq to write changes and quit.

  3. Sort the content of "one.txt":

    sort one.txt
    

Connecting to Remote Servers and System Monitoring

  1. Check disk space:

    df
    df -h
    
  2. Navigate to the parent directory and check disk usage:

    cd ..
    du .
    
  3. List all files, including hidden ones:

    ls -a
    

Managing Processes

  1. View active processes:

    ps
    top
    
  2. Find which process is using a file:

    fuser filename
    
  3. Kill a process by ID:

    kill -9 process_id
    

Memory and System Performance

  1. Check free memory:

    free
    free -h
    
  2. Run a command in the background and view the output later:

    nohup free -h
    ls
    cat nohup.out
    
  3. Display the first five lines of "nohup.out":

    head -n 5 nohup.out
    
  4. Check system performance with vmstat:

    vmstat
    vmstat -a
    

This guide covers essential Linux commands for beginners, offering practical examples and outputs to help you get started with basic file operations, linking, system monitoring, and process management.