Chapter 3: Files and directories

Exercise: Organise a website's files

A colleague left a website's files in one heap. On servera, sort them into directories, make a backup and an archive copy, rename a page, and clean up after yourself.

Organise a website's files

  • Create directories and empty files.
  • Move, rename and copy files and whole directories.
  • Remove files and directories, and see why rmdir is the safe choice.

Start the exercise

On workstation, start the exercise. It removes any site and archive directories from an earlier run from student's home on servera.

[student@workstation ~]$ lab start sa-manage-files

Later tasks assume that you are logged in to the server they name: ssh student@servera from workstation, then sudo -i for a root shell.

Create the site directory and its three subdirectories

[student@workstation ~]$ ssh student@servera
[student@servera ~]$ mkdir site
[student@servera ~]$ cd site
[student@servera site]$ mkdir images css pages

Create the heap of files

touch creates all six empty files at once:

[student@servera site]$ touch logo.png banner.png style.css home.html about.html contact.html
[student@servera site]$ ls
about.html  contact.html  home.html  logo.png  style.css
banner.png  css           images     pages

Move each kind of file into its directory

With several files to move, the last argument is the directory they go into:

[student@servera site]$ mv logo.png banner.png images
[student@servera site]$ mv style.css css
[student@servera site]$ mv home.html about.html contact.html pages
[student@servera site]$ ls -R
.:
css  images  pages

./css:
style.css

./images:
banner.png  logo.png

./pages:
about.html  contact.html  home.html

Rename the contact page

Rename pages/contact.html to pages/contact-us.html.

[student@servera site]$ mv pages/contact.html pages/contact-us.html

The destination isn't an existing directory, so mv treats it as the new name.

Back up the pages directory

Copy the whole pages directory to pages-backup, next to it.

[student@servera site]$ cp -r pages pages-backup
[student@servera site]$ ls pages pages-backup
pages:
about.html  contact-us.html  home.html

pages-backup:
about.html  contact-us.html  home.html

Without -r, cp would skip the directory.

Archive a copy of the whole site

Create ~/archive/2026 (both levels, with one command), then copy the whole site into it as site-v1.

[student@servera site]$ mkdir -p ~/archive/2026
[student@servera site]$ cp -r ~/site ~/archive/2026/site-v1
[student@servera site]$ ls ~/archive/2026/site-v1
css  images  pages  pages-backup

~/archive/2026/site-v1 didn't exist, so it became the name of the copy. Had it existed, you would have got site-v1/site instead. Run the same cp -r again now and check with ls ~/archive/2026/site-v1 to see it happen.

Remove the backup directory

Try rmdir first, then remove it properly.

[student@servera site]$ rmdir pages-backup
rmdir: failed to remove 'pages-backup': Directory not empty
[student@servera site]$ rm -r pages-backup
[student@servera site]$ ls
css  images  pages

rmdir's refusal is a safety check: it only removes directories that are already empty.

Remove the archive interactively, and log out

Remove ~/archive with rm -ri, answering y at each question, and watch the order: rm goes into each directory, removes its contents, then the directory itself.

[student@servera site]$ cd
[student@servera ~]$ rm -ri archive
rm: descend into directory 'archive'? y
rm: descend into directory 'archive/2026'? y
rm: descend into directory 'archive/2026/site-v1'? y
...output omitted...
rm: remove directory 'archive'? y
[student@servera ~]$ exit

Leave ~/site for now, or run rht-vmctl reset servera on the host to put servera back.

Grade and finish

Check your work with lab grade sa-manage-files, then put the exercise away with lab finish sa-manage-files.