As a DevOps engineer, mastering Linux commands is crucial for efficient system management and automation. In this blog post, we'll explore some essential Linux commands that every DevOps professional should know. Let's dive in!
- View the content of a file and display line numbers To view a file's content with line numbers, use the cat or nl command. Here's an example:
cat -n filename.txt
or
nl filename.txt
The -n option in cat displays line numbers, while nl directly shows the content with line numbers.
- Change the access permissions of files to make them readable, writable, and executable by the owner only To modify file permissions so that only the owner has read, write, and execute rights, use the chmod command:
chmod 700 filename.txt
In this case, 7 gives the owner all permissions (read, write, and execute), while 0 denies all permissions for the group and others.
- Check the last 10 commands you have run To view the last 10 commands you executed, use the history command:
history | tail -10
This displays the last 10 entries from your command history.
- Remove a directory and all its contents To remove a directory and all of its contents, including subdirectories and files, use the rm command with the -r and -f flags:
rm -rf directory_name
-r: Recursively removes directories and their contents.
-f: Forces removal without prompting for confirmation.
- Create a fruits.txt file, add content (one fruit per line), and display the content You can create the fruits.txt file and add content with the echo command or a text editor like nano:
echo -e "Apple\nMango\nBanana\nCherry\nKiwi\nOrange\nGuava" > fruits.txt
cat fruits.txt
This command creates a file called fruits.txt with each fruit on a new line, and cat displays the file content.
- Add content in devops.txt (one in each line), and then append "Pineapple" to the end of the file To add fruits to devops.txt and then append "Pineapple" at the end:
echo -e "Apple\nMango\nBanana\nCherry\nKiwi\nOrange\nGuava" > devops.txt
echo "Pineapple" >> devops.txt
cat devops.txt
The >> operator appends data to the file.
- Show the first three fruits from the file in reverse order To display the first three fruits in reverse order, use a combination of head and tac:
head -3 fruits.txt | tac
head -3 extracts the first three lines.
tac reverses the order of the lines.
- Show the bottom three fruits from the file, and then sort them alphabetically To display the bottom three fruits and sort them alphabetically, use tail and sort:
tail -3 fruits.txt | sort
tail -3 extracts the last three lines.
sort organizes them alphabetically.
- Create another file Colors.txt, add content (one color per line), and display the content To create Colors.txt and add content:
echo -e "Red\nPink\nWhite\nBlack\nBlue\nOrange\nPurple\nGrey" > Colors.txt
cat Colors.txt
This adds each color to a new line in the Colors.txt file.
- Add content in Colors.txt, then prepend "Yellow" to the beginning of the file To prepend "Yellow" at the beginning of Colors.txt:
echo -e "Yellow\n$(cat Colors.txt)" > Colors.txt
cat Colors.txt
This command adds "Yellow" at the start by reading the original file content and prepending it with "Yellow."
- Find and display the lines that are common between fruits.txt and Colors.txt To find the common lines between the two files:
comm -12 <(sort fruits.txt) <(sort Colors.txt)
The comm command compares the two sorted files and shows the common lines.
- Count the number of lines, words, and characters in both fruits.txt and Colors.txt To count the number of lines, words, and characters, use the wc (word count) command:
wc fruits.txt Colors.txt
The output displays the counts for each file in the format:
lines words characters filename.
By mastering these Linux commands, DevOps engineers can streamline their workflow and enhance their productivity. Remember to practice these commands in a safe environment before applying them to production systems. Happy coding!
Top comments (0)