Linux on call · Chapter 5 of 30

Finding things

By the end of this chapter you can search a whole folder tree by name, type, age and size, and act on what you find, deleting included, safely. 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
  • 4 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

Ravi has a list. The logs folder keeps growing, and Kabir says the logs older than a week can go, but nothing that is not a log. Something in the chapter folder is huge and Ravi wants to know what. The team needs a list of every config file under srv. And something changed in the app after Arjun's last deploy: which files?

Bunty reached for ls. It shows one folder at a time, and there are folders inside folders inside folders. ls -l shows dates and sizes, but only for the folder you name, so Bunty would be opening every folder by hand and comparing dates in his head, and deleting the old logs one by one.

What you build: 4 tasks

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

  1. Task 1

    Delete the logs older than a week

    Delete the old .log files under logs, and keep everything else.

    1. Go to the chapter folder.

    2. Use find on logs to list every .log file changed more than 7 days ago. Use its -mtime test. Look in every folder under logs.

    3. Read the list. It should have only old .log files.

    4. Run the same find again, with -delete at the end.

    To pass: every .log file under logs older than 7 days is gone. Every newer .log file stays. Every file that is not a .log file stays, even an old one (like logs/rotate.conf and logs/archive/README.txt). You ran find with -mtime in the chapter folder, and it worked.

  2. Task 2

    Save the biggest file's path in biggest.txt

    Find the biggest file in the chapter folder.

    1. Use find on the chapter folder (.) to list only files bigger than 1 megabyte. Look in every folder.

    2. More than one file is that big. List them with their sizes, the biggest first.

    3. Save only the path of the biggest file in biggest.txt, in the chapter folder.

    To pass: biggest.txt has the path of the one biggest file under the chapter folder, and no other file's path.

  3. Task 3

    List every config file in confs.txt

    Save a list of all the .conf files under srv.

    1. Go to the chapter folder.

    2. Use find on srv to list every name that ends in .conf.

    3. One result is a folder, not a file. Add the find test that keeps only files.

    4. Save the output of that find in confs.txt, in the chapter folder.

    To pass: confs.txt has the path of every file under srv whose name ends in .conf, one on each line, and nothing else. No folders. Each path starts with srv/, as find prints it from the chapter folder.

  4. Task 4

    List the files changed since the deploy in changed.txt

    Save a list of the app files changed after the last deploy.

    1. Go to the chapter folder.

    2. Use find on app with its -newer test. Compare with the file app/DEPLOYED.

    3. Keep only files, not folders.

    4. Save the output in changed.txt, in the chapter folder.

    To pass: changed.txt has the path of every file under app changed after app/DEPLOYED, one on each line, and nothing else. No folders. Each path starts with app/, as find prints it from the chapter folder.

What you will learn

  • find with -name and -type: walk a whole tree, keep what matches a name, files only or folders only.
  • -mtime, -size and -newer: keep what is old, big, or changed after another file.
  • -exec and -delete: run a command on what you found, or remove it.

More in Finding your way

Build "Finding things" 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.