Dev Ops Day03

  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. Show only the top three fruits from the file.

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

Solutions(with command overview):

Q1:

cat:

The cat command is a utility command in Linux. One of its most common usages is to print the content of a file onto the standard output stream. Other than that, the cat command also allows us to write some texts into a file.

Syntax:

cat file_name

Example:

let's suppose there is a file called myFile.txt. And it has the content "cat command" inside it. so just follow the command as it shows in the syntax.

Q2:

chmod:

Linux chmod command is used to change the access permissions of files and directories. It stands for change mode. It can not change the permission of symbolic links. It even ignores the symbolic links that come across recursive directory traversal.

Syntax:

chmod <options> <filename>

In the Linux file system, each file is associated with a particular owner and has permission access for different users. The user classes may be:

  • Owner

  • Grouped

  • Others

The file permissions in Linux are the following three types:

  • read(r)

  • write(w)

  • execute(x)

The file permission using numeric:

  • read(r): 4

  • write(w): 2

  • execute(x):1

    777: read +write+execute

Example:

The file we have just created 'myFile.txt' has some important content in it and the owner can't allow to access it to any of the people {grouped, Others}. So the owner wants to put the permission as {rwx--} means only the owner has the permission to read, write and execute. So the execute this command on his terminal.

After entering the command you can see the changes that happened using the 'ls -l' command:

Q3:

History:

Linux history command is used to display the history of the commands executed by the user. It is a handy tool for auditing the executed commands along with their date and time. Most of the commands read input from the terminal a line at a time.

just type history on your terminal and it will show you the commands run until now.

Q4:

rmdir or rm:

rmdir: If you want to delete a directory which empty from the inside which means it doesn't have any content inside it then you can use rmdir. rmdir stands for 'remove directory '

Syntax:

rmdir <directory_name>

Example:

let's suppose you have a directory named 'myDir' it is an empty directory so you can use rmdir to delete it. use ls to check if the directory is deleted or not.

rm command:

If you're dealing with a directory which is having some sort of content in it then we have to use the rm command to delete the directory.

Syntax:

rm <options><file_name>

Example:

Let's consider that you have a directory that contains a text file and if you try to delete this directory by using the rmdir then you will get an error that this directory is not empty.

let's delete this file with the help of rm command:

Q5:

touch:

It is used to create a file without any content. The file created using the touch command is empty. This command can be used when the user doesn’t have data to store at the time of file creation.

Syntax:

touch <file_name>

Example:

Let's create a file called fruits.txt in our Desktop directory.

Q6:

vim:

Vim is a text editor for Unix that comes with Linux, BSD, and macOS. It is known to be fast and powerful, partly because it is a small program that can run in a terminal (although it has a graphical interface). It is mainly because it can be managed entirely without menus or a mouse with a keyboard.

To edit a file that is already present in your system then you can either edit it manually by using echo <text> > file_name or you can use vim editor to edit a file.

How to use vim?

To open a file in vim editor just follow this syntax:

Syntax:

vim <file_name>

When you open a file in vim editor you will go to see a black screen like this:

to insert any content in this file you will have first press ' i ' for going to reading mode to inserting mode. when you press the ' i ' button on your keyboard you will see an insert option on the left bottom corner of your screen like this:

now you are in inserting mode. You can now add content in this file like you add regularly on notepad on windows.

After adding content to the file press esc key on your keyboard for getting out from the inserting mode. Now to save the file type ':wq' which stands for write and quit. After writing it press enter. Now you will redirect to your main terminal. now print the content of the file by using the cat command.

Q 7 and 8:

Head:

Head command will print the first few lines in a file.

Syntax:

head <options> file_name

Task:

To print the first 3 lines of devops.txt file type this:

Tail :

The tail command is used to print the bottom few lines of a file.

tail <options> file_name

Task:

to print the last 3 lines of devops.txt file type this on your terminal:

0
Subscribe to my newsletter

Read articles from Subhodeep goswami directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Subhodeep goswami
Subhodeep goswami

i am an software developer intern at an IT company