Managing Files and Directories

Use these commands to copy, move, create, link, and remove files in a terminal. Each section shows the common case and the Peios behavior that affects it. The man page for each command lists every option.

Before you start #

Use a terminal on a machine with the command-line utilities installed. To try the examples, work in an empty directory so that nothing you need is overwritten. The examples name files such as report.txt; create them first, or substitute your own.

Copy files #

Use cp to make a new file or directory tree from existing data:

cp -i report.txt report-backup.txt

-i asks before overwriting a file that already exists. To copy a directory and everything in it, use cp -R notes notes-copy. If the destination already exists as a directory, cp copies the source into it.

A copy is a new object, so it inherits security from its destination directory, and copying to another location can change who can read the data. To keep particular attributes, ask for them:

  • --preserve=timestamps keeps the access and modification times.
  • --sd-explicit carries the source's explicit access rules, and lets the destination directory add its inherited rules.
  • --sd copies the source's owner and its full access and audit lists.

Neither security option copies the security descriptor's group, so a copy is not a complete security backup. cp(1) describes every preservation option and the access each one needs.

Move and rename files #

Use mv to change a file's name or location:

mv -i draft.txt report.txt

Within one filesystem, a move is a rename: the file keeps its security descriptor, even in a directory with different inherited rules. Across filesystems, mv copies the file and then removes the source, and the copy inherits security from its destination. Requesting particular attributes also makes mv copy, even within one filesystem. See mv(1).

Create files and directories #

Create a directory, with any missing parents:

mkdir -p reports/2026/drafts

touch build.marker creates an empty file, or updates the timestamps of a file that exists without changing its contents. mkfifo creates a named pipe that passes a stream from one program to another, and mknod creates device nodes, which needs the relevant authority. mktemp -d creates a directory with a unique name and prints it, so another program can't claim the name first.

A new object inherits security from its parent directory. mkdir, touch, mkfifo, and mknod can request an owner, group, integrity label, or complete security descriptor for the object they create. mkfifo and mknod also accept a -m mode option, but it isn't how security is set on Peios. Check the result with sd show before you share the location, because choosing an owner doesn't on its own give that owner the access you meant. See mkdir(1) for the security options.

Create a symbolic link, a file that stores a path to another file:

ln -s report.txt latest

A relative path in a symbolic link is resolved from the directory that contains the link. Without -s, ln creates a hard link: another name for the same object, sharing its contents and security. Hard links can't usually cross filesystems. link and unlink add or remove exactly one hard link, without the other choices ln and rm offer.

Remove files and directories #

Remove a file, with a confirmation prompt:

rm -i disposable.txt

Removal has no undo. rm removes a directory and its contents only when you ask for recursion. A prompt doesn't decide whether you can delete the file: the file's security descriptor does. rmdir removes only empty directories, which protects you from removing contents by mistake.

rm asks before deleting a read-only file, and needs -f to delete one in a script. See Read-Only Files and Creation Dates.

rm normally removes a file's name at once, even if a program still has the file open; that program carries on with a file that no longer has a name. To leave the name in place until the last program using the file closes it, as Windows does, use --when-closed:

rm --when-closed build.log

If a program has the file open, rm says the file will be removed when it is closed. Until then the name stays, and opening the file again fails. A directory goes when its last user closes it, if it is empty then. If a program has opened the file without allowing it to be deleted, rm reports it as in use either way.

shred overwrites a file's data before removing it, but snapshots, backups, journals, and the storage device can keep copies that it doesn't reach. When data must become unrecoverable, use a method designed for the storage, such as encryption with a key you can destroy.

Change a file's length #

truncate -s 1K size-sample.bin sets a file's length. Shrinking a file discards its end, and growing it again doesn't bring that data back. Work on a copy when you're unsure.

Check the result #

List the result and its security:

ls -l
sd show ./report-backup.txt

Check the security descriptor of anything you copied or created somewhere other people can reach. A recursive command that failed part way through can leave some files changed.

Commands on this page #

This page covers these commands:

CommandUse it toManual
cpCopy files and directory treescp(1)
mvMove or rename filesmv(1)
mkdirCreate directoriesmkdir(1)
touchCreate a file or update its timestampstouch(1)
mkfifoCreate a named pipemkfifo(1)
mknodCreate a device nodemknod(1)
mktempCreate a uniquely named file or directorymktemp(1)
lnCreate symbolic or hard linksln(1)
link, unlinkAdd or remove one hard linklink(1), unlink(1)
rmRemove files and directory treesrm(1)
rmdirRemove empty directoriesrmdir(1)
shredOverwrite a file's datashred(1)
truncateChange a file's lengthtruncate(1)

Edit this page