Day 3 - Basic Linux Commands and with Twists
Managing files and directories in Unix can be intimidating for beginners. But with the right commands, you can efficiently handle tasks such as viewing file contents, modifying permissions, and manipulating data. In this blog, I will cover the following topics:
Viewing the content of a file and displaying line numbers.
Changing file access permissions.
Checking your command history.
Removing directories and their contents.
Creating and manipulating text files.
Finding common lines between files.
Counting lines, words, and characters in files.
Let's dive in! ๐โโ๏ธ
1. View the Content of a File and Display Line Numbers
To view the content of a file with line numbers, use the cat
command with the -n
option.
cat -n example.txt
2. Change the Access Permissions of Files
To change a file's access permissions to make it readable, writable, and executable by the owner only, use the chmod
command.
chmod 700 example.txt
๐ Explanation:
7
(owner): read (4) + write (2) + execute (1) = 70
(group and others): no permissions
3. Check the Last 10 Commands You Have Run
To view the last 10 commands you executed, use the history
command.
history | tail -10
๐ Explanation:
history
: lists all the commands you've executed.tail -10
: shows the last 10 lines of the history.
4. Remove a Directory and All Its Contents
To remove a directory and all its contents, use the rm
command with the -r
(recursive) option.
rm -r directory_name
โ ๏ธ Caution: This command is irreversible. Make sure you double-check before executing it.
5. Create a fruits.txt File and Display its Content
Create a fruits.txt
file and add some fruit names, one per line.
echo -e "Apple\nMango\nBanana\nCherry\nKiwi\nOrange\nGuava" > fruits.txt
cat fruits.txt
๐ Example:
The
-e
option enables interpretation of backslash escapes.\n
represents a new line.
6. Append "Pineapple" to devops.txt
Create a devops.txt
file and append "Pineapple" to the end of the file.
echo -e "Apple\nMango\nBanana\nCherry\nKiwi\nOrange\nGuava" > devops.txt
echo "Pineapple" >> devops.txt
cat devops.txt
๐ Example:
- The
>>
operator appends text to the file.
7. Show the First Three Fruits in Reverse Order
To display the first three fruits in reverse order, use head
and tac
commands.
head -3 fruits.txt | tac
๐ Example:
head -3
shows the first three lines.tac
reverses the lines.
8. Show the Bottom Three Fruits Alphabetically
To display the bottom three fruits and sort them alphabetically, use tail
and sort
commands.
tail -3 fruits.txt | sort
๐ Example:
tail -3
shows the last three lines.sort
arranges them alphabetically.
9. Create Colors.txt and Display its Content
Create a Colors.txt
file and add some color names, one per line.
echo -e "Red\nPink\nWhite\nBlack\nBlue\nOrange\nPurple\nGrey" > Colors.txt
cat Colors.txt
๐ Example:
10. Prepend "Yellow" to Colors.txt
To prepend "Yellow" to the beginning of Colors.txt
, use sed
command.
sed -i '1iYellow' Colors.txt
cat Colors.txt
๐ Example:
sed -i '1iYellow'
: inserts "Yellow" at the first line.
11. Find and Display Common Lines Between Two Files
To find and display the common lines between fruits.txt
and Colors.txt
, use the comm
command.
comm -12 <(sort fruits.txt) <(sort Colors.txt)
๐ Example:
comm -12
: displays common lines.<(sort file)
: sorts the files before comparing.
12. Count the Number of Lines, Words, and Characters
To count the number of lines, words, and characters in fruits.txt
and Colors.txt
, use the wc
command.
wc fruits.txt Colors.txt
๐ Example:
wc
: word count, displays lines, words, and characters.
Subscribe to my newsletter
Read articles from Himanshu Palhade directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by