Day 3 Task → Basic Linux Commands with a Twist
This is #90DaysofDevops challenge under the guidance of Shubham Londhe sir.
Are you ready to dive into the world of Linux commands? Let’s explore some fundamental commands that every Linux user should know.
- View the content of a file and display line numbers.
To see what’s written in a file, we can use the cat command and for line numbers we use -n.
2. Change the access permissions of files to make them readable, writable, and executable by the owner only.
To modify the access permissions of files, you can use the chmod
command.
r = read
w = write
x = execute
Here, we only give permissions to owner that’s why group and other space will be filled with (-).
Here’s what 700
means:
‘7’ for the owner: read (4), write (2), and execute (1) permissions (4+2+1 = 7).
‘0’ for the group: no permissions.
‘0’ for others: no permissions.
3. Check the last 10 commands you have run.
Here, we run hitory | tail -n 10 then we get last 10 commands.
Here’s what each part of this command does:
history: Displays the command history.
| : Pipes the output of history to another command.
tail -n 10: Shows the last 10 lines of the piped output.
4. Remove a directory and all its contents.
To remove a directory and all its contents, we use ‘rm’ command with the ‘-r’ (recursive) option.
Here, we deleted directory and with their files also.
5. Create afruits.txt
file, add content (one fruit per line), and display the content.
To create a file in Linux, we can use ‘touch’ command or directly open the file with a text editor like “ vi ”
After using above (vi) command we add contents in ‘fruits.txt’ file. Then, we use to display content using cat command
6. Add content indevops.txt
(one in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava. Then, append "Pineapple" to the end of the file.
To append “Pineapple” to the file, use ‘echo’ command with the append operator ‘>>’.
Here, we only use echo to add Pineapple in fruits.txt file.
7. Show the first three fruits from the file in reverse order.
To reverse the order of the first three lines, we’ll use a combination of the ‘head’ and ‘tac’ commands. The ‘tac’ command is the reverse of the ‘cat’ command and displays lines in reverse order:
8. Show the bottom three fruits from the file, and then sort them alphabetically.
To display the bottom three lines of the devops.txt file, we can use the ‘tail’ command after that we use ‘sort’ command to arrange alphabetically.
9**.Create another file** Colors.txt
, add content (one color per line), and display the content.
10. Add content inColors.txt
(one in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey. Then, prepend "Yellow" to the beginning of the file.
The command echo -e "Yellow\n$(cat colors.txt)" > colors.txt
is used to prepend the text "Yellow" followed by a newline to the contents of the file colors.txt
. Here's a step-by-step breakdown of what this command does:
echo -e "Yellow\n$(cat colors.txt)"
:
echo
: A command used to display a line of text.-e
: An option that enables the interpretation of backslash escapes."Yellow\n$(cat colors.txt)"
: The text to be echoed. It includes:"Yellow\n"
: The text "Yellow" followed by a newline character (\n
).$(cat colors.txt)
: A command substitution that executescat colors.txt
and inserts its output at this point.cat colors.txt
reads the contents of the filecolors.txt
.
2. > colors.txt
:
>
: The redirection operator that directs the output of theecho
command to the filecolors.txt
, overwriting the file's existing contents.
11. Find and display the lines that are common betweenfruits.txt
andColors.txt
.
“comm -12 <(sort fruits.txt) <(sort colors.txt)”
comm -12
: displays common lines.<(sort file)
: sorts the files before comparing.
12. Count the number of lines, words, and characters in bothfruits.txt
andColors.txt
.
We used ‘wc ‘ command to count the number of lines, words and characters in files.
Happy Learning🚀🌟