Day 3: 90DaysOfChallenge

Day 3: 90DaysOfChallenge

Task: Basic Linux commands-

  • To view what's written in a file

cat - 'cat' command is used to view all the contents written in a file. We can see in the below example, the content of fruits.txt is displayed on giving the cat command cat fruits.txt.

  • To change the access permission of files.

    chmod - 'chmod' command is used to set read(r), write(w) and execute(x)permission on a file/directory for the owner/user(u), group(g) or others(o). The three operators +,-, and = are used to add, remove and set permissions respectively.

Ex- chmod u+rwx devops.txt - this gives the read, write and execute permission to the user for devops.txt file,

chmod u-w Colors.txt - this removes the write permission of the user for Colors.txt file,

chmod go-w Colors.txt - this removes the write permission of group and others for Colors.txt file.

  • To check which commands you have run till now.

    history - 'history' command shows the list of all commands which were previously used till the present. Ex- history

  • To remove a directory/ Folder

    rm and rm-r- 'rm' command is used to delete the selected file and 'rm -r' is used to delete the entire directory. As in the below example, we can see Ubuntu directory has 2 folders named Shilpi and test and one txt file named test1.txt. Hence, to delete test1.txt command rm test1.txt is used and to delete the test folder command rm -r test is used.

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

    touch and cat - 'touch' command is used to create empty files, we can also update the modification and access time of each file with the help of touch command. As discussed earlier 'cat' command reads data from the file and gives its content as output. It helps us to create, view, and concatenate files.

    We can see in the below example, the command touch fruits.txt is used to create fruits.txt file and cat fruits.txt is used to display the content of fruits.txt file.

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

We are using vim editor to store the data. This makes our work easier as it simply takes us to the vim editor where we can write, modify and save our data. As soon as we give the command vim devops.txt and hit enter, it takes us to the vim editor. To start writing the content, we need to press 'i', it enables the writing mode and we can start writing. Once we have written the content, we will press the Esc key to disable the writing mode. Now we have three options - :wq - save the written file and close the editor, :qa - close the editor without saving, :qa! - close the editor forcefully.

vim devops.txt command opens the editor-

Writing the name of fruits in the editor in insert mod-

After exiting from the insert mode by pressing the Esc key and giving :wq command, we can see the saved content(the contents are shown in each line) by giving the command cat devops.txt

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

head -n filename - To view the content from the top, we use head -n folderName.

Ex: head -3 devops.txt(to view the top three content)

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

tail -n filename - To view the content from the bottom, we use tail -n folderName.

Ex: tail -3 devops.txt(to view the bottom three content)

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

touch and cat - This is same as Pint 5. In the below example, the command touch Colors.txt is used to create Colors.txt file and cat Colors.txt is used to display the content of Colors.txt file. As we have just created the file and not added anything so we don't see any content on giving cat Colors.txt.

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

cat > filename - Here we will be using the cat command to create, add and view the content. The 'cat' command can be used to create a new file with a greater than sign (>). cat > Colors.txt is used to create Colors.txt file and as soon as we hit enter we can write the content. Once the content is written, go to the next line and press Ctrl+D to save the file. Again, we can see the content using cat Colors.txt command.

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

    diff - It displays the difference by comparing files line by line. It uses certain symbols and instructions to make the two files identical i.e. a: add, d: delete, c: change. It tells us how to change the first file to match it with the second. < or less than symbol signifies the content from the first file and > or greater than symbol signifies the content from the second file. diff fruits.txt Colors.txt command is used to display the difference between fruits.txt and Colors.txt file

    Happy Learning!

    ~Shilpi