Files and Paths
Single-page view · as markdown
Files, Names, and Security
Peios / Using Peios / Files and Paths
File commands change names, contents, or metadata. Knowing which of those a command changes tells you whether data can be lost, and which security the result has. Every file on Peios has a security descriptor that decides who can use it, and copying, moving, and creating files treat that descriptor differently.
A name and an object are different #
A file is an object with contents, metadata, and a security descriptor. A name is an entry in a directory that refers to the object. One object can have several names, called hard links, and removing one name leaves the others. A symbolic link is a separate kind of file that stores a path, and that path can later lead to a different object, or to nothing.
Which security a file gets #
The command you use decides which security descriptor the result has:
| Operation | Result |
|---|---|
| Rename a file, including moving it within one filesystem | The same object, with its own security descriptor |
| Copy a file, including moving it to another filesystem | A new object, which inherits security from its destination directory |
| Create a file or directory | A new object, which inherits security from its parent directory |
| Add a hard link | Another name for the same object and descriptor |
Moving a file into another directory doesn't apply that directory's inherited rules to it. Inheritance happens once, when an object is created, and doesn't keep existing objects in step with their parent.
cp, mv, mkdir, and the other creation commands can request specific
security instead. Managing Files and
Directories shows how. To see or
change a file's security descriptor, use sd.
Results that need checking #
A recursive operation can fail after it has already changed part of a tree, and a command can report success while skipping files under an update policy. Check the result when every file matters.
Removing a name doesn't erase its data. Other hard links, snapshots, backups, and the storage device itself can keep copies.
What a listing shows #
A listing is for people to read. File names can contain spaces, tabs, and
newlines, so don't parse ls output to find file names in a script.
The mode, user, and group fields that ls and stat print are kept for
compatibility. They summarize some metadata, but they don't describe who can
use a file: the security descriptor does.
Every listing, metadata query, or path check describes one moment. The file can change, be replaced, or disappear before the next command runs, so a program that must act on a particular file checks the file it opens.
Managing Files and Directories
Peios / Using Peios / Files and Paths
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:
-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=timestampskeeps the access and modification times.--sd-explicitcarries the source's explicit access rules, and lets the destination directory add its inherited rules.--sdcopies 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:
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:
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.
Link files #
Create a symbolic link, a file that stores a path to another file:
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:
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:
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:
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:
| Command | Use it to | Manual |
|---|---|---|
cp | Copy files and directory trees | cp(1) |
mv | Move or rename files | mv(1) |
mkdir | Create directories | mkdir(1) |
touch | Create a file or update its timestamps | touch(1) |
mkfifo | Create a named pipe | mkfifo(1) |
mknod | Create a device node | mknod(1) |
mktemp | Create a uniquely named file or directory | mktemp(1) |
ln | Create symbolic or hard links | ln(1) |
link, unlink | Add or remove one hard link | link(1), unlink(1) |
rm | Remove files and directory trees | rm(1) |
rmdir | Remove empty directories | rmdir(1) |
shred | Overwrite a file's data | shred(1) |
truncate | Change a file's length | truncate(1) |
Read-Only Files and Creation Dates
Peios / Using Peios / Files and Paths
Every file on Peios can carry two special attributes besides its security descriptor: a read-only flag, and a creation date that copies can carry over. Windows programs set and check both, and Peios keeps them the same way.
The examples use a sample file named report.txt. Create it first, or
substitute your own.
Make a file read-only #
Mark a file read-only:
No program can then open the file for writing, truncate it, replace it, or delete it, whoever runs the program, the file's owner included. Clear the flag to change the file again:
Run attrib with only file names to see which are read-only. The first
column shows R for a read-only file and - for a writable one; the second
shows the indexing preference described below:
Read-only guards against accidental change, not against other people. It
grants and denies no one access, and anyone allowed to write the file's
attributes can clear it. Use sd to decide who can
use a file. A program that already had the file open for writing keeps that
access, and you can still rename the file. A directory keeps the flag but
isn't protected by it.
Delete a read-only file #
rm asks before deleting a read-only file:
Answer y to delete it. rm -f deletes it without asking. In a script,
where rm can't ask, it refuses and leaves the file unless you give -f, so
a read-only file survives a cleanup that didn't mean to remove it. Deleting a
read-only file needs the right to change its attributes as well as the right
to delete it.
Keep files out of search #
A file or folder can say how far a search index may record it. Keep a file's contents out of search, while its name can still be found:
attrib then shows I in its second column. Keep a folder and everything
in it out of search entirely, names included:
A folder's preference covers everything beneath it, so files created there
later are covered too. To index one subfolder normally again, give it its
own preference with attrib --indexing=all Private/Shared. attrib -i
removes a file's own preference, so it follows its folder's again.
The preference narrows what a search indexer records. It doesn't hide the
file from listings or change who can open it. Windows sees it as the file's
"not content indexed" attribute through a share. A filesystem that keeps no
extended attributes, such as a USB drive formatted for another system,
can't keep it, and attrib says so.
See and set a creation date #
A file has two creation dates. Its birth time is when the filesystem created it, and nobody can change it. Its Created At is the date people and copy tools mean: a copy can carry its original's date, and you can set one. Until a file has a Created At, its creation date is its birth time.
stat shows both:
Birth is the birth time. Created is the creation date, marked (birth)
when it is only the birth time. Attributes shows read-only for a read-only
file. For one value, use stat -c %k report.txt, or %K for seconds since
the Epoch.
Set a file's Created At with touch, writing the date in any form touch -d
accepts:
This changes only the creation date. Add -d, -t, -r, -a, or -m to
change the access and modification times as well. Remove the date again, so
that the creation date is the birth time:
Copy and move files #
A plain cp makes a new file, with today's creation date and no read-only
flag. cp -p and cp -a keep the original's creation date and, if it is
read-only, make the copy read-only too:
To keep the creation date without the flag, use --preserve without a value,
or remove the flag from the selection with --no-preserve=readonly. Moving a
file keeps both.
cp won't copy over a read-only file. cp -f clears the destination's
read-only flag and copies over it, as xcopy /R does on Windows:
The copy is read-only afterwards only if you also preserve the flag and the
original is read-only, as with cp -pf. A filesystem that has no special attributes, such as a USB
drive formatted for another system, can't keep a creation date or the flag.
Commands on this page #
| Command | Use it to | Manual |
|---|---|---|
attrib | Show, set, or clear the read-only flag and the indexing preference | attrib(1) |
touch | Set or unset a file's Created At | touch(1) |
stat | Show a file's creation date and attributes | stat(1) |
cp, mv | Copy or move a file, with its creation date and flag | cp(1), mv(1) |
rm | Delete a read-only file | rm(1) |
Inspecting Files and Paths
Peios / Using Peios / Files and Paths
Use these commands to see what is in a directory, read a file's metadata, and work out what a path refers to. A path can be text you are building, a link's stored target, or a route to an existing file, and each needs a different command. The man page for each command lists every option.
The examples use sample file names such as report.txt. Create those files
first, or substitute your own.
List a directory #
Show a directory's entries, with their metadata:
Names that begin with a dot are hidden. -a includes them, and -A includes
them without the . and .. entries. dir and vdir use the same listing
engine with fixed layouts: columns for dir, one detailed row per entry for
vdir.
The ownership and permission fields in a long listing are kept for
compatibility, and don't describe who can use the file. Use sd show for
that. Don't parse ls output in a script: names can contain spaces and
newlines, and the listing is meant for people.
To color listings, dircolors -b prints a shell setting that maps kinds of
file to colors. Review the settings before adding them to your shell
configuration, and keep listings readable without color.
Read a file's metadata #
Print selected properties of a file:
Like ls, stat prints mode, user, and group fields for compatibility, and
they don't describe access. A symbolic link has metadata of its own, so decide
whether you want the link or the file it points to. A size can change before
you read the file, so a program that limits how much it reads checks the file
it has opened.
Without -c, stat also prints a file's creation date and its attributes.
Birth is when the filesystem created the file. Created is the date a copy
can carry over from its original, and is the birth time, marked (birth), if
none has been set. Attributes shows read-only for a file that no one can
change or delete until the flag is cleared. See Read-Only Files and Creation
Dates.
Resolve a path #
Choose the command by the question you are asking:
- What does this link store?
readlink latestprints the path stored in a symbolic link. The path can be relative, and its target might not exist. - Where is this existing file?
realpath -e reports/draft.txtfollows every link and prints the absolute path. With-e, every part of the path must exist. - Where would this new file go?
realpath reports/planned.txtallows the last part to be missing, andrealpath -mallows missing parent directories too. The result is a planned path, not proof that you can create the file there. - Where am I?
pwd -Pprints the working directory with every symbolic link resolved. Your shell also has its ownpwd.
A resolved path doesn't reserve a name or check access, and another program
can change the file before you open it. The quiet mode of realpath hides
error messages but not a failing exit status, so check the status before using
the output.
Split and check path text #
basename and dirname work on the text of a path without reading the
filesystem:
The first prints draft, the second reports. Neither checks that the file
exists. pathchk reports/draft.txt checks that a proposed path meets naming
limits, but success doesn't promise that creating the file will work.
Commands on this page #
This page covers these commands:
| Command | Use it to | Manual |
|---|---|---|
ls | List directory entries | ls(1) |
dir, vdir | List entries in a fixed layout | dir(1), vdir(1) |
dircolors | Configure listing colors | dircolors(1) |
stat | Read file or filesystem metadata | stat(1) |
readlink | Read a symbolic link's stored path | readlink(1) |
realpath | Resolve a path to an absolute path | realpath(1) |
pwd | Print the working directory | pwd(1) |
basename, dirname | Split a path's text | basename(1), dirname(1) |
pathchk | Check a proposed path name | pathchk(1) |
Measuring Space and Copying Raw Data
Peios / Using Peios / Files and Paths
Use df and du to find out how much space is available and what is using
it, and dd to copy an exact number of bytes. These commands measure and copy
storage rather than files, so read their results with the storage model in
mind.
The examples use sample file names such as source.bin. Create those files
first, or substitute your own.
Check available space #
Show the space on the filesystem that holds the current directory:
df reports space for the whole filesystem. Reserved space, metadata,
snapshots, and files that are still open after removal all affect its totals,
so free space isn't the size minus the files you can see.
Measure a directory tree #
Total the space used by a directory and everything in it:
du adds up the files it can reach. It can report allocated size, the storage
blocks a file occupies, or apparent size, the file's logical length; the two
differ for sparse and compressed files. A directory that du can't read makes
its total incomplete, so check for errors.
df and du answer different questions, and a difference between them is a
reason to look at what each one counts, not proof that either is wrong. For a
full procedure, see Diagnosing Storage
Pressure.
Copy an exact range of bytes #
Copy the first 4 bytes of a file into another file:
if= names the input and of= names the output. dd overwrites the output
without asking, so check of= before you run it, especially if it names a
device: a mistake there affects a whole disk, not one file. Counts are in
units of the block size bs= unless you choose byte units. dd finishing
doesn't mean the data has reached the storage device; use its synchronization
options or sync when that matters.
Commands on this page #
This page covers these commands:
| Command | Use it to | Manual |
|---|---|---|
df | Show filesystem capacity | df(1) |
du | Measure a directory tree | du(1) |
dd | Copy a byte stream in blocks | dd(1) |