Files, paths, and directories

Synopsis

Keep the course files in a directory named unix-practice under the account’s home directory. Use an ordinary account and the /bin/sh session from A first shell session. The following exercises use the names notes and copies inside the practice directory.

Make a place to work

Create the practice directory.

Shell input
mkdir "$HOME/unix-practice"

A successful mkdir normally prints nothing. If the directory already exists from an earlier lesson, keep it. If the name belongs to unrelated work, use a fresh practice account. Enter the directory and check the resulting path.

Shell input
cd "$HOME/unix-practice"
pwd

The path should end in /unix-practice. If cd reports an error, the shell stays in the previous directory. Correct the path before creating any files.

Create the two subdirectories and list them.

Shell input
mkdir -p notes copies
ls

Look for copies and notes. The -p option permits either directory to exist already.

Follow relative paths

Change into notes, then inspect its location.

Shell input
cd notes
pwd

The path now ends in /unix-practice/notes. A name beginning with / is an absolute path. A relative path, such as notes, starts at the current directory. A single dot names the current directory. Two dots name its parent.

Return to the practice directory.

Shell input
cd ..
pwd

The final component should again be unix-practice. From any other directory, cd "$HOME/unix-practice" returns directly to the same place.

Copy a name containing a space

Use a disposable field note for this exercise. List the two directories before creating the file.

Shell input
ls notes copies

The next command replaces any previous practice copy of notes/field notes.txt. The shell opens that destination for output when it reads >. Single quotes preserve the space in the filename.

Shell input
printf '%s\n' 'north meadow' > 'notes/field notes.txt'

Copy the note into copies. The trailing slash names the destination directory. Start with an empty copies directory, or confirm that its existing practice copies may be replaced.

Shell input
cp 'notes/field notes.txt' copies/
ls copies

Look for field notes.txt. Rename the copy and list both names with a wildcard.

Shell input
mv 'copies/field notes.txt' copies/meadow.txt
ls notes/*.txt copies/*.txt

The result includes notes/field notes.txt and copies/meadow.txt. The shell expands each unquoted * to matching names before it runs ls. A space inside a matching filename stays part of that filename. The quoted path in the cp command names one file explicitly.

Remove only the copy

Confirm the target before removing it.

Shell input
ls -l copies/meadow.txt

Check that the path names the copy. Remove that single file and list what remains in each directory.

Shell input
rm copies/meadow.txt
ls copies
ls notes

The first listing should be empty after a fresh run. The second should still contain field notes.txt. The rm command removes the named entry without moving it to a desktop trash folder. If the wrong practice copy disappears, recreate it from the surviving note.

Continue with Read, edit, and find text.

UNIX is a registered trademark of The Open Group.