Day 3 - Basic Linux Commands
Basic Linux Commands: A Beginner's Guide
Learning Linux can be overwhelming for new users. In this guide, Iโll cover some basic Linux commands to get you started with common tasks such as viewing files, changing permissions, and manipulating text files.
Viewing a File
To view the content of a file, you can use the following commands:
cat <file_name>
: Displays the entire content of a file.more <file_name>
orless <file_name>
: Allows you to navigate through a file one page at a time.head -n <number> <file_name>
: Shows the firstn
lines of a file.tail -n <number> <file_name>
: Shows the lastn
lines of a file.
For example, to view the content of a file named example.txt
, you can use:
cat example.txt
Changing File Permissions
In Linux, you can change the access permissions of files using the chmod
command. Permissions are defined by three groups: the owner, the group, and others.
chmod 644 <file_name>
: Sets read-write permissions for the owner and read-only for others.chmod 755 <file_name>
: Sets read-execute permissions for all, but only the owner can write.
To grant execute permissions to a script script.sh
, you can use:
chmod +x script.sh
Checking Command History
To check the list of commands you've executed in the current session, you can use the history
command:
history
Removing a Directory
To remove a directory or folder, you can use rmdir
for empty directories and rm -r
for non-empty ones:
rmdir <directory_name>
: Removes an empty directory.rm -r <directory_name>
: Removes a directory and its contents.
For example, to remove a directory named old_folder
, you can use:
rm -r old_folder
Creating and Manipulating Text Files
To create a new file and add content, you can use the echo
or touch
command, followed by a redirection operator (>
, >>
).
- To create a file named
fruits.txt
and view its content, use:
touch fruits.txt
cat fruits.txt
- To add content to
devops.txt
, one item per line, use:
echo "Apple" > devops.txt
echo "Mango" >> devops.txt
echo "Banana" >> devops.txt
echo "Cherry" >> devops.txt
echo "Kiwi" >> devops.txt
echo "Orange" >> devops.txt
echo "Guava" >> devops.txt
- To show only the top three fruits from
devops.txt
, use:
head -n 3 devops.txt
- To show only the bottom three fruits from
devops.txt
, use:
tail -n 3 devops.txt
Creating a Second Text File and Finding Differences
To create a new file named Colors.txt
and add content, use:
echo "Red" > Colors.txt
echo "Pink" >> Colors.txt
echo "White" >> Colors.txt
echo "Black" >> Colors.txt
echo "Blue" >> Colors.txt
echo "Orange" >> Colors.txt
echo "Purple" >> Colors.txt
echo "Grey" >> Colors.txt
To find the differences between fruits.txt
and Colors.txt
, you can use the diff
command:
diff fruits.txt Colors.txt
This command will display the differences between the two files, allowing you to see where they differ.
Conclusion
These basic Linux commands can help you get started with essential tasks in a Linux environment. Whether you're creating files, manipulating text, changing permissions, or exploring command history, these commands are the building blocks for more advanced Linux usage. Keep experimenting with them to become more comfortable in a Linux terminal.
Thank you for reading our DevOps blog post. We hope you found it informative and helpful. If you have any questions or feedback, please don't hesitate to contact us.
I hope this helps!
Happy Learningโจ
Subscribe to my newsletter
Read articles from Rahul Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Rahul Gupta
Rahul Gupta
Hey there! ๐ I'm Rahul Gupta, a DevOps Engineer passionate about all things AWS DevOps Technology. Currently, on a learning adventure, I'm here to share my journey and Blogs in the world of cloud and DevOps. ๐ ๏ธ My focus? Making sense of AWS services, improving CI/CD, and diving into infrastructure as code. Whether you're fellow interns or curious enthusiasts, let's grow together in the vibrant DevOps space. ๐ Connect with me for friendly chats, shared experiences, and learning moments. Here's to embracing the learning curve and thriving in the exciting world of AWS DevOps Technology!