Day 3 Task: Basic Linux Commands

Day 3 Task: Basic Linux Commands

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

    You can use the cat command to view the contents of a file in the terminal. The basic syntax is:

    Replace "filename" with the actual name of the file you want to view. For example, to view the contents of a file named "example.txt", you would use:

  2. To change the access permissions of files.

    You can use the chmod command to change the access permissions of files. The chmod command allows you to modify the read, write, and execute permissions for the owner, group, and others. Here's the basic syntax:

    Replace permissions with the desired permission settings, and filename with the name of the file you want to modify.

    Permissions are represented using a three-digit number or a combination of letters. The three digits correspond to the owner, group, and users, respectively. Each digit is a combination of read (4), write (2), and execute (1) permissions.

    For example, if you want the owner to have read and write access (4 + 2 = 6), the group to have read access (4), and others to have no access (0), you end up with the permission code 640.

    Linux Tutorials: Chmod Commands - DevOpsSchool.com

    Understanding Linux Permissions and chmod Usage

    File permissions in Linux determine who can do what with a file. There are three main types of permissions: read, write, and execute. These permissions are set for three different groups: the file's owner, the group the file belongs to, and everyone else.

    1. Read (r): If a user has read permission on a file, they can view the contents of the file.

    2. Write (w): Write permission allows a user to modify the contents of a file. They can add, change, or delete the content.

    3. Execute (x): Execute permission is for files that are programs or scripts. If a user has execute permission on a file, they can run it as a program or script.

Now, let's talk about the three groups:

  • Owner: This is the person who created the file. They have full control over the file and can change its permissions.

  • Group: Each file belongs to a certain group. Members of that group share the group's permissions for that file. For example, if a file belongs to a "staff" group and the group has read and write permissions, all members of the "staff" group can read and write to that file.

  • Others: This refers to everyone else who is not the owner or a member of the group that the file belongs to. These are usually people who don't have any particular connection to the file.

Permissions are often shown as a sequence of letters or numbers, like "rwxr-x---". The first three characters represent the owner's permissions, the next three represent the group's permissions, and the last three represent the permissions for others.

For example, "rwxr-x---" means:

  • The owner can read, write, and execute the file.

  • The group can read and execute the file.

  • Others have no permissions at all.

Managing file permissions helps keep your files secure and controls who can access or modify them. It's a crucial aspect of maintaining the privacy and integrity of your data in a Linux system.

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

    The history command is used to display a list of previously executed commands in a terminal session. It's a handy tool for quickly recalling and reusing commands you've used in the past without having to retype them. This can be especially useful when you're working on the command line and need to refer back to commands you've used before.

  2. To remove a Directory/Folder.

    To remove a directory (folder) in Linux, you can use the rmdir or rm command, depending on whether the directory is empty or contains files. Here's how to use both commands:

    1. Removing an Empty Directory:

If the directory is empty, you can use the rmdir command:

Replace directory_name with the name of the directory you want to remove.

  1. Removing a Non-Empty Directory:

If the directory contains files or other directories, you need to use the rm command with the -r (or --recursive) option to remove the directory and its contents recursively:

Be cautious when using rm -r as it will permanently delete the directory and all its contents.

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

    1. Creating the fruits.txt file:

You can create a new file using the touch command, which will create an empty file with the specified name:

  1. Viewing the content of fruits.txt:

To view the content of the fruits.txt file, you can use the cat command:

can also use other text viewers like less or more for more advanced ways of viewing and navigating through the file content.

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

    To add the specified content to a file named devops.txt with each item on a separate line, we can use the following command:

    This command uses the echo command to print the specified content and the -e option enables interpretation of backslash escapes (such as \n for a new line). The > operator is used to redirect the output to the devops.txt file, effectively creating the file and adding the content to it.

    Another method to add the specified content to a file named devops.txt with each item on a separate line, we can use the following commands:

    1. Create the file:

If the devops.txt file doesn't exist, you can create it using the touch command:

  1. Add content to the file:

You can use a text editor to add the content to the file. For example, using nano:

Add the following lines in the text editor:

Press Ctrl + O to save the changes and Ctrl + X to exit nano.

  1. View the content of the file:

To verify that the content has been added correctly, you can use the cat command:

  1. To show only the top three fruits from the file.

    To display only the top three fruits from the devops.txt file, we can use the head command. The head command is used to display the beginning (or top) lines of a file.

    This command will display the first three lines of the devops.txt file, which correspond to the top three fruits.

    The -n option specifies the number of lines to display, and 3 is the number of lines you want to show.

    This will give you the desired output of showing only the top three fruits from the devops.txt file.

  2. To show only the bottom three fruits from the file.

    To display only the bottom three fruits from the devops.txt file, you can use the tail command. The tail command is used to display the end (or bottom) lines of a file.

    This command will display the last three lines of the devops.txt file, which corresponds to the bottom three fruits:

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

    1. Create the Colors.txt file:

You can use the touch command to create an empty file named Colors.txt:

  1. View the content of the Colors.txt file:

You can use the cat command to view the content of the newly created file:

At this point, since you just created the file, it will be empty and the cat command will not display any content.

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

    We can use the echo command along with the > operator to add content to the Colors.txt file one in each line.

    In this command:

    • echo -e: The -e option enables the interpretation of escape sequences, allowing you to use \n for newlines.

    • "Red\nPink\nWhite\nBlack\nBlue\nOrange\nPurple\nGrey": This string contains the colours separated by newline characters \n.

    • >: This operator redirects the output of the echo command to a file.

    • Colors.txt: The target file to which the output of the echo command will be redirected.

  2. To find the difference between fruits.txt and Colors.txt files.

    To find the difference between two text files, you can use the diff command.

    The diff command will display the differences between the two files, showing lines that are unique to each file and lines that differ between the files.

    If the files are identical, the diff command will produce no output. If there are differences, the command will show the differing lines with a line-by-line comparison.

    Remember to make sure that both fruits.txt and Colors.txt are in the same directory or provide the full paths to the files if they are in different locations.