Skip to content
Tutorials Center
My learning

Topics

Lesson 76 of 134 ยท Standard Library

Tempfile, Pathname and FileUtils

Work safely with temporary files and directories using Tempfile and Dir.mktmpdir, handle paths as objects with Pathname, and copy, move and delete with FileUtils.

Files and IO gave you File and Dir. Three more standard-library pieces make real file work pleasant and safe. Tempfile and Dir.mktmpdir give you scratch space that cleans up after itself. Pathname turns a path from a plain string into an object with methods. FileUtils adds the Unix-style commands you know from a shell: mkdir -p, cp -r, mv, rm -rf, touch.

All three ship with Ruby and load with a require. They are the tools behind installers, build scripts, test suites that need fixtures, and any program that has to work with files without leaving a mess.

Tempfile: scratch files that clean up

Hard-coded names like "/tmp/work.txt" are a classic bug: two copies of your program overwrite each other's file, and an attacker may be able to guess the name and plant something there. Tempfile creates a file with a unique, unpredictable name in the system's temporary directory.

Ruby Tempfile.create and Tempfile.new

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

Prefer Tempfile.create with a block: Ruby deletes the file the moment the block finishes. The name argument is a prefix ("report"), or a two-element array of prefix and extension (["data", ".csv"]), because some tools care about the extension.

Tempfile.new is the older, object-style API. It returns a Tempfile object, and it also deletes the file when the object is garbage collected, but you should not wait for that: call close and then unlink yourself, usually in an ensure. After unlink, path returns nil. Note that you write to a temp file like to any other File, and remember to flush or rewind before reading back what you wrote.

Writes are buffered. If another piece of code (or another process) will open the file by path while your handle is still open, call flush or close first, or it may see an empty file.

Temporary directories

When you need several files, create a temporary directory. Dir.mktmpdir with a block creates it, yields its path and deletes the directory and everything inside when the block ends.

Ruby Dir.mktmpdir for a throwaway workspace

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

The final File.exist?(saved) prints false, because the whole directory tree is removed. This is exactly what you want in tests: every test gets a clean workspace, and a failure cannot leave junk behind. If an exception happens inside the block, cleanup still runs.

Pathname: paths as objects

A Pathname wraps a path string and adds methods from File, Dir and FileTest, plus path arithmetic. The / operator (or + and join) builds paths, so you stop writing File.join everywhere.

Ruby Building and taking apart paths

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

Look at the last two path joins. base + "../shared" resolves the .. and gives /srv/shared, while base.join("/etc/passwd") returns /etc/passwd: an absolute right-hand side replaces the left side. Keep that in mind when you join a path with something a user supplied. Pathname("...") with parentheses is a shortcut for Pathname.new. Convert back with to_s when an API wants a string.

Pathname does the file work too

The same object can read, write, list and test the file system, so one variable carries both the "where" and the "do".

Ruby Reading, writing and listing with Pathname

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

mkpath creates a directory and any missing parents (like mkdir -p), rmtree removes a directory and everything inside, children returns Pathname objects (not strings), and glob takes a pattern relative to the path. relative_path_from is the standard way to print short, readable paths in reports.

Use Pathname for code that manipulates many paths, such as build tools or file organizers. For a single File.read, plain strings are fine. In your own methods, accept anything that responds to to_s or to_path: File.read, File.open and friends already accept Pathname objects directly.

FileUtils: shell commands in Ruby

FileUtils mirrors the everyday Unix commands as module methods, and works the same on Windows. Every method also accepts noop: true (do nothing, useful for a dry run) and verbose: true (print the equivalent shell command).

Ruby mkdir_p, cp, mv, rm and friends

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

The verbose output (mkdir -p dry/run) is the shell command the call stands for, and since noop: true is on, no directory is created, which is why Dir.exist?("dry") is false. Learn the pairs:

FileUtils Shell Notes
mkdir_p mkdir -p creates parents, no error if it exists
cp, cp_r cp, cp -r file or whole tree
mv mv move or rename
rm, rm_f rm, rm -f rm raises if the file is missing
rm_r, rm_rf rm -r, rm -rf directories; rm_rf ignores errors
touch touch create empty or update the time
ln_s ln -s symbolic link

Dangerous calls need care

rm_rf does what it says, with no confirmation and no trash. A path built from an empty variable can delete far more than you intended. Defend yourself: check that a path is inside the directory you expect before deleting, prefer a temp directory for scratch work, and run with noop: true first when a script is new.

Ruby A guard before deleting

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

expand_path resolves .. before the comparison, so work/../other is correctly seen as outside work. This start_with? check on a path that ends with / is a standard guard. It does not follow symbolic links, so for hostile input you would also resolve them with realpath.

Putting it together: a tiny file organizer

A realistic task: sort the files of a messy folder into sub-folders by extension, entirely inside a temporary directory so nothing is left behind.

Ruby Organizing files by extension

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

Notice how little glue code there is. children, select(&:file?), extname, mkpath and / read almost like a description of the task. Because it runs in Dir.mktmpdir, you can run it as often as you like.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the main advantage of Tempfile.create("x") { |f| ... } over writing to a fixed path such as /tmp/x.txt?

    A unique name avoids collisions and guessing attacks, and the block form removes the file automatically.
  2. 2. What does Pathname.new("/srv/app").join("/etc/passwd") return?

    When the joined part is an absolute path it replaces the left side, so the result is /etc/passwd. Be careful with user-supplied parts.
  3. 3. Which FileUtils call is the equivalent of mkdir -p a/b/c?

    mkdir_p creates all missing parent directories and does not fail if the directory already exists.
  4. 4. What does FileUtils.mkdir_p("x", noop: true, verbose: true) do?

    noop: true turns the call into a dry run, and verbose: true prints the equivalent shell command.
  5. 5. Why call t.unlink for a Tempfile created with Tempfile.new?

    Without the block form you are responsible for cleanup. close and unlink (in an ensure) delete the file deterministically.

Summary

  • Use Tempfile.create (block form) for scratch files and Dir.mktmpdir for scratch directories; both clean up automatically.
  • Tempfile.new needs close and unlink in an ensure; flush or close before another reader opens the path.
  • Pathname represents paths as objects: /, join, basename, extname, parent, relative_path_from, plus read, write, children, glob, mkpath, rmtree.
  • Joining an absolute path onto a Pathname replaces the left side; validate user-supplied paths.
  • FileUtils provides mkdir_p, cp, cp_r, mv, rm, rm_rf, touch and ln_s; use noop: and verbose: to preview.
  • Guard destructive calls like rm_rf and do experiments inside a temporary directory.

Next: Time and Date