Linux on call · Chapter 3 of 30

Files and folders

By the end of this chapter you can copy, move, rename and remove files and folders, and know which of those you cannot undo. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Finding your way
  • 5 tasks
  • Linux
  • In the all labs plan

The first 2 chapters of Linux on call are free. This one comes with the all labs plan.

Why you need this

Bunty has been downloading everything onto the server, all of it into one folder:

ls downloads
IMG_2041.jpg  gst-return.pdf	menu-final-FINAL-v2.txt
IMG_2042.jpg  invoice-sept.pdf	old-stuff
bunty-tmp     logo.png		prices.csv

Kabir wants it sorted: pictures in one folder, PDFs in another, the menu under a sane name, and Bunty's junk gone. But a full copy first, in case something is needed back. Bunty offered to rm -rf the lot.

So far you can make folders and write a line into a file. You could rebuild each file somewhere else with echo, but a photo is not a line of text, and nothing you know removes anything. And Linux has no recycle bin: what you remove here is gone.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Make a full backup of downloads first

    Copy the whole downloads folder before you change anything in it.

    1. Go to the chapter folder.

    2. Copy the folder downloads to a new folder downloads-backup. Use cp with the option that copies a folder and everything inside it.

    3. List downloads-backup, and look inside its bunty-tmp folder too.

    To pass: downloads-backup is a folder in the chapter folder. It has every file that downloads had at the start: the files in bunty-tmp too, and the menu with its old long name. So do this task before the other tasks.

  2. Task 2

    Put pictures in images and PDFs in documents

    Sort the pictures and the PDFs in downloads into two folders.

    1. Make two folders inside downloads: images and documents.

    2. Move the two .jpg files and the .png file into downloads/images. Use mv.

    3. Move the two .pdf files into downloads/documents.

    4. List downloads to check that no picture or PDF is left there.

    To pass: downloads/images has the three picture files and nothing else. downloads/documents has the two .pdf files and nothing else. No .jpg, .png or .pdf file is left in downloads itself.

  3. Task 3

    Rename the menu to menu.txt

    Give the menu file in downloads a short name.

    1. Find the menu file in downloads. It has a long name.

    2. Rename it to menu.txt, in downloads. Use mv: the old name first, then the new name.

    3. List downloads to check.

    To pass: downloads/menu.txt has the menu, and the old long name is gone. Use mv, not cp: a copy keeps the old name.

  4. Task 4

    Delete the empty folder and Bunty's junk

    Delete two folders in downloads, and keep everything else.

    1. List downloads/old-stuff and downloads/bunty-tmp. One is empty, one is not.

    2. Delete the empty one with rmdir.

    3. Delete bunty-tmp and everything inside it with rm and the option for folders.

    To pass: downloads/old-stuff and downloads/bunty-tmp are gone. Everything else in downloads stays. downloads/prices.csv must still be there, as it was.

  5. Task 5 · interview question

    Interview: why rm -rf "$DIR/" is dangerous

    Interview question: what does rm -rf "$DIR/" do when DIR is empty?

    1. Think about what the command turns into when DIR is empty or not set.

    2. Write one or two sentences in your own words. Say what happens when DIR is empty, and what the command would then delete.

    3. Save them in answer.txt, in the chapter folder. Put the text inside single quotes, so the shell does not change $DIR in your sentence.

    To pass: answer.txt says that DIR is empty (or not set), and says what rm -rf would then delete.

What you will learn

  • cp and cp -r: copy a file, or a whole folder.
  • mv: move files into a folder, or rename one.
  • rm, rmdir and rm -r: remove a file, an empty folder, or a folder with everything in it.
  • Patterns and variables: *.txt for many files at once, and what the shell puts in place of "$DIR".

More in Finding your way

Build "Files and folders" yourself

Tapri’s ordering app moves onto a Linux server, and the server is now yours to run. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Linux on call are free. This one comes with the all labs plan.