Unix File Management: Paths, Commands, and Permissions

Unix File Management lets you organize project files, locate data, and control access to shared work. Unix and Linux systems use closely related commands for these tasks. To predict what a command will do, identify its starting directory, its source and destination, and the permissions that apply. These habits matter when a short command can change or remove important files.

Finding files through paths

A pathname identifies a file or directory by giving directory names separated by /. An absolute path begins with /, the root directory. A relative path begins from your current working directory, the directory used as the starting point for relative names.


A directory tree connects the root to home, sam, and the entries lab and notes.txt.


In the diagram, /home/sam/notes.txt is an absolute path. If your current directory is /home/sam, the relative path notes.txt identifies the same file. Changing your current directory changes what a relative path identifies; it does not change what that absolute path identifies.


The component . means the current directory, and .. means its parent. For the examples here, paths do not pass through symbolic links.


Example: From /home/sam/lab, ../notes.txt first goes to /home/sam and then selects notes.txt.



Inspecting and changing entries

Use these core commands with explicit names. In the copy and move examples below, the destination file does not already exist.


Command

Effect

pwd

Displays the current directory's absolute path.

ls -l

Lists non-hidden entries with permissions and other details.

ls -a

Includes names beginning with a dot.

mkdir results

Creates a directory named results.

cp draft.txt copy.txt

Copies a file while retaining the source.

mv draft.txt final.txt

Renames the entry, removing the old name.

rm draft.txt

Removes the named file entry.

rmdir results

Removes the named directory if it is empty.


Use cd results to make results your current directory. Copying a directory requires an appropriate recursive option, such as cp -R; the simple file-copy form above does not copy a directory tree. Copy and move commands can replace existing destination files, so check the destination before running them.

Reading and setting permissions

A permission mode records access permissions for the owner, group, and others. In an ordinary ls -l listing, the first character identifies the entry type: - for a regular file or d for a directory. The next nine characters form three groups of r, w, and x, with - showing an absent permission.


Permission

Regular file

Directory

Read, r

Read its contents.

List its entry names.

Write, w

Modify its contents.

Add, remove, or rename entries, normally together with search permission.

Execute, x

Permit execution as a program.

Search for named entries and traverse the directory.


For ordinary permission checks, use the owner group of bits if you own the entry; otherwise use the group bits if you belong to its group; otherwise use the others bits. These groups are selected, not added together. Access through a path also requires search permission on its directories. Additional access rules and privileged overrides are outside these examples.


Numeric chmod modes use three octal digits. For each permission group:


d=4r+2w+x,r,w,x∈{0,1}d = 4r + 2w + x, \qquad r,w,x \in \{0,1\}


Each indicator is 1 when that permission is granted and 0 otherwise. Write the resulting digits in owner–group–others order. Thus, chmod 640 report.txt gives the owner read and write, the group read only, and others no access. These digits encode permissions; they are not physical quantities with units.



What to remember

  • Resolve relative paths from the current directory.
  • Distinguish copying contents from moving or removing names.
  • Interpret directory permissions separately from file permissions.
  • Compute each permission digit with weights 4, 2, and 1.

Example: Preserve a survey data file

Nia is preparing a reference copy of survey data. Her current directory is /home/nia/survey/raw, which contains the ordinary file readings.csv. The sibling directory /home/nia/survey/archive exists and is empty. Paths contain no symbolic links, and all commands below have the permissions needed to succeed.


The survey directory contains raw and archive directories, with readings.csv initially inside raw.


a) Give a relative path and an absolute path to the archive directory without changing the current directory.


A relative path identifies an entry starting from the current directory. Begin at /home/nia/survey/raw. Moving up one directory reaches ________. From that parent, select the child named archive.


The relative path is ../archive, and the absolute path is /home/nia/survey/archive. Both identify the same directory here. The relative version depends on the stated starting directory, while the absolute version begins at the root.


b) Copy readings.csv into the archive, then rename only the archived copy to baseline.csv. Keep the original file in raw, and do not change the current directory.


First copy the source file to the existing archive directory, specifying the new file's full relative name:


cp readings.csv ../archive/readings.csv


The source name still exists in raw, and a separate copy now exists in archive. Next rename that archived entry:


mv ../archive/readings.csv ../archive/baseline.csv


Both arguments of mv refer to the archive directory. Moving the original source at this stage would fail the requirement to preserve it.


The final files are /home/nia/survey/raw/readings.csv and /home/nia/survey/archive/baseline.csv. These commands leave the current directory unchanged.


c) Nia owns the archive directory. Set its ordinary permissions so she can list, traverse, and change its entries; its group can list and traverse it; and others have no access. Give the numeric mode and command. Ignore additional access rules and special mode bits.


The permission mode records access for owner, group, and others. For a directory, listing requires read permission, traversal requires execute permission, and changing entries normally requires write together with execute permission.


The owner therefore needs rwx, giving the digit ________ from 4+2+14+2+1. The group needs r-x, giving the digit ________ from 4+0+14+0+1. Others need ---, giving 0+0+0=00+0+0=0.


Arrange these digits in owner–group–others order. The numeric mode is 750, with no physical unit. From the unchanged current directory, the command is chmod 750 ../archive.


This changes the directory's permissions, not the copied file's permissions. Directory search permission lets a group member look up a known file, but reading that file's contents also depends on the file's own permissions.

Your current directory contains an empty directory named old-results. You have the required permissions. Which command removes that directory rather than creating or renaming it?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A Unix project contains two regular files, settings and .settings, with identical ownership and permissions. Complete the conventional classification: because its name begins with a dot, .settings is described as a file. This classification concerns directory listings rather than additional protection against opening the file.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Your current directory is /home/leo/project/src/tools. All named directories exist, paths contain no symbolic links, and names are case-sensitive. What absolute path does ../../output/log.txt identify? Give the normalized path with no . or .. components and no trailing slash.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice