Basic Linux Commands and its uses'

Basic Linux Commands and its uses'

Day 3 Task: Basic Linux Commands:

Task: What is the Linux command to

  1. To view what's written in a file.

  2. To change the access permissions of files.

  3. To check which commands you have run till now.

  4. To remove a directory/ Folder.

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

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

  7. To Show only top three fruits from the file.

  8. To Show only bottom three fruits from the file.

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

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

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

    1.To view what's written in a file: The command to view the contents of a file is 'cat'. The syntax is:

cat <filename>
Example:
cat fruits.txt

2.To change the access permissions of files: The command to change access permissions is 'chmod'. The syntax is:

chmod <permissions> <filename>
Example:
    chmod 644 fruits.txt

Here, 'permissions' refer to the new permissions you want to set, and can be specified using numbers or letters.

This command sets the read and write permissions for the owner, and read-only permission for the group and others.

3.To check which commands you have run till now: The command to view the history of commands is 'history'. The syntax is:

history

4.To remove an empty directory/Folder: The command to remove a directory is 'rmdir'. The syntax is:

rmdir <directory_name>

However, this command can only be used to remove empty directories. If you want to remove a directory and all its contents, you can use the 'rm' command with the '-r' option:

    rm -rf <directory_name>

5.To create a fruits.txt file and to view the content: The command to create a new file is 'touch'. The syntax is:

    touch <filename>
example:
    touch fruits.txt

6.Add content in devops.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava: The command to add content to a file is 'echo'. The syntax is:

    echo <content> >> <filename>

Here, 'content' refers to the text you want to add, and '>>' appends the text to the end of the file (creating the file if it doesn't exist).

    echo "Apple" >> devops.txt
    echo "Mango" >> devops.txt
    echo "Banana" >> devops.txt
    echo "Cherry" >> devops.txt
    echo "Kiwi" >> devops.txt
    echo "Orange" >> devops.txt
    echo "Guava" >> devops.txt

7. To Show only top three fruits from the file: The command to show the first few lines of a file is 'head'. The syntax is:

    head -n <number of lines> <filename>
exapmle:
    head -n 5 devops.txt
/*This command will display the first 3 fruits in the 'devops.txt' file*/

8.To Show only bottom three fruits from the file: The command to show the last few lines of a file is 'tail'. The syntax is:

    tail -n <number of lines> <filename>
example:
    tail -n 5 devops.txt
/*This command will display the last 3 fruits in the 'devops.txt' file.*/