Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

Chapter 3Name in the course: Files and folders

Read, write and organise files and folders

Scripts read logs and configs, write reports, and clean up folders. pathlib handles paths, open() reads and writes, shutil copies, moves and archives.

  • 5 Lessons
  • 0 Commands

$ run as a normal user# run as root (or with sudo)

3.1

Work with paths

Path objects join paths safely and have methods for everything.

paths.py
from pathlib import Path
 
home = Path.home()
logs = Path("/var/log")
conf = Path("/etc") / "ssh" / "sshd_config"
 
print(conf.name, conf.suffix, conf.parent, conf.exists(), conf.is_file())
for p in logs.glob("*.log"):
    print(p, p.stat().st_size)
for p in Path("/etc").rglob("*.repo"):
    print(p)

3.2

Read and write text files

with open(...) closes the file by itself, also after an error.

read_write.py
from pathlib import Path
 
with open("/etc/hosts", encoding="utf-8") as f:
    for line in f:
        if line.strip() and not line.startswith("#"):
            print(line.rstrip())
 
report = Path("/tmp/report.txt")
report.write_text("disk OK\n", encoding="utf-8")
with report.open("a", encoding="utf-8") as f:
    f.write("memory OK\n")
print(report.read_text(encoding="utf-8"))
ModeMeaning
"r"Read (default)
"w"Write — empties the file first
"a"Append to the end
"x"Create; fail if it exists
"rb", "wb"Binary

3.3

Copy, move and delete

shutil does the file work of cp, mv and rm -r.

organise.py
import shutil
from pathlib import Path
 
src = Path("/srv/www")
backup = Path("/backup/www-copy")
shutil.copytree(src, backup, dirs_exist_ok=True)
shutil.copy2("/etc/hosts", "/backup/hosts")
shutil.move("/tmp/report.txt", "/backup/report.txt")
shutil.rmtree("/tmp/old-build", ignore_errors=True)
Path("/backup/logs").mkdir(parents=True, exist_ok=True)

Watch out

shutil.rmtree deletes without asking. Print the path first, and never build it from user input without checks.

3.4

Clean up old files

Find files older than N days and delete (or archive) them.

cleanup.py
import time
from pathlib import Path
 
days = 14
cutoff = time.time() - days * 86400
for p in Path("/var/tmp/app").rglob("*"):
    if p.is_file() and p.stat().st_mtime < cutoff:
        print("deleting", p)
        p.unlink()

3.5

Make archives

shutil.make_archive and tarfile pack folders like tar.

archive.py
import shutil
import tarfile
from datetime import date
 
name = shutil.make_archive(f"/backup/etc-{date.today()}", "gztar", root_dir="/etc")
print(name)
 
with tarfile.open(name) as t:
    print(len(t.getnames()), "files in the archive")