Table of contents
- Task 1: View the Content of a File with Line Numbers ๐
- Command :
- Example :
- Task 2: Change File Permissions (Read, Write, Execute) ๐
- Command :
- Example :
- Task 3: Check the Last 10 Commands Executed ๐
- Command :
- Example :
- Task 4: Remove a Directory and All Its Contents ๐๏ธ
- Command :
- Example :
- Task 5: Create and Display a File ๐
- Command:
- Example :
- Task 6: Append to a File ๐ฅ
- Example :
- Task 7: Display the First 3 Fruits in Reverse Order ๐
- Example :
- Task 8: Show the Last 3 Fruits and Sort Alphabetically ๐ค
- Command:
- Example :
- Task 9: Create a colors File and Display Content
- Command:
- Example :
- Task 10: Prepend Content to a File ๐ ๏ธ
- Task 12: Count Lines, Words, and Characters ๐งฎ
In today's task, we will cover essential Linux commands that are frequently used in day-to-day DevOps activities. This practical guide includes viewing files, managing permissions, handling file contents, and more. Let's dive in!
Task 1: View the Content of a File with Line Numbers ๐
Linux provides the cat
command to display the contents of a file. To display line numbers along with the content, we use the -n
option.
Command :
cat -n chintamani.txt
Example :
This command prints the content of chintamani.txt
with line numbers.
Task 2: Change File Permissions (Read, Write, Execute) ๐
The chmod
command is used to change file permissions. Here, we make the file readable, writable, and executable by the owner only.
Command :
chmod 700 chintamani.txt
Example :
This command sets permissions for the owner to read, write, and execute the file.
Task 3: Check the Last 10 Commands Executed ๐
We often need to check our command history. The history
command helps, and we can use tail
to show only the last 10 commands.
Command :
history | tail -10
Example :
his command shows the last 10 commands that you executed in your terminal.
Task 4: Remove a Directory and All Its Contents ๐๏ธ
The rm
command is used to remove files and directories. To remove a directory along with all its contents, use the -r
option.
Command :
rm -r chintamani.txt
Example :
This will remove the directory and all its files permanently.
Task 5: Create and Display a File ๐
Creating a file and adding content is easy with the echo
command. We can use it to write multiple lines into a file.
Command:
vim fruits.txt
cat fruits.txt
Example :
This will create a fruits.txt
file with fruit names on each line and then display the contents using cat
.
Task 6: Append to a File ๐ฅ
Appending data to an existing file is also simple using the echo
command.
Command :
echo "Pineapple" >> fruits.txt
cat fruits.txt
Example :
This will append the word "Pineapple" to the end of the fruits.txt
file.
Task 7: Display the First 3 Fruits in Reverse Order ๐
We can use head
to show the first few lines of a file, and tac
to reverse them.
Command :
head -3 fruits.txt | tac
Example :
This command will display the first three fruits from the file in reverse order.
Task 8: Show the Last 3 Fruits and Sort Alphabetically ๐ค
Similarly, we can use tail
to get the last few lines and sort
to organize them alphabetically.
Command:
tail -3 fruits.txt | sort
Example :
This shows the last 3 fruits, sorted in alphabetical order.
Task 9: Create a colors File and Display Content
Letโs create a Colors.txt
file and add colors line by line.
Command:
echo -e "Red\nPink\nWhite\nBlack\nOrange\nPurple\nGrey" > colors.txt
cat colors.txt
Example :
his command creates a colors.txt
file and lists colors one by one.
Task 10: Prepend Content to a File ๐ ๏ธ
Prepending content to a file is not as straightforward as appending. But we can manage using a trick.
Command:
echo "Yellow" | cat - colors.txt > temp && mv temp colors.txt
cat colors.txt
Example :
This will add "Yellow" at the beginning of colors.txt
.
Task 11: Find Common Lines Between Two Files ๐
To compare two files and find common lines, we use the comm
command.
Command:
comm -12 <(sort fruits.txt) <(sort colors.txt)
Example :
This command will show the lines common between fruits.txt
and colors.txt
.
Task 12: Count Lines, Words, and Characters ๐งฎ
The wc
command gives the number of lines, words, and characters in a file.
Command:
wc fruits.txt colors.txt
Example :
This will count and display the lines, words, and characters for both files.
Wrapping Up ๐
These are some of the most basic but useful Linux commands for managing files and permissions. Performing these tasks not only helps you understand the command-line better but also builds a strong foundation for working with Linux in DevOps environments.
#90DaysofDevOps #TrainWithShubham #CloudComputing #Automation #Scaling #Infrastructure