On this page5 sections
Your practice web page will begin with one line: “Dispatch practice service.” You will add a second line, compare the two versions, and put the original back. The useful part is having both versions available when you want to know exactly what changed.
Open the disposable Ubuntu VM from the first lab and use your ordinary account inside it. Everything here stays in ~/linux-basics, away from a real service. Later, the practice server will send this same page to a browser. For now, the page is small enough that you can follow every change yourself.
Give the page a home and a spare copy
The first commands create a folder, write the page and make its backup. Notice the two names before you run them: index.html is the page you will edit, while index.html.backup keeps the starting contents.
mkdir -p ~/linux-basics/service
cd ~/linux-basics
pwd
printf 'Dispatch practice service
' > service/index.html
cp -p service/index.html service/index.html.backup
ls -l service
After cd ~/linux-basics, the command pwd prints where you are. The ~ in that path stands for your home directory. From this position, service/index.html tells Linux to look in the service folder for index.html. That is a relative path: its starting point is the current directory. An absolute path starts at the filesystem root, as a path beginning /home/ does.
The writing command uses a single >, which replaces the file’s contents. That suits a new page, but running it again later would replace your edits as well. The next command, cp -p, makes a separate recovery copy before anything changes.
The listing should now contain both files. Their contents match, but they are no longer tied together. Add a line to the page and the backup stays as it was. That independence is what will make it useful in a moment.
There is an easy way to lose your bearings even in this small folder. Step into service, then ask for service/index.html, and Linux looks for another service folder inside the one you are already in. A familiar filename can produce a missing-file message simply because the path starts from a different place. pwd lets you check that starting point.
One added line, then a return to the original
If nano is installed, open nano service/index.html and add “Exports are available here.” on a second line. Save with Ctrl+O, confirm the filename, then leave with Ctrl+X. If nano is unavailable, use the printf line below to append the text instead. Choose one way of adding it, so the line appears only once, then run the comparison.
printf 'Exports are available here.
' >> service/index.html
diff -u service/index.html.backup service/index.html
The two arrows, >>, keep the existing contents and add to the end. In the output from diff -u, the new line should have a + in front of it. The saved copy gives the comparison a before; the edited page gives it an after.
A nonzero exit status from diff can simply mean that the files differ. It need not mean the command failed. man diff explains the statuses for a difference and for an error, a useful distinction when another script will act on the result.
Now replace the page with its saved copy and compare again:
cp -p service/index.html.backup service/index.html
diff -u service/index.html.backup service/index.html
This time, no output with exit status zero means the two files have matching contents. The extra line has gone from the page on disk. If a server were already running, it might still need to reload the file; this comparison cannot tell you what that server has loaded.
Both versions looked like plausible pages. Opening either one alone would not have shown you the history. With a saved copy, the change is visible twice: the added line first appears in the comparison, then disappears after the restore.
Another name for the page is not another copy
The backup holds its own contents. A symbolic link does something different: it holds a route to a file. Make one called current-page, inspect the route it stores, and read through it:
ln -s service/index.html ~/linux-basics/current-page
readlink ~/linux-basics/current-page
cat ~/linux-basics/current-page
The link contains service/index.html. Because that route is relative, Linux follows it from the directory containing the link, rather than from wherever you happen to be when reading it. Opening or editing through current-page reaches the original page. Deleting the link would remove that route, leaving the page itself in place.
To see the difference between a path and the contents it reaches, move the page with mv service/index.html service/index.html.saved. Then read current-page again. The read fails: the page’s contents still exist, but under a name the link does not ask for.
Move it back with mv service/index.html.saved service/index.html, then repeat cat ~/linux-basics/current-page. The route works again. If a later attempt to run ln -s reports that the destination already exists, inspect it with readlink. There is already a link there to examine.
Leave the page ready for its next job
Keep the restored page, its backup and the working link in the lab folder. Note the page’s absolute path and the copy command that restored it. Another command will eventually need to find the page, and these details tell you both where it is and how to return to the saved version.
You changed the contents without changing the name, then changed the name without losing the contents. The backup and the link responded differently because they held different things. When the practice server starts using this page in a later lesson, an empty response and an outdated one will already give you different places to begin looking.
File operations and comparisons
The GNU Coreutils manual describes the file operations and symbolic links used here. The GNU Diffutils manual explains comparisons and their exit statuses. These files are synthetic lab material.
Sources & context
Sources linked in this article. Read alongside the author’s analysis; a citation does not independently verify a publisher’s claims.
- GNU Coreutilswww.gnu.org
- GNU Diffutilswww.gnu.org
