Linux Crash Course

Day 1: Introduction to Linux

Topics:

  1. Linux operating system
    Explanation: Learn about the Linux operating system, its features, and its popularity among developers and system administrators.
  2. Basic Linux commands
    Explanation: Understand essential Linux commands such as ls (list files and directories), cd (change directory), pwd (print working directory), and mkdir (make directory).
  3. File and directory management
    Explanation: Learn how to create, delete, move, and rename files and directories using commands like touch, rm, mv, and cp.
  4. File permissions and ownership
    Explanation: Understand the concept of file permissions and how to set permissions using chmod, as well as changing file ownership using chown.
  5. File manipulation and text editing
    Explanation: Explore commands like cat (concatenate and display file contents), less (view file contents page by page), and nano (a basic text editor).

Assignments:

  1. Create a directory named “my_files” and navigate into it.
    Answer: mkdir my_files (to create the directory) and cd my_files (to navigate into it).
  2. Create a file named “my_text.txt” and write some text into it.
    Answer: touch my_text.txt (to create the file) and nano my_text.txt (to open the file in the nano text editor and write the text).
  3. List all the files and directories in the current directory.
    Answer: ls (to list files and directories).
  4. Rename the file “my_text.txt” to “new_text.txt”.
    Answer: mv my_text.txt new_text.txt (to rename the file).
  5. Delete the directory “my_files” and all its contents.
    Answer: rm -r my_files (to remove the directory and its contents).

Day 2: Working with Files and Permissions

Topics:

  1. File manipulation commands
    Explanation: Explore more file manipulation commands such as head (display the beginning of a file), tail (display the end of a file), and wc (word count).
  2. File permissions and access modes
    Explanation: Understand the three levels of file permissions (owner, group, others) and how to set permissions using symbolic notation (rwx) and numeric notation (777).
  3. Changing file ownership and group
    Explanation: Learn how to change the owner and group of a file using the chown and chgrp commands.
  4. File compression and archiving
    Explanation: Discover commands like tar (archive files) and gzip (compress files) to manage large file collections efficiently.
  5. File searching and filtering
    Explanation: Learn about commands like find (search for files and directories) and grep (search for text patterns in files).

Assignments:

  1. Create a file named “sample.txt” and write some content into it.
    Answer: touch sample.txt (to create the file) and nano sample.txt (to open the file in the nano text editor and write the content).
  2. Display the first 5 lines of the file “sample.txt”.
    Answer: head -n 5 sample.txt (to display the first 5 lines).
  3. Set the file permissions of “sample.txt” to read and write for the owner only.
    Answer: chmod 600 sample.txt (to set the permissions).
  4. Change the owner of “sample.txt

” to a different user.
Answer: chown username sample.txt (to change the owner to the specified username).

  1. Archive the file “sample.txt” and compress it into a single file.
    Answer: tar -czvf sample.tar.gz sample.txt (to create the archive and compress it).

Day 3: Introduction to Vim Editor

Topics:

  1. Introduction to Vim
    Explanation: Understand the basics of the Vim editor, including its modes (Normal, Insert, Visual), navigation, and command execution.
  2. Opening and saving files
    Explanation: Learn how to open files in Vim, make changes, and save them using commands like :e (open), :w (save), and :q (quit).
  3. Moving and navigating within files
    Explanation: Explore various movement commands like h (left), j (down), k (up), l (right), and using line numbers to jump to specific locations.
  4. Editing and modifying text
    Explanation: Understand text editing commands like i (insert), o (open a new line), x (delete a character), and yy (copy a line).
  5. Undo and redo operations
    Explanation: Learn how to undo and redo changes made in Vim using the u (undo) and Ctrl+R (redo) commands.

Assignments:

  1. Open the file “my_file.txt” in Vim and navigate to the end of the file.
    Answer: vim my_file.txt (to open the file) and G (to move to the end of the file).
  2. Insert a new line after the current line and write some text into it.
    Answer: Press o (to open a new line) and start typing the desired text.
  3. Delete the current line in Vim.
    Answer: Press dd (to delete the current line).
  4. Copy the current line in Vim and paste it below the current line.
    Answer: Press yy (to copy the current line) and then p (to paste it below).
  5. Save the changes made to the file and exit Vim.
    Answer: Press :wq (to save and quit).

Day 4: Advanced Vim Editing

Topics:

  1. Visual mode in Vim
    Explanation: Learn how to select and manipulate blocks of text using Visual mode, including commands like v (characterwise), V (linewise), and Ctrl+V (blockwise).
  2. Searching and replacing text
    Explanation: Discover how to search for specific text patterns using / (forward search) and ? (backward search), as well as replacing text using :s (substitute).
  3. Advanced editing commands
    Explanation: Explore advanced editing commands like x (delete a character), r (replace a character), J (join lines), and . (repeat the last command).
  4. Advanced movement and navigation
    Explanation: Learn advanced movement commands like w (jump to the beginning of the next word), b (jump to the beginning of the previous word), and gg (jump to the first line).
  5. Split windows and tabs in Vim
    Explanation: Understand how to split the Vim editor window vertically and horizontally using commands like :split and :vsplit, and navigate between tabs.

Assignments:

  1. Open the file “my_notes.txt” in Vim and search for the word “important”.
    Answer: vim my_notes.txt (to open the file) and /important (to search for the word “important”).
  2. Replace all occurrences of the word “old” with “new” in the current line.
    Answer: :s/old/new/ (to perform the substitution).
  3. Join the current line with the line below it.
    Answer: Press J (to join the lines).
  4. Split the Vim editor window vertically.
    Answer: :vsplit (to split the window vertically).
  5. Move the cursor to the beginning of the next word in Vim.
    Answer: Press w (to jump to the beginning of the next word).

Day 5: Vim Customization and Advanced Topics

Topics:

  1. Vim configuration files
    Explanation: Understand the .vimrc file and how to customize Vim settings, key mappings, and plugins.
  2. Customizing Vim colorschemes
    Explanation: Learn how to change the colorscheme in Vim to enhance the visual appearance and readability of your code.
  3. Advanced Vim features and plugins
    Explanation: Explore advanced Vim features like macros, multiple cursors (using plugins), and code completion (using plugins).
  4. Vim navigation shortcuts
    Explanation: Discover useful navigation shortcuts like Ctrl+U (scroll half a page up), Ctrl+D (scroll half a page down), and gg (jump to the first line).
  5. Vim documentation and help
    Explanation: Learn how to access Vim documentation and help resources, including built-in help pages and online resources.

Assignments:

  1. Customize the Vim settings by adding the following lines to your .vimrc file:
  • Set the tab width to 4 spaces.
  • Enable line numbers.
    Answer: Open the .vimrc file in Vim (vim ~/.vimrc) and add the desired configurations.
  1. Change the colorscheme in Vim to a different one.
    Answer: In Vim, type :colorscheme <colorscheme_name> to change the colorscheme.
  2. Create a macro in Vim that inserts a specific code snippet.
    Answer: Record the macro using q<register> and replay it using @<register>.
  3. Install a plugin in Vim for code completion or any other desired functionality.
    Answer: Install the desired plugin using a plugin manager like Vundle or Pathogen.
  4. Access the Vim built-in help and find information on a specific Vim command or feature.
    Answer: In Vim, type :help <command_or_feature> to access the built-in help pages.

Shell Scripting

Apologies for the oversight. Here are the shell scripting assignments along with sample answers for each day:

Day 1:

  1. Write a shell script that takes two numbers as input and prints their sum.
#!/bin/bash

echo "Enter the first number: "
read num1

echo "Enter the second number: "
read num2

sum=$((num1 + num2))
echo "The sum is: $sum"
  1. Create a shell script that reads a filename from the user and checks if it exists in the current directory. If the file exists, display a message confirming its existence; otherwise, display an error message.
#!/bin/bash

echo "Enter a filename: "
read filename

if [ -e "$filename" ]; then
    echo "The file '$filename' exists in the current directory."
else
    echo "Error: The file '$filename' does not exist in the current directory."
fi
  1. Write a shell script that reads a string from the user and prints it in reverse order.
#!/bin/bash

echo "Enter a string: "
read input_string

reversed_string=$(echo "$input_string" | rev)
echo "Reversed string: $reversed_string"
  1. Create a script that takes a directory name as input and lists all the files in that directory.
#!/bin/bash

echo "Enter a directory name: "
read directory

if [ -d "$directory" ]; then
    echo "Files in $directory:"
    ls "$directory"
else
    echo "Error: '$directory' is not a valid directory."
fi
  1. Write a shell script that generates a random number between 1 and 100 and asks the user to guess the number. Provide appropriate feedback based on the user’s guess.
#!/bin/bash

random_number=$((RANDOM % 100 + 1))

echo "Guess the number between 1 and 100: "
read user_guess

if [ "$user_guess" -eq "$random_number" ]; then
    echo "Congratulations! You guessed the correct number."
elif [ "$user_guess" -lt "$random_number" ]; then
    echo "Try again. The number is higher than your guess."
else
    echo "Try again. The number is lower than your guess."
fi

Day 2:

  1. Write a shell script that takes a filename as input and checks if it is a regular file or a directory.
#!/bin/bash

echo "Enter a filename: "
read filename

if [ -f "$filename" ]; then
    echo "'$filename' is a regular file."
elif [ -d "$filename" ]; then
    echo "'$filename' is a directory."
else
    echo "Error: '$filename' is neither a regular file nor a directory."
fi
  1. Create a script that takes a file containing a list of numbers and calculates their sum.
#!/bin/bash

sum=0
while read -r num; do
    sum=$((sum + num))
done < "$1"

echo "Sum of numbers in the file: $sum"
  1. Write a shell script that renames all files in a directory with a specific extension to have a prefix “backup_” followed by the original filename.
#!/bin/bash

echo "Enter the directory name: "
read directory

echo "Enter the file extension to rename: "
read ext

for file in "$directory"/*."$ext"; do
    filename=$(basename "$file")
    mv "$file" "$directory/backup_$filename"
done
  1. Create a script that reads a file and counts the number of lines, words, and characters in it.
#!/bin/bash

echo "Enter a filename: "
read filename

if [ -f "$filename" ]; then
    line_count=$(wc -l < "$filename")
    word_count=$(wc -w < "$filename")
    char_count=$(wc -c < "$filename")

    echo "Number of lines: $line_count"
    echo "Number of words: $word_count"
    echo "Number of characters: $char_count"
else
    echo "Error: '$filename' is not a valid file."
fi
  1. Write a shell script that takes a number as input and prints all the prime numbers less than or equal to that number.
#!/bin/bash

echo "Enter a number: "
read num

if [ "$num" -lt 2 ]; then
    echo "There are no prime numbers less than 2."
else
    echo "Prime numbers less than or equal to $num:"
    for ((i = 2; i <= num; i++)); do
        is_prime=1
        for ((j = 2; j <= i / 2; j++)); do
            if ((i % j == 0)); then
                is_prime=0
                break
            fi
        done
        if [ "$is_prime" -eq 1 ]; then
            echo "$i"
        fi
    done
fi

Day 3:

  1. Create a shell script that takes a directory name as input and finds all the subdirectories within it.
#!/bin/bash

echo "Enter a directory name: "
read directory

if [ -d "$directory" ]; then
    echo "Subdirectories in '$directory':"
    find "$directory" -type d
else
    echo "Error: '$directory' is not a valid directory."
fi
  1. Write a script that reads a file and removes all the empty lines from it.
#!/bin/bash

echo "Enter a filename: "
read filename

if [ -f "$filename" ]; then
    sed -i '/^[[:space:]]*$/d' "$filename"
    echo "Empty lines removed from '$filename'."
else
    echo "Error: '$filename' is not a valid file."
fi
  1. Create a shell script that takes a string as input and checks if it is a palindrome.
#!/bin/bash

echo "Enter a string: "
read input_string

reverse_string=$(echo "$input_string" | rev)

if [ "$input_string" = "$reverse_string" ]; then
    echo "The string is a palindrome."
else
    echo "The string is not a palindrome."
fi
  1. Write a shell script that reads a number as input and checks if it is even or odd.
#!/bin/bash

echo "Enter a number: "
read num

if ((num % 2 == 0)); then
    echo "$num is an even number."
else
    echo "$num is an odd number."
fi
  1. Create a shell script that prints the current date and time.
#!/bin/bash

current_date=$(date +"%Y-%m-%d")
current_time=$(date +"%H:%M:%S")

echo "Current date: $current_date"
echo "Current time: $current_time"

Day 4:

  1. Write a shell script that takes a directory name as input and deletes all the files in that directory with a “.tmp” extension.
#!/bin/bash

echo "Enter a directory name: "
read directory

if [ -d "$directory" ]; then
    find "$directory" -type f -name "*.tmp" -delete
    echo "All '.tmp' files in '$directory' deleted."
else
    echo "Error: '$directory' is not a valid directory."
fi
  1. Create a script that reads a file and replaces all occurrences of a word with another word.
#!/bin/bash

echo "Enter a filename: "
read filename

if [ -f "$filename" ]; then
    echo "Enter the word to replace: "
    read old_word

    echo "Enter the new word: "
    read new_word

    sed -i "s/$old_word/$new_word/g" "$filename"
    echo "Occurrences of '$old_word' replaced with '$new_word' in '$filename'."
else
    echo "Error: '$filename' is not a valid file."
fi
  1. Write a shell script that reads a number as input and checks if it is prime.
#!/bin/bash

echo "Enter a number: "
read num

if [ "$num" -lt 2 ]; then
    echo "The number must be greater than or equal to 2 to check for primality."
else
    is_prime=1
    for ((i = 2; i <= num / 2; i++)); do
        if ((num % i == 0)); then
            is_prime=0
            break
        fi
    done

    if [ "$is_prime" -eq 1 ]; then
        echo "$num is a prime number."
    else
        echo "$num is not a prime number."
    fi
fi
  1. Create a shell script that takes a directory name as input and counts the number of files and subdirectories in it.
#!/bin/bash

echo "Enter a directory name: "
read directory

if [ -d "$directory" ]; then
    file_count=$(find "$directory" -type f | wc -l)
    dir_count=$(find "$directory" -type d | wc -l)

    echo "Number of files: $file_count"
    echo "Number of subdirectories: $dir_count"
else
    echo "Error: '$directory' is not a valid directory."
fi
  1. Write a shell script that reads a string as input and converts it to uppercase.
#!/bin/bash

echo "Enter a string: "
read input_string

upper_case_string=$(echo "$input_string" | tr '[:lower:]' '[:upper:]')

echo "Uppercase string: $upper_case_string"

Day 5:

  1. Create a shell script that takes two numbers as input and swaps their values.
#!/bin/bash

echo "Enter the first number: "
read num1

echo "Enter the second number: "
read num2

echo "Before swapping: num1 = $num1, num2 = $num2"

# Swapping using a temporary variable
temp=$num1
num1=$num2
num2=$temp

echo "After swapping: num1 = $num1, num2 = $num2"
  1. Write a script that takes a file and sorts its lines in ascending order.
#!/bin/bash

echo "Enter a filename: "
read filename

if [ -f "$filename" ]; then
    sort "$filename" > sorted_"$filename"
    echo "Lines in '$filename' sorted in ascending order and saved to 'sorted_$filename'."
else
    echo "Error: '$filename' is not a valid file."
fi
  1. Create a shell script that reads a directory name as input and prints the names of the 10 largest files in it.
#!/bin/bash

echo "Enter a directory name: "
read directory

if [ -d "$directory" ]; then
    echo "The 10 largest files in '$directory':"
    du -ah "$directory" | sort -rh | head -n 10
else
    echo "Error: '$directory' is not a valid directory."
fi
  1. Write a shell script that takes a list of filenames as arguments and checks if all of them exist in the current directory.
#!/bin/bash

for filename in "$@"; do
    if [ ! -e "$filename" ]; then
        echo "Error: '$filename' does not exist in the current directory."
    else
        echo "'$filename' exists in the current directory."
    fi
done
  1. Create a shell script that calculates the factorial of a given number.
#!/bin/bash

echo "Enter a number: "
read num

factorial=1

for ((i = 1; i <= num; i++)); do
    factorial=$((factorial * i))
done

echo "Factorial of $num is: $factorial"

Leave a Reply

Your email address will not be published. Required fields are marked *