Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

class method

File.symlink

(::path old_name, ::path new_name) -> 0
File.symlink(target_path, link_path) → 0

Not supported on some platforms.

Creates a symbolic link at link_path to the entry at target_path:

Example
filepath = '/etc/passwd'                   # Regular file.
linkpath = '/tmp/foo'
File.symlink(filepath, linkpath)
File.readlink(linkpath)                    # => "/etc/passwd"
File.read(filepath) == File.read(linkpath) # => true

If the entry at target_path is itself a symbolic link, that link is not followed:

Example
link2path = '/tmp/bar'
File.symlink(linkpath, link2path)
File.readlink(link2path)                    # => "/tmp/foo"
File.read(filepath) == File.read(link2path) # => true
File.delete(linkpath, link2path)            # Clean up.
See also .symlink? .sticky? .stat .truncate also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0