Page 1 of 1

LINUX Intro: The lab for this lesson will explore how directories affect some of the utilities we've covered in the past

Posted: Sun Jul 03, 2022 12:00 pm
by answerhappygod
LINUX
Intro:The lab for this lesson will explore how directories affect some ofthe utilities we've covered in the past, as well as introduce a fewnew utilities. To begin the lab, please use the script command torecord your session. Activity 1: mkdirWe've looked at this before, but today we're going to explore howto use the mkdir command. We know that "mkdir mydir" willcreate a directory in the current working directory,but mkdir can be also used with both relative andabsolute directory paths. For example, let's try thefollowing:$ mkdir ~/cis18a_lesson12a$ mkdir ~/cis18a_lesson12bWe should now have two (at least!) directories in the user's homedirectory. As discussed in the lecture, you can add the -p optionto mkdir to force mkdir to create any requiredparent directories. In other words, if one or more ancestors of thedirectory you are trying to create does not exist,the mkdir command will simply create them:$ mkdir -p ~/cis18a_lesson12c/a/b/c/d$ cd ~/cis18a_lesson12c/a/b/c/d$ pwdUse the man page to figure out how to create~/cis18a_lesson12d/e/f/g/h and force the system to show you eachand every directory that it makes along the way. Hint: You onlyneed to add a single option to the create string you've previouslyused.Activity 2: ls and directoriesWe've previously looked at several uses ofthe ls command, but today we're going to cover howthe ls command works with directories. We'll start byjumping to the home directory, and then looking at a list of thedirectory:$ cd ~$ ls -lAll directories should be marked with a "d" on the far left. Whenyou run the ls command and provide a directory name,the ls command will show you the contents of thatdirectory:$ ls -l cis18a_lesson12cHowever, you can override this behavior with the -d option:$ ls -ld cis18a_lesson12cUse the manual page for ls to learn about the options,then run the command that will display a list of files matching thefollowing criteria:* almost all files (don't bother showing the implied . and..),* sorted by modification time,* with the most recently modified file at the bottom of thelist,* and display long infoHINT: The entries above are separated into several lines for areason. Also, on the Voyager system, you may not need to includethe -d entry.Activity 3: rmdir and optionsIf you try to just remove a directory:$ rm ~/cis18a_lesson12c... you'll get a weird error message statingthat rm cannot remove it because it's a directory. Thisis a pretty good thing, because it prevents you from accidentallydeleting a significant number of files with a relatively simplecommand. To remove a directory, Linux providesthe rmdir (remove directory) command:$ cd ~$ ls -l$ mkdir tempdir$ ls -l$ rmdir tempdir$ ls -lJust as the mkdir command can take the -p command,the rmdir can as well. Let's explore what this lookslike; we'll use the -v option so we can see what the system isdoing.$ mkdir -pv tempa/b/c/d$ rmdir -pv tempa/b/c/dOpen the man page for rmdir and look for the optioncalled: --ignore-fail-on-non-empty. You might be tempted to usethis option to attempt to remove a non-empty directory. Forexample:$ mkdir -p tempa/b$ rmdir -v --ignore-fail-on-non-empty tempa$ ls -lThe result of the above command is likely not what you expected tosee. The --ignore-fail-on-non-empty option simply suppresses theerror message, it does not force the expected behavior. Instead,see the rm entry in this lesson.Activity 4: rm and directoriesThe rm command can be configured to work with directoriesin addition to files, though it's very easy to temporarily forgetwhat you're doing and accidentally remove a LOT of files. Let'slook at a rather dangerous entry - be sure you type the commandsbelow exactly before you press enter.$ mkdir ~/tempdir$ cd ~/tempdir$ mkdir -pv a/b/c/d$ touch a/b/c/d/a.txt$ rm -rfv aThe final command says, "recursively delete all files and don't askme to confirm anything."* -r: recursive - meaning delete any subdirectories first* -f: force - meaning don't ask for confirmation (if you leave thisout, directories will not be removed.)* -v: verbose - report on every directory you've deleted.The -f option also forces the system to remove directory entries aswell. If you want to only remove all the files in those directorieswithout deleting the folders, you may want to experiment withleaving out the -f option. When you are running as a regular user,you typically will not need the -f option above, but in the eventyou are working as root on your own machine, the system willrequire this option. In order to make things a bit less dramatic,the rm command provides the -i (interactive) option.Notice the difference between what happened above and what happenswhen you use these commands:$ mkdir ~/tempdir$ cd ~/tempdir$ mkdir -pv a/b/c/d$ touch a/b/c/d/a.txt$ rm -riv aOrder of options:Note that the order in which you specify options can have adramatic effect on the task you are attempting to accomplish. Forexample, if we have the following setup:$ mkdir ordertest$ cd ordertestCompare what will happen if we type the following entries:$ touch aa.txt ab.txt ac.txt$ rm -rfiv a?.txt... vs ...$ touch aa.txt ab.txt ac.txt$ rm -rifv a?.txtSince -f and -i are mutually exclusive, the system will enforce theLAST OPTION IT SEES. The first entry will ask you to verify eachfile it encounters. The second entry will remove them all with noverificationActivity 5: mv and directoriesWe've previously looked at how to use the mv command torename files. Now we'll explore the primary use for mv -moving files:$ mkdir ~/tempdir$ cd ~/tempdir$ touch a.txt$ mv a.txt ..$ ls -lThe commands above create a file called "a.txt" in the tempdirdirectory. Next, they mv the file out of the currentworking directory and into the user's home directory. Rather thanproviding two file names (as we did before when renaming a file),we simply provide a file to be moved and the directory into whichwe wish to move it.Use the man page for mv and figure out how to perform thesteps below, but:a) ONLY move the a.txt file if it's newer than the one that alreadyexists in the parent directory, andb) only require a prompt before overwriting the file$ cd ~/tempdir$ touch a.txt$ touch ../a.txt$ mv [ ????? ] a.txt ..$ ls -lActivity 6: cp and directoriesFinally, let's look at the effect of the cp (copy)command on directories. The cp command can can be usedwith many of the same attributes above to create copies ofdirectories and their contents. For example:$ mkdir -p a/b/c/d$ mkdir -pv a/b/c/d$ touch a/b/c/d/a.txt$ touch a/b/c/d/b.txt$ cp -rv a b$ ls -lPay close attention to the output of the last two entries and makesure you understand what it's doing. Open the man page forthe cp command and take a look at the various optionsavailable. Figure out what is missing fromthe cp command below. You will need to display alist of all files copied, but only copy over the one new file(hint: you're actually copying files that have be updated.)$ touch a/b/c/d/e.txt$ cp [?????] a/* b