Chapter 17.1: Manage Files from the Command Line
Welcome to the final chapter of our RH124 course! This chapter offers a comprehensive review of the course material and five practical labs to solidify our knowledge. Today, we will focus on the first lab, "Manage Files from the Command Line," which provides essential skills for effective file management. In this lab, we will learn how to manipulate and edit files using the command line interface. The main objectives include displaying a specific number of lines from text files, redirecting the output to another file, and editing text files directly from the command line. File management is a crucial aspect of Linux system administration. The ability to organize, access, and manipulate data directly impacts productivity and efficiency. Through this lab, we will not only enhance our command line proficiency but also gain a deep understanding of the principles that govern file management in Linux. Let's dive into the lab and explore how to manage files from the command line. By the end of this session, you will possess valuable techniques that will strengthen your capabilities as a Linux administrator and facilitate seamless file management in your future endeavours. Get ready to unlock new horizons in file management prowess as we embark on this exhilarating lab. Stay tuned for step-by-step instructions that will empower you with the skills to manage files effectively using the command line!
TASK
- Log in to
serverb
as thestudent
user. - Create the
/home/student/grading
directory. - Create three empty files called
grade1
,grade2
, andgrade3
, in the/home/student/grading
directory. - Capture the first five lines of the
/home/student/bin/manage
file in the/home/student/grading/review.txt
file. - Append the last three lines of the
/home/student/bin/manage
file to the/home/student/grading/review.txt
file. Do not overwrite any existing text in the/home/student/grading/review.txt
file. - Copy the
/home/student/grading/review.txt
file to the/home/student/grading/review-copy.txt
file. - Edit the
/home/student/grading/review-copy.txt
file so that theTest JJ
line appears twice. - Edit the
/home/student/grading/review-copy.txt
file to remove theTest HH
line. - Edit the
/home/student/grading/review-copy.txt
file so that a line withA new line
exists between theTest BB
line and theTest CC
line. - Create the
/home/student/hardcopy
hard link to the/home/student/grading/grade1
file. You must create the hard link after completing the earlier step to create the/home/student/grading/grade1
file. - Create the
/home/student/softcopy
symbolic link to the/home/student/grading/grade2
file. - Save the output of a command that lists the contents of the
/boot
directory to the/home/student/grading/longlisting.txt
file. The output should be a "long listing" that includes file permissions, owner and group owner, size, and modification date of each file. The output should omit hidden files.
SOLUTION
- Log in to
serverb
as thestudent
user using the command:ssh student@serverb
. - Create the
/home/student/grading
directory by running the command:mkdir /home/student/grading
. - Navigate to the
/home/student/grading
directory using the command:cd /home/student/grading
. Create three empty files namedgrade1
,grade2
, andgrade3
in the/home/student/grading
directory. Use the command:touch grade1 grade2 grade3
. - Capture the first five lines of the
/home/student/bin/manage
file and append them to the/home/student/grading/review.txt
file. Run the command:head -n5 /home/student/bin/manage > /home/student/grading/review.txt
. - Append the last three lines of the
/home/student/bin/manage
file to the existing content in the/home/student/grading/review.txt
file. Use the command:tail -n3 /home/student/bin/manage >> /home/student/grading/review.txt
. - Copy the
/home/student/grading/review.txt
file to/home/student/grading/review-copy.txt
using the command:cp /home/student/grading/review.txt /home/student/grading/review-copy.txt
. - Open the
/home/student/grading/review-copy.txt
file with the Vim editor using the command:vim /home/student/grading/review-copy.txt
. Inside Vim, navigate to the line containing 'Test JJ'. Press 'yy' to copy the line, then move to the next line and press 'p' to paste the line. - While still in the Vim editor, navigate to the line with 'Test HH' and press 'dd' to delete the line.
- To insert a new line between the 'Test BB' and 'Test CC' lines, press 'i' to switch to insert mode. Move to the 'Test BB' line, press 'Enter' to start a new line, and type the text 'A new line'. Press 'Esc' to exit insert mode and save the changes.
- Create a hard link by using the following command:
ln /home/student/grading/grade1 /home/student/hardcopy
. Make sure you have already completed the previous step to create the/home/student/grading/grade1
file. - Create a symbolic link using the command:
ln -s /home/student/grading/grade2 /home/student/softcopy
. - Save the long listing of the contents in the
/boot
directory to the/home/student/grading/longlisting.txt
file. Execute the command:ls -l /boot > /home/student/grading/longlisting.txt
to complete this task.
Conclusion
In this lab, we embarked on a journey to enhance our file management skills through the command line interface. By mastering various commands and techniques, we gained the ability to manipulate files, extract specific lines, redirect output, edit file contents, and create links.
Throughout the lab, we created the /home/student/grading
directory and populated it with empty files. We captured and appended lines from a source file to another file, ensuring that no existing content was overwritten. We also made modifications to the copied file, duplicating lines, removing specific lines, and inserting new lines.
Additionally, we explored the concept of hard links and symbolic links, creating both types of links to demonstrate their functionality and usage. Lastly, we obtained a comprehensive listing of the /boot
directory, saving the output to a text file for reference.
By successfully completing these tasks, we have expanded our capabilities as Linux system administrators. Proficiency in file management from the command line empowers us to efficiently handle files, organize data, and streamline our workflow.
We hope this lab has provided you with valuable insights and practical knowledge. Now, it's time to hear from you! We would love to learn about your experience with managing files from the command line. Did you find the tasks challenging or straightforward? Were there any particular commands or techniques that caught your interest? Share your thoughts, questions, and feedback in the comments below.
Remember, mastering file management is an essential skill for any Linux administrator. As you continue your journey, keep exploring and practising these techniques to further refine your abilities. Stay tuned for more exciting labs and tutorials that will continue to expand your knowledge in the world of Linux system administration.
We look forward to hearing from you and wish you continued success in your learning journey!