Day 3 Task: Basic Linux Commands

Task: What is the linux command to

To view what's written in a file.

cat filename: Displays the entire contents of a file.

  1. To change the access permissions of files.

To change the access permissions of files.

Certainly! Changing the access permissions of files in Linux is a common task. Here's a practical guide with clear examples:

Using Octal Notation:

  1. Numeric Representation:

    • chmod [owner][group][others] filename

Examples:

    # Give read, write, and execute permissions to the owner, and read permission to others
    chmod 744 filename

  1. In this example:

    • 7 (owner) grants read (4) + write (2) + execute (1).

    • 4 (group) grants read.

    • 4 (others) grants read.

  2. Symbolic Representation:

    • chmod [who][operator][permissions] filename

Examples:


    chmod u+x filename

  1. In this example:

    • u (owner) gets the execute permission added.

Using Symbolic Notation:

  1. Symbolic Representation:

    • chmod [who][operator][permissions] filename

Examples:

    chmod o-w filename

  1. In this example:

    • o (others) has write permission removed.

Practical Tips:

  • Readable Permissions:

    • r for read, w for write, x for execute.

    • Combine them logically for quick understanding.

  • Quick Reference:

    • chmod 755 gives read, write, and execute to the owner, and read/execute to group and others.
  • Symbolic Clarity:

    • u (owner), g (group), o (others), + (add), - (remove).

Examples:

  1. Give Full Access to Owner, Read-Only to Others:

     chmod 644 filename
    
  2. Add Execute Permission for Everyone:

     chmod a+x filename
    
  3. Restrict Access to Owner Only:

      chmod 700 filename
    

Feel free to adapt the examples to your needs, and always consider security implications when adjusting file permissions.

To check which commands you have run till now.

Usinghistorycommand:

history

This will display a list of previously executed commands with line numbers.

To remove a directory/ Folder.

  • To remove a directory (folder) in Linux, you can use the rmdir or rm command. Here are the basic commands:

    1. Using rmdir command:

      • The rmdir command is used to remove empty directories.
        rmdir directory_name

Note: If the directory is not empty, rmdir will not remove it.

  1. Using rm command:

    • The rm command, when used with the -r (or -R) option, can remove directories and their contents recursively.
        rm -r directory_name

Warning: Be cautious when using rm -r as it will remove the directory and its contents without confirmation.

Always be careful when using the rm command, especially with the -r option, to avoid unintentional removal of important data. Double-check your command before executing it.

To create a fruits.txt file and to view the content.

Creating fruits.txt:


touch fruits.txt

Adding Content to fruits.txt:

You can use a text editor like nano, vim, or even echo to add content to the file. Here's an example using echo:


echo "Apple" >> fruits.txt
echo "Banana" >> fruits.txt
echo "Orange" >> fruits.txt

Add content in devops.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.

  •   echo -e "Apple\nMango\nBanana\nCherry\nKiwi\nOrange\nGuava" > devops.txt
    

    This command uses the -e option for echo to enable interpretation of backslash escapes, and \n is used to represent newlines between each fruit. The output is then redirected (>) to the devops.txt file.

    After running this command, the devops.txt file will contain each fruit on a new line.

To Show only top three fruits from the file.

You can use the head command to display the top (first) three lines from the file. Here's how you can do it:

head -n 3 devops.txt

This command uses the -n option to specify the number of lines to display, and in this case, it will show the top three lines from the devops.txt file.

After running this command, you should see the names of the top three fruits in the file.

To Show only bottom three fruits from the file.

You can use the tail command to display the bottom (last) three lines from the file. Here's how you can do it:

tail -n 3 devops.txt

This command uses the -n option to specify the number of lines to display, and in this case, it will show the bottom three lines from the devops.txt file.

To create another file Colors.txt and to view the content.

echo -e "Red\nGreen\nBlue" > Colors.txt
cat Colors.txt

These commands:

  1. Use echo with the -e option to create a file Colors.txt with three colors on separate lines.

  2. Use cat to display the content of Colors.txt.

After running these commands, you'll have a file named Colors.txt with the specified colors, and you'll see the content displayed in the terminal using cat.

  • Add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey.

To find the difference between fruits.txt and Colors.txt file.

  • To find the difference between two files, you can use the diff command. Here's how you can find the difference between fruits.txt and Colors.txt:

  • This command will display the lines that differ between the two files. If there are no differences, it will produce no outpu