Red Hat System Administration I
Efficiently create, move, and remove files and directories by using the shell and various file name matching techniques.
Outcomes
Use wildcards to locate and manipulate files.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command prepares your environment and ensures that all required resources are available.
[student@workstation ~]$ lab start files-review
Instructions
Use the
sshcommand to log in to theserverbmachine as thestudentuser. The system's configuration supports the use of SSH keys for authentication.Create a directory called
project_plansin theDocumentsdirectory. TheDocumentsdirectory is placed in thestudentuser's home directory. Create two empty files in theproject_plansdirectory calledseason1_project_plan.odfandseason2_project_plan.odf.Hint: If the
~/Documentsdirectory does not exist, then use themkdircommand-poption to create it.[student@serverb ~]$
mkdir -p ~/Documents/project_plans[student@serverb ~]$touch \~/Documents/project_plans/{season1,season2}_project_plan.odf[student@serverb ~]$ls -lR Documents/Documents/: total 0 drwxr-xr-x. 2 student student 70 Mar 7 03:50 project_plans Documents/project_plans: total 0 -rw-r--r--. 1 student student 0 Mar 7 03:50 season1_project_plan.odf -rw-r--r--. 1 student student 0 Mar 7 03:50 season2_project_plan.odfCreate sets of empty practice files to use in this lab. If you do not immediately recognize the intended shell expansion shortcut, then use the solution to learn and practice. Use shell tab completion to locate file path names. Create 12 files with
tv_seasonnames in theX_episodeY.ogg/home/studentdirectory. Replace X with the season number and Y with that season's episode, for two seasons of six episodes each.[student@serverb ~]$
touch tv_season{1..2}_episode{1..6}.ogg[student@serverb ~]$ls tv*tv_season1_episode1.ogg tv_season1_episode5.ogg tv_season2_episode3.ogg tv_season1_episode2.ogg tv_season1_episode6.ogg tv_season2_episode4.ogg tv_season1_episode3.ogg tv_season2_episode1.ogg tv_season2_episode5.ogg tv_season1_episode4.ogg tv_season2_episode2.ogg tv_season2_episode6.oggAs the author of a successful series of mystery novels, you are editing your next bestseller's chapters for publishing. Create eight files with
mystery_chapternames. Replace X with the numbers 1 through 8.X.odfUse a single command to create two subdirectories called
season1andseason2under theVideosdirectory to organize the TV episodes. Move the appropriate TV episodes into the season subdirectories. Use only two commands, and specify destinations with relative syntax.Create two subdirectories called
season1andseason2under theVideosdirectory by using a single command.[student@serverb ~]$
mkdir -p Videos/season{1..2}[student@serverb ~]$ls Videosseason1 season2Move the appropriate TV episodes into the season subdirectories by using only two commands.
[student@serverb ~]$
mv tv_season1* Videos/season1[student@serverb ~]$mv tv_season2* Videos/season2[student@serverb ~]$ls -R VideosVideos: season1 season2 Videos/season1: tv_season1_episode1.ogg tv_season1_episode3.ogg tv_season1_episode5.ogg tv_season1_episode2.ogg tv_season1_episode4.ogg tv_season1_episode6.ogg Videos/season2: tv_season2_episode1.ogg tv_season2_episode3.ogg tv_season2_episode5.ogg tv_season2_episode2.ogg tv_season2_episode4.ogg tv_season2_episode6.ogg
Create a two-level directory hierarchy with a single command to organize the mystery book chapters. Create the
my_bestsellersubdirectory under theDocumentsdirectory, and create thechapterssubdirectory under the newmy_bestsellerdirectory. Create three more subdirectories directly under themy_bestsellerdirectory with a single command. Name these subdirectorieseditor,changes, andvacation. You do not need to use themkdir -pcommand to create parents because themy_bestsellerparent directory exists.Create the
my_bestsellerdirectory under theDocumentsdirectory. Create thechaptersdirectory under themy_bestsellerdirectory.[student@serverb ~]$
mkdir -p Documents/my_bestseller/chapters[student@serverb ~]$ls -R DocumentsDocuments: my_bestseller project_plans Documents/my_bestseller: chapters Documents/my_bestseller/chapters: Documents/project_plans: season1_project_plan.odf season2_project_plan.odfCreate three directories called
editor,changes, andvacation, under themy_bestsellerdirectory by using a single command.[student@serverb ~]$
mkdir Documents/my_bestseller/{editor,changes,vacation}[student@serverb ~]$ls -R DocumentsDocuments: my_bestseller project_plans Documents/my_bestseller: changes chapters editor vacation Documents/my_bestseller/changes: Documents/my_bestseller/chapters: Documents/my_bestseller/editor: Documents/my_bestseller/vacation: Documents/project_plans: season1_project_plan.odf season2_project_plan.odf
Change to the
chaptersdirectory. Use the tilde (~) home directory shortcut to move all book chapters to thechaptersdirectory, which is now your current directory. Use the simplest syntax to specify the destination directory.You want to send the first two chapters to the editor for review. Move only those two chapters to the
editordirectory to avoid modifying them during the review. Starting from thechapterssubdirectory, use brace expansion with a range to specify the chapter file names to move and a relative path for the destination directory.While on vacation, you intend to write chapters 7 and 8. Use a single command to move the files from the
chaptersdirectory to thevacationdirectory. Specify the chapter file names by using brace expansion with a list of strings and without using wildcard characters.Change to the
chaptersdirectory and use the tilde (~) home directory shortcut to move all book chapters to thechaptersdirectory.[student@serverb ~]$
cd Documents/my_bestseller/chapters[student@serverb chapters]$mv ~/mystery_chapter* .[student@serverb chapters]$lsmystery_chapter1.odf mystery_chapter4.odf mystery_chapter7.odf mystery_chapter2.odf mystery_chapter5.odf mystery_chapter8.odf mystery_chapter3.odf mystery_chapter6.odfMove the first two chapters to the
editordirectory. Use brace expansion with a range to specify the chapter file names to move and a relative path for the destination directory.[student@serverb chapters]$
mv mystery_chapter{1..2}.odf ../editor[student@serverb chapters]$lsmystery_chapter3.odf mystery_chapter5.odf mystery_chapter7.odf mystery_chapter4.odf mystery_chapter6.odf mystery_chapter8.odf [student@serverb chapters]$ls ../editormystery_chapter1.odf mystery_chapter2.odfUse a single command to move the chapters 7 and 8 from the
chaptersdirectory to thevacationdirectory. Specify the chapter file names by using brace expansion with a list of strings and without using wildcard characters.[student@serverb chapters]$
mv mystery_chapter{7,8}.odf ../vacation[student@serverb chapters]$lsmystery_chapter3.odf mystery_chapter5.odf mystery_chapter4.odf mystery_chapter6.odf [student@serverb chapters]$ls ../vacationmystery_chapter7.odf mystery_chapter8.odf
Change your working directory to
~/Videos/season2, and then copy the first episode of the season to thevacationdirectory. Use a singlecdcommand to change from your working directory to the~/Documents/my_bestseller/vacationdirectory. List its files. Use the previous working directory argument to return to theseason2directory. (This argument succeeds if the last directory change with thecdcommand used only one command rather than severalcdcommands.) From theseason2directory, copy the episode 2 file into thevacationdirectory. Use the shortcut again to return to thevacationdirectory.Change your working directory to
~/Videos/season2, and then copy the first episode of the season to thevacationdirectory.[student@serverb chapters]$
cd ~/Videos/season2[student@serverb season2]$cp *episode1.ogg ~/Documents/my_bestseller/vacationUse a single
cdcommand to change from your working directory to the~/Documents/my_bestseller/vacationdirectory, list its files, and use the-argument to return to the previous directory. Copy the episode 2 file into thevacationdirectory. Use thecdcommand with the-argument to return to thevacationdirectory.[student@serverb season2]$
cd ~/Documents/my_bestseller/vacation[student@serverb vacation]$lsmystery_chapter7.odf mystery_chapter8.odf tv_season2_episode1.ogg [student@serverb vacation]$cd -/home/student/Videos/season2 [student@serverb season2]$cp *episode2.ogg ~/Documents/my_bestseller/vacation[student@serverb season2]$cd -/home/student/Documents/my_bestseller/vacation [student@serverb vacation]$lsmystery_chapter7.odf tv_season2_episode1.ogg mystery_chapter8.odf tv_season2_episode2.ogg
The authors of chapters 5 and 6 want to experiment with possible changes. Copy both files from the
~/Documents/my_bestseller/chaptersdirectory to the~/Documents/my_bestseller/changesdirectory to prevent these changes from modifying original files. Navigate to the~/Documents/my_bestsellerdirectory. Use square-bracket pattern matching to specify which chapter numbers to match in thefilenameargument of thecpcommand.[student@serverb vacation]$
cd ~/Documents/my_bestseller[student@serverb my_bestseller]$cp chapters/mystery_chapter[56].odf changes[student@serverb my_bestseller]$ls chaptersmystery_chapter3.odf mystery_chapter5.odf mystery_chapter4.odf mystery_chapter6.odf [student@serverb my_bestseller]$ls changesmystery_chapter5.odf mystery_chapter6.odfChange your current directory to the
changesdirectory and use thedate +%Fcommand with command substitution to copy themystery_chapter5.odffile to a new file that includes the full date. Use themystery_chapter5_YYYY-MM-DD.odfname format.By using command substitution with the
date +%scommand, make another copy ofmystery_chapter5.odf, and append the current time stamp (as the number of seconds since the epoch, 1970-01-01 00:00 UTC) to ensure a unique file name.[student@serverb my_bestseller]$
cd changes[student@serverb changes]$cp mystery_chapter5.odf \mystery_chapter5_$(date +%F).odf[student@serverb changes]$cp mystery_chapter5.odf \mystery_chapter5_$(date +%s).odf[student@serverb changes]$lsmystery_chapter5_1646644424.odf mystery_chapter5.odf mystery_chapter5_2022-03-07.odf mystery_chapter6.odfAfter further review, you decide that you do not need the plot changes. Delete the
changesdirectory.If it is necessary, then navigate to the
changesdirectory and delete all the files within the directory. You cannot delete a directory when it is the current working directory.Change to the parent directory of the
changesdirectory. Try to delete the empty directory by using thermcommand without the-rrecursive option. This attempt should fail. Finally, use thermdircommand to delete the empty directory, which succeeds.When the vacation is over, you no longer need the
vacationdirectory. Delete it by using thermcommand with the recursive option.When finished, return to the
studentuser's home directory.Delete the
changesdirectory. Change to the parent directory of thechangesdirectory, and try to delete the empty directory by using thermcommand without the-rrecursive option, which should fail. Use thermdircommand to delete the empty directory.[student@serverb changes]$
rm mystery*[student@serverb changes]$cd ..[student@serverb my_bestseller]$rm changesrm: cannot remove 'changes': Is a directory [student@serverb my_bestseller]$rmdir changes[student@serverb my_bestseller]$lschapters editor vacationDelete the
vacationdirectory by using thermcommand with the-roption. Return to thestudentuser's home directory.[student@serverb my_bestseller]$
rm -r vacation[student@serverb my_bestseller]$lschapters editor [student@serverb my_bestseller]$cd[student@serverb ~]$
Create a hard link to the
~/Documents/project_plans/season2_project_plan.odffile called~/Documents/backups/season2_project_plan.odf.back. A hard link protects against accidental deletion of the original file and keeps the backup file updated as you change the original file.Hint: If the
~/Documents/backupsdirectory does not exist, then use themkdircommand to create it.Create a hard link to the
~/Documents/project_plans/season2_project_plan.odffile called~/Documents/backups/season2_project_plan.odf.back.[student@serverb ~]$
mkdir ~/Documents/backups[student@serverb ~]$ln ~/Documents/project_plans/season2_project_plan.odf \~/Documents/backups/season2_project_plan.odf.back[student@serverb ~]$ls -lR ~/Documents//home/student/Documents/: total 0 drwxr-xr-x. 2 student student 43 Mar 7 04:18 backups drwxr-xr-x. 4 student student 36 Mar 7 04:16 my_bestseller drwxr-xr-x. 2 student student 70 Mar 7 03:50 project_plans /home/student/Documents/backups: total 0 -rw-r--r--. 2 student student 0 Mar 7 03:50 season2_project_plan.odf.back /home/student/Documents/my_bestseller: total 0 drwxr-xr-x. 2 student student 118 Mar 7 04:07 chapters drwxr-xr-x. 2 student student 62 Mar 7 04:06 editor /home/student/Documents/my_bestseller/chapters: total 0 -rw-r--r--. 1 student student 0 Mar 7 03:56 mystery_chapter3.odf -rw-r--r--. 1 student student 0 Mar 7 03:56 mystery_chapter4.odf -rw-r--r--. 1 student student 0 Mar 7 03:56 mystery_chapter5.odf -rw-r--r--. 1 student student 0 Mar 7 03:56 mystery_chapter6.odf /home/student/Documents/my_bestseller/editor: total 0 -rw-r--r--. 1 student student 0 Mar 7 03:56 mystery_chapter1.odf -rw-r--r--. 1 student student 0 Mar 7 03:56 mystery_chapter2.odf /home/student/Documents/project_plans: total 0 -rw-r--r--. 1 student student 0 Mar 7 03:50 season1_project_plan.odf -rw-r--r--. 2 student student 0 Mar 7 03:50 season2_project_plan.odfNotice that the link count is
2for bothseason2_project_plan.odf.backandseason2_project_plan.odffiles.Return to the
workstationsystem as thestudentuser.[student@serverb ~]$
exitlogout Connection to serverb closed. [student@workstation ~]$