Basic Linux Commands-DAY 3

·

9 min read

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

In Linux, you can use the "cat" command to view the contents of a file.

To use the "cat" command, open a terminal and type the following command:

cat filename

Replace "filename" with the name of the file that you want to view. When you press enter, the shell will display the contents of the file in the terminal.

If the file is very large, you may want to use the "less" command instead. The "less" command displays the contents of a file one page at a time, and allows you to scroll through the file with the arrow keys. To use the "less" command, type the following command:

less filename

Again, replace "filename" with the name of the file that you want to view. When you press enter, the shell will display the first page of the file in the terminal. You can use the arrow keys to scroll through the file, or type "q" to quit the "less" command and return to the shell.

  1. To change the access permissions of files.

In Linux, you can use the "chmod" command to change the access permissions of files. The "chmod" command stands for "change mode". The access permissions of a file determine which users can read, write, or execute the file.

The "chmod" command uses a three-digit code to specify the new permissions for the file. The first digit represents the permissions for the owner of the file, the second digit represents the permissions for the group that owns the file, and the third digit represents the permissions for all other users.

Each digit can be a number from 0 to 7, representing the permissions as follows:

  • 0: no permissions

  • 1: execute permission

  • 2: write permission

  • 3: write and execute permissions

  • 4: read permission

  • 5: read and execute permissions

  • 6: read and write permissions

  • 7: read, write, and execute permissions

To change the access permissions of a file using the "chmod" command, open a terminal and type the following command:

chmod xyz filename

Replace "xyz" with the three-digit code that represents the new permissions for the file, and replace "filename" with the name of the file that you want to modify.

For example, the command "chmod 755 myfile.txt" would set the permissions of "myfile.txt" so that the owner has read, write, and execute permissions, and all other users have read and execute permissions.

You can also use symbolic notation to modify the permissions of a file. For example, the command "chmod u+x myfile.txt" would add execute permission for the owner of the file.

Note that changing the permissions of a file can affect its security and stability, so be careful when using the "chmod" command. It's a good practice to only change the permissions of files that you own or have explicit permission to modify.

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

In Linux, you can use the "history" command to view a list of the commands that you have run in the current terminal session. The "history" command displays a numbered list of the commands, with the most recent commands at the bottom of the list.

To use the "history" command, open a terminal and type "history" followed by the enter key. The shell will display the list of commands.

You can also use the up and down arrow keys to navigate through the list of commands. If you find a command that you want to run again, you can either type it manually or use the arrow keys to select the command and press enter.

If you want to search for a specific command in the history, you can use the "grep" command. For example, the following command would search the history for all commands that contain the word "ls":

history | grep ls

This command will display a list of all the commands that contain the word "ls".

  1. To remove a directory/ Folder.

The "rmdir" command is used to remove an empty directory. To use this command, open a terminal and type the following command:

rmdir directoryname

Replace "directoryname" with the name of the directory that you want to remove. If the directory is empty, it will be deleted. If the directory contains any files or subdirectories, you will need to remove them first before using the "rmdir" command.

The "rm" command is used to remove a directory and its contents, including all files and subdirectories. To use this command, open a terminal and type the following command:

rm -r directoryname

Replace "directoryname" with the name of the directory that you want to remove. The "-r" option tells the shell to remove the directory recursively, including all files and subdirectories. Be careful when using the "rm" command, as it can delete files and directories without confirmation.

It's also a good practice to double-check the name of the directory that you want to remove, to avoid accidentally deleting the wrong directory.

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

To create a file named "fruits.txt" in Linux, you can use the "touch" command:

touch fruits.txt

This command will create an empty file named "fruits.txt" in the current directory.

To add some content to the file, you can use a text editor such as "nano", "vim", or "emacs". For example, to use "nano" to edit the "fruits.txt" file, you can type the following command:

nano fruits.txt

This will open the "fruits.txt" file in the nano text editor. You can then type the contents of the file, such as:

apple
banana
orange

To save the changes and exit the editor, press "Ctrl+X", then "Y" to confirm the save, and finally "Enter".

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

cat fruits.txt

This will display the contents of the "fruits.txt" file in the terminal:

apple
banana
orange

Alternatively, you can use the "less" command to view the contents of the file one page at a time:

less fruits.txt

This will display the contents of the "fruits.txt" file one page at a time, and allow you to navigate up and down through the file.

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

To add the content "Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava" (one item per line) to a file named "devops.txt" in Linux, you can use a text editor such as "nano", "vim", or "emacs".

Here's how to use "nano" to edit the "devops.txt" file and add the content:

  1. Open the "devops.txt" file in nano by typing the following command:
nano devops.txt
  1. Type or copy and paste the content "Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava" into the file, one item per line. The file should look like this:
Apple
Mango
Banana
Cherry
Kiwi
Orange
Guava
  1. To save the changes and exit the editor, press "Ctrl+X", then "Y" to confirm the save, and finally "Enter".

That's it! You have now added the content to the "devops.txt" file. You can view the contents of the file using the "cat" command:

cat devops.txt

This will display the contents of the "devops.txt" file in the terminal:

Apple
Mango
Banana
Cherry
Kiwi
Orange
Guava

To Show only top three fruits from the file.

To show only the top three fruits from the "devops.txt" file in Linux, you can use the "head" command with the "-n" option to specify the number of lines to display. Here's how you can do it:

head -n 3 devops.txt

This command will display the first three lines of the "devops.txt" file, which in this case are the top three fruits:

Apple
Mango
Banana

This will only display the top three fruits in the file, as per your requirement.

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

To show only the bottom three fruits from the "devops.txt" file in Linux, you can use the "tail" command with the "-n" option to specify the number of lines to display. Here's how you can do it:

tail -n 3 devops.txt

This command will display the last three lines of the "devops.txt" file, which in this case are the bottom three fruits:

Kiwi
Orange
Guava

This will only display the bottom three fruits in the file, as per your requirement.

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

To create a new file named "Colors.txt" in Linux and view its contents, you can use the following commands:

bashCopy codetouch Colors.txt

This command creates an empty file named "Colors.txt" in the current directory.

To add some content to the file, you can use a text editor like "nano", "vim", or "emacs". Here's how to use "nano" to edit the "Colors.txt" file and add some colors:

Copy codenano Colors.txt

This command opens the "Colors.txt" file in the nano text editor. You can then type or copy and paste the colors into the file. For example, you can add the following lines:

mathematicaCopy codeRed
Green
Blue
Yellow
Orange
Purple

Once you have added the content, you can save and close the file by pressing "Ctrl+X", then "Y" to confirm the save, and finally "Enter".

To view the contents of the "Colors.txt" file, you can use the "cat" command:

cat Colors.txt

This command will display the contents of the "Colors.txt" file in the terminal:

Red
Green
Blue
Yellow
Orange
Purple

That's it! You have now created a new file named "Colors.txt" in Linux and viewed its contents.

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

To add content to the "Colors.txt" file in Linux, you can use a text editor like "nano", "vim", or "emacs". Here's how to use "nano" to edit the "Colors.txt" file and add the colors you listed:

nano Colors.txt

This command opens the "Colors.txt" file in the nano text editor. You can then type or copy and paste the colors into the file. For example, you can add the following lines:

Red
Pink
White
Black
Blue
Orange
Purple
Grey

Once you have added the content, you can save and close the file by pressing "Ctrl+X", then "Y" to confirm the save, and finally "Enter".

Now, if you view the contents of the "Colors.txt" file using the "cat" command, like this:

cat Colors.txt

This command will display the updated contents of the "Colors.txt" file in the terminal:

Red
Pink
White
Black
Blue
Orange
Purple
Grey

That's it! You have now added the new colors to the "Colors.txt" file in Linux.

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

To find the differences between two files, you can use the "diff" command in Linux. Here's how to use it to compare the "fruits.txt" and "Colors.txt" files:

diff fruits.txt Colors.txt

This command will compare the two files and display the differences in the terminal. The output will look something like this:

3c3
< Banana
---
> White
5,6c5,6
< Orange
< Guava
---
> Orange
> Purple

This output indicates that:

  • "Banana" appears in "fruits.txt" but not in "Colors.txt"

  • "White" appears in "Colors.txt" but not in "fruits.txt"

  • "Orange" and "Guava" appear in "fruits.txt" but only "Orange" and "Purple" appear in "Colors.txt"

The "<" symbol indicates lines that appear in the first file ("fruits.txt"), while the ">" symbol indicates lines that appear in the second file ("Colors.txt").

That's it! You have now used the "diff" command in Linux to find the differences between two files.