Day-3 Mastering File Permissions in Linux for DevOps

Basic Linux Commands

We have to perform some Linux commands. So let’s start

01. What is Linux command to view what’s written in a file.

In Linux, you can view the contents of a file in the terminal using the “cat” command. The “cat” command is short for “concatenate” and is used to display the contents of a file on the terminal.

Here are the steps to view the contents of a file in Linux:

  1. Open the terminal by pressing “Ctrl + Alt + T” on your keyboard.

  2. Navigate to the directory where the file is located using the “cd” command. For example, if the file is located in the home directory of the current user, type the following command and press Enter

cd ~

3. Type the following command and press Enter to view the contents of the file:

cat filename

Replace “filename” with the name of the file you want to view.

4. The “cat” command will display the entire contents of the file on the terminal.

Alternatively, you can use other commands like “less” or “more” to view the contents of a file. These commands allow you to view the contents of a file page by page, making it easier to read long files.

Viewing the contents of a file in Linux is a useful skill for reading configuration files, logs, and other text-based files. It is an essential part of managing files and directories in the terminal.

02. What is the Linux command for to change the access permissions of files.

In Linux, access permissions control who can access and modify files and directories. These permissions include read, write, and execute permissions for the owner, group, and other users. You can change the access permissions of files using the “chmod” command.

Here are the steps to change the access permissions of a file in Linux:

  1. Open the terminal by pressing “Ctrl + Alt + T” on your keyboard.

  2. Navigate to the directory where the file is located using the “cd” command. For example, if the file is located in the home directory of the current user, type the following command and press Enter:

cd ~

3. Type the following command and press Enter to change the access permissions of the file:

chmod permissions filename

Replace “permissions” with the new permissions you want to set for the file, and “filename” with the name of the file you want to modify.

The permissions can be specified in two ways: symbolic mode and numeric mode.

  • Symbolic mode: In this mode, you can use letters like “u” for the owner, “g” for the group, and “o” for other users, followed by “+”, “-”, or “=” signs to add, remove, or set permissions, respectively. For example, to add write permission for the owner, type “chmod u+w filename”.

  • Numeric mode: In this mode, you can use a three-digit number to represent the permission settings. The first digit represents the owner’s permissions, the second digit represents the group permissions, and the third digit represents other users’ permissions. Each digit can be a combination of the numbers 4, 2, and 1, which represent read, write, and execute permissions, respectively. For example, to give the owner read, write, and execute permissions, type “chmod 700 filename”.

4. The “chmod” command will change the access permissions of the file according to the specified mode.

Changing the access permissions of files is an important part of managing files and directories in Linux. It helps you control who can access and modify your files and keeps your system secure.

03. What is the Linux command to check which commands you have run till now.

In Linux, you can check which commands you have run till now using the “history” command. The “history” command is a built-in command in Linux that displays a list of previously executed commands in the current terminal session.

Here are the steps to check the commands you have run in Linux:

  1. Open the terminal by pressing “Ctrl + Alt + T” on your keyboard.

  2. Type the following command and press Enter to display a list of previously executed commands:

history

3. The “history” command will display a list of the most recent commands you have executed, along with their line numbers.

4. You can also use the “history” command with the “grep” command to search for specific commands. For example, to search for all the commands that contain the word “ls”, type the following command and press Enter:

history | grep ls

5. The “grep” command will search the output of the “history” command for the word “ls” and display all the commands that match.

Checking the commands you have run in Linux is a useful way to keep track of your work and troubleshoot any issues you encounter. It is also helpful for recalling previously executed commands that you may need to use again in the future.

04. What is the Linux command to remove a directory/ Folder.

In Linux, you can remove a directory or folder using the “rmdir” or “rm” command. However, there is a difference between these two commands. The “rmdir” command can only remove an empty directory, while the “rm” command can remove both empty and non-empty directories.

Here are the steps to remove a directory or folder in Linux:

  1. Open the terminal by pressing “Ctrl + Alt + T” on your keyboard.

  2. Navigate to the parent directory of the directory or folder you want to remove using the “cd” command. For example, if the directory you want to remove is located in the home directory of the current user, type the following command and press Enter:

cd ~

3. To remove an empty directory, type the following command and press Enter:

rmdir directory_name

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

4. To remove a non-empty directory or folder, type the following command and press Enter

rm -r directory_name

The “-r” option tells the “rm” command to remove the directory and its contents recursively.

5. If the directory or folder you want to remove is write-protected, you may need to use the “sudo” command to gain administrative privileges. For example:

sudo rm -r directory_name

6. The command will remove the directory or folder and its contents permanently.

Note: Be careful when using the “rm” command, as it can permanently delete your files and folders without any confirmation. Always double-check the command before executing it.

Removing a directory or folder is an important part of managing your files and directories in Linux. It helps you keep your system organized and free up disk space.

05. What is Linux command to create a fruits.txt file and to view the content.

To create a text file in Linux, you can use a text editor like Nano or Vim. Here are the steps to create a “fruits.txt” file and view its content in Linux:

  1. Open the terminal by pressing “Ctrl + Alt + T” on your keyboard.

  2. Navigate to the directory where you want to create the “fruits.txt” file using the “cd” command. For example, to create the file in the home directory of the current user, type the following command and press Enter:

cd ~

To create the “fruits.txt” file, type the following command and press Enter:

nano fruits.txt

4. This will open the Nano text editor. Type the content you want to add to the file, such as a list of fruits, and press Ctrl + X to exit. When prompted to save the changes, type “Y” and press Enter.

5. To view the content of the “fruits.txt” file, type the following command and press Enter:

cat fruits.txt

6. This will display the content of the file in the terminal window.

You can also use other text editors like Vim or Emacs to create and edit text files in Linux. Creating and editing text files is a basic skill that is useful for many tasks, such as creating configuration files, writing scripts, and taking notes.

06. What is the Linux command to add content in devops.txt (One in each line) — Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.

To add content to a file in Linux, you can use a text editor like Nano or Vim. Here are the steps to add the fruits — Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava — to the “devops.txt” file, one on each line:

  1. Open the terminal by pressing “Ctrl + Alt + T” on your keyboard.

2. Navigate to the directory where the “devops.txt” file is located using the “cd” command. For example, if the file is located in the home directory of the current user, type the following command and press Enter:

cd ~

3. To open the “devops.txt” file in Nano, type the following command and press Enter:

nano devops.txt

4. This will open the Nano text editor. Move the cursor to the first line of the file and type “Apple”, followed by a new line character (press Enter).

5. Repeat step 4 for the remaining fruits, typing each one on a new line:

Mango
Banana
Cherry
Kiwi
Orange
Guava

6. Once you have added all the fruits, press Ctrl + X to exit Nano. When prompted to save the changes, type “Y” and press Enter.

7. To view the content of the “devops.txt” file, type the following command and press Enter:

cat devops.txt

8. This will display the content of the file in the terminal window, with each fruit on a new line.

You can also use other text editors like Vim or Emacs to add content to text files in Linux. Adding content to files is a basic skill that is useful for many tasks, such as updating configuration files, appending data to log files, and writing reports.

07. What is the Linux command to show only top three fruits from the file?

To show only the top three fruits from a file in Linux, you can use the "head" command. Here are the steps to show only the top three fruits from the "devops.txt" file:

  1. Open the terminal by pressing "Ctrl + Alt + T" on your keyboard.

  2. Navigate to the directory where the "devops.txt" file is located using the "cd" command. For example, if the file is located in the home directory of the current user, type the following command and press Enter:

cd ~

3. To show only the top three fruits from the "devops.txt" file, type the following command and press Enter:

head -n 3 devops.txt

4. This will display the first three lines of the "devops.txt" file in the terminal window, which correspond to the top three fruits in the file.
The "head" command is used to display the first few lines of a file, and it takes an optional argument (-n) to specify the number of lines to display. In this case, we use the "-n 3" option to show only the first three lines. If you want to show a different number of lines, simply replace "3" with the desired number.

You can also use other commands like "sed" or "awk" to extract specific lines from a file in Linux. Extracting data from files is a common task in data analysis, report generation, and automation.

08. What is the Linux command to show only bottom three fruits from the file.

To show only the bottom three fruits from a file in Linux, you can use the "tail" command. Here are the steps to show only the bottom three fruits from the "devops.txt" file:

  1. Open the terminal by pressing "Ctrl + Alt + T" on your keyboard.

  2. Navigate to the directory where the "devops.txt" file is located using the "cd" command. For example, if the file is located in the home directory of the current user, type the following command and press Enter:

cd ~

3. To show only the bottom three fruits from the "devops.txt" file, type the following command and press Enter:

tail -n 3 devops.txt

4. This will display the last three lines of the "devops.txt" file in the terminal window, which correspond to the bottom three fruits in the file.
The "tail" command is used to display the last few lines of a file, and it takes an optional argument (-n) to specify the number of lines to display. In this case, we use the "-n 3" option to show only the last three lines. If you want to show a different number of lines, simply replace "3" with the desired number.

You can also use other commands like "sed" or "awk" to extract specific lines from a file in Linux. Extracting data from files is a common task in data analysis, report generation, and automation

09. What is Linux command to create another file Colors.txt and to view the content.

  1. Use the 'touch’ command to create an empty file called "Colors.txt". Type the following command and press Enter:
touch Colors.txt

2. Use a text editor like nano or vi to open the file and add the content you want. For example, you can add the following lines to the file:

Red
Blue
Green
Yellow

3. To do this, type the following command and press Enter:

nano Colors.txt

This will open the nano text editor with a blank file called "Colors.txt".

4. Type or paste the content you want to add to the file, then press Ctrl+X, followed by Y and Enter to save the changes and exit nano.

5. To view the content of the “Colors.txt” file, you can use the cat command. Type the following command and press Enter:

cat Colors.txt

This will display the content of the file in the terminal window.

Creating and viewing files is a basic task in Linux, and there are many different ways to do it depending on your preferences and the tools available on your system. The touch, nano, and cat commands are just a few examples of the many tools available for file creation and manipulation in Linux.

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

To add the content “Red, Pink, White, Black, Blue, Orange, Purple, Grey” to the existing “Colors.txt” file in Linux, you can follow these steps:

  1. Open a terminal window by clicking on the terminal application or using the keyboard shortcut Ctrl+Alt+T.

2. Use a text editor like nano or vi to open the "Colors.txt" file. Type the following command and press Enter:

nano Colors.txt

This will open the nano text editor with the existing content of the file.

3. Add the new content to the file, one line at a time. For example, add the following lines to the end of the file:

Red
Pink
White
Black
Blue
Orange
Purple
Grey

4. Press Ctrl+X, followed by Y and Enter to save the changes and exit nano.

5. To view the updated content of the “Colors.txt” file, you can use the cat command. Type the following command and press Enter:

cat Colors.txt

This will display the updated content of the file in the terminal window.

By following these steps, you have successfully added new content to the “Colors.txt” file in Linux. Note that there are many other ways to edit and modify files in Linux, depending on your preferences and the tools available on your system. The nano text editor is just one example of a simple and powerful text editor that can be used for a wide range of file editing tasks.

11. Commands to find the difference between fruits.txt and Colors.txt file.

To find the differences between the “fruits.txt” and “Colors.txt” files in Linux, you can use the diff command. The diff command is used to compare two files and show the differences between them.

Here are the steps to find the differences between the two files:

  1. Open a terminal window by clicking on the terminal application or using the keyboard shortcut Ctrl+Alt+T.

  2. Type the following command and press Enter to compare the two files:

diff fruits.txt Colors.txt

3. The diff command will display the differences between the two files, if any. If the two files are identical, there will be no output.

In this example, the diff command shows that the "Mango" line in the "fruits.txt" file is different from the "Pink" line in the "Colors.txt" file. It also shows that the "Banana" and "Cherry" lines in the "fruits.txt" file are different from the "White" and "Black" lines in the "Colors.txt" file, respectively. Similarly, it shows the other differences between the two files.

By using the diff command, you can easily find the differences between two files in Linux, which can be useful for a variety of tasks, such as comparing configuration files or checking for changes in log files.

Thank you for Reading..!

"Embark on the Journey of Knowledge: Happy Learning Lights Up Your Path."

_Abhi Sagare.