#Day3:- 90 Days DevOps Challenge @ Linux Fundamentals
Empowering DevOps with Linux Fundamentals π§π»
Table of contents
- Introduction π
- 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 file.txt file and to view the content π
- 6. Add content in fruits.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guavaπππππ₯ππ
- 7. To Show only top three lines from the file π
- 8. To Show only bottom three lines from the file π»
- 9. To create another file Colors.txt and to view the content π
- 10. Add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey πΉπβͺβ«π΅πππ³
- 11. To find the difference between fruits.txt and Colors.txt file πππ
- Conclusion π
Introduction π
Welcome back to Day 3 of the thrilling #90DaysOfDevOps challenge! Today, we'll explore the wonders of Linux commands, like magical keys unlocking every DevOps engineer's potential! With these tools, navigating your Linux system becomes as easy as exploring a map. ππΊοΈπ» Get ready for a tech-packed journey! Together, we'll unravel the mysteries of essential Linux commands, discovering hidden treasures that elevate your skills. Excitement fills the air as we dive into this epic DevOps adventure! Let's go! ππ»
1. To view what's written in a file π
To view the contents of a file in Linux, you can use the "cat"
command. For example, let's say you have a file named "example.txt" To view what's written in this file, open your terminal and write cat example.txt:
Also, we can use "less
" or "more
" and "head
" or "tail
" instead of "cat
" to view files as they offer better text navigation. "Cat
" shows all content at once, while "less
" and "more
" allow scrolling and analyzing large files more easily. Apart from that head
is a command used to display the beginning lines of a text file or input stream, while tail
is a command used to display the ending lines of a text file or input stream.
2. To change the access permissions of files π
Changing the access permissions of files in Linux is done using the "chmod
" command, which stands for "change mode." πͺ
In Linux, every file has three types of permissions: read (r), write (w), and execute (x). These permissions can be set for three different categories of users: the file's owner (u), the group (g) the file belongs to, and others (o) who are not the owner or part of the group.
To change the permissions, we use a combination of letters and symbols. For example:
"
chmod u+rwx example.txt
" grants read, write, and execute permissions to the file owner."
chmod go-r example.txt
" revokes read permission from the group and others.
Here's an example:
Let's say we have a script file named "script.sh" that we want to make executable only for the owner and read-only for others. We would use the following command:
Output:
In this command:
"u+x" adds execute permission to the owner.
"go-w" removes written permission from the group and others.
After running this command, the owner of "script.sh" will be able to execute it, while the group and others will only have read access.
/Another Approach (Octal Notation):
Using Octal Notation, The octal notation assigns a numerical value to each permission. Read is represented by 4, write by 2, and execute by 1. To achieve the same permissions as in Example 1, we can use:
Output:
In this example, the owner has read and write permissions (4), the group has read-only permissions (6), and others have no permissions (7).
3. To check which commands you have run till now ππͺ¨
To check the commands you have run in the current terminal session in Linux, you can use the "history
" command. π
4. To remove a directory/ Folder ποΈ
Removing a directory or folder in Linux is done using the "rmdir
" or "rm -r
" command. ποΈ
The "rmdir
" command is used specifically to remove empty directories. For example:
The "rmdir
" removes an empty "example" directory, while "rm -r
" deletes directories with content and files. For example
Be cautious with "rm -r
" to delete "demo" and its contents without confirmation, leading to permanent data removal. Double-check directories to avoid accidental data loss. Use commands responsibly! π‘οΈποΈππ§Ή
5. To create a file.txt file and to view the content π
To create a "file.txt" file in Linux, you can use the "touch" command. π
For example:
This command will create an empty file named "file.txt" in the current directory.
Next, to view the content of the "file.txt" file, you can use the "cat" command. π±
For example:
6. Add content in fruits.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guavaπππππ₯ππ
To add content to the "file.txt" file, you can use a text editor or the "echo
" command. Here's an example using the "echo
" (-e using for edit) command:
This command adds your content fruit or if you want to append any new line then you can use >>
instead of >
. You can then use the "cat
" command to view the contents of the file:
Now your "file.txt" file is filled with delicious fruits! ππ₯πππ₯ππ Enjoy!
There are some more editors to edit your files e.g.:- Vi, vim, nano etc. You can use any one of them, all are file editors.
7. To Show only top three lines from the file π
To display only the top three fruits from the "file.txt" file, you can use the "head
" command with the "-n" option, specifying the number of lines you want to see. In this case, we want to see the first three lines, which represent the top three lines in the file. ππ₯π
8. To Show only bottom three lines from the file π»
To display only the bottom three fruits from the "file.txt" file, you can use the "tail
" command with the "-n" option, specifying the number of lines you want to see from the end of the file. In this case, we want to see the last three lines, which represent the bottom three lines in the file. π₯ππ
9. To create another file Colors.txt and to view the content π
To create a new file called "Colors.txt" in Linux, you can use the "touch" command followed by the desired filename. This command will create an empty file named "Colors.txt" in the current directory. Now, to view the content of the file, you can use the "cat
" command:
10. Add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey πΉπβͺβ«π΅πππ³
To add the specified content to the "Colors.txt" file with each color on a separate line, you can use the following command:
ππ¨ This command will add the colors πΉ Red, π Pink, β¬ White, β¬ Black, π΅ Blue, π Orange, π Purple, and π³ Grey to the "Colors.txt" file, with each color on its line. If you view the content of the file using the cat
command:
11. To find the difference between fruits.txt and Colors.txt file πππ
To find the difference between the contents of two files, such as "file.txt" and "Colors.txt," you can use the "diff
" command. Here's an example:
ππππ¨ This command will compare the contents of "file.txt" and "Colors.txt" files. If there are any differences between the two files, the diff
command will show them in the output.
With the diff
command, you can easily spot the variations between two files and manage your data more efficiently. ππ
Conclusion π
ππ Congratulations on mastering essential Linux commands! ππ» You've gained valuable skills in file interactions, permissions, and command analysis. From creating files to exploring their contents, you've become proficient in various Linux aspects. ππ By using commands like "ls," "chmod
," and "rm," you're now a Linux command-line pro! "Less" and "more" help you read files effortlessly. ππ You've even created "fruits.txt" and "Colors.txt" and learned to display top and bottom items with "head" and "tail." The "diff" command aids in identifying file differences. πππ Keep practicing and exploring as your Linux adventure thrives! πͺπ‘ Embrace the command line's power and discover new possibilities in the vast Linux world. ππ§ Happy DevOps exploring! πππ§
Stay in the loop with my latest insights and articles on cloud βοΈ and DevOps π by following me on Hashnode, LinkedIn (linkedin.com/in/mahesh-verma-441178250), and GitHub (https://github.com/maheshverma123).
Thank you for reading! π Your support means the world to me. Let's keep learning, growing, and making a positive impact in the tech world together.