Skip to content

Creating and organizing files

Module
Working with Files and Directories
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab container), GNU Bash 5.2.21, GNU Coreutils 9.4
On this page

Your notes are in ~/lab/Documents, but you need a separate place for a project: a working note, an empty checklist, and a directory for drafts. You want to keep the original notes intact.

By the end, you can build that workspace, copy a small directory, and move or rename files. You can also stop before a command replaces a file you meant to keep.

Finish Practice: Navigating the Filesystem (B05). Keep the existing ~/lab from B04.1’s setup; do not recreate its files.

Before starting, take a VM snapshot named Before B06.1, following B02.4. Restoring that snapshot also discards other VM changes made since it. The scoped Reset this lesson is the lighter option for repeating this lesson. Keep all work here disposable.

Add only the B06 working directory:

Terminal window
cd ~/lab
mkdir -p b06
cd b06
pwd
Captured output
/home/learner/lab/b06

You met mkdir -p in B04.1: it creates directories, including missing parents, and accepts directories that already exist. Here it adds only b06. cd b06 makes that your current directory. Every remaining command runs in ~/lab/b06; ../Documents/notes.txt reaches the existing note one level above, without changing it.

Terminal window
mkdir project

No output means mkdir created the directory without reporting a problem. Check the name in your prompt before continuing: relative paths start there.

Put a copy of the note inside it:

Terminal window
cp ../Documents/notes.txt project/

cp copies. Its first path is the source, what you copy from. Its second is the destination, where the copy goes. Because project is an existing directory, the copy keeps the name notes.txt. The source stays in Documents.

Give the working copy a name that describes its job:

Terminal window
mv project/notes.txt project/plan.txt
ls project
Captured output
plan.txt

mv moves a source to a destination. Here both paths have the same parent, so it renames the file. There is no longer a notes.txt inside project; there is a plan.txt. The original ../Documents/notes.txt remains.

Create, copy, and rename
learner@rootvolt:~/lab$ cd b06learner@rootvolt:~/lab/b06$ mkdir projectlearner@rootvolt:~/lab/b06$ cp ../Documents/notes.txt project/learner@rootvolt:~/lab/b06$ mv project/notes.txt project/plan.txtlearner@rootvolt:~/lab/b06$ 

Files ~/lab/b06

  • ~
    • lab/b06/cwd
      • project/
        • plan.txtcreated-rw-r--r--

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. mkdir creates project, cp adds notes.txt, and mv removes that name and creates plan.txt.
Steps (8)
  1. Type cd b06
  2. Press Enter
  3. Type mkdir project
  4. Press Enter
  5. Type cp ../Documents/notes.txt project/
  6. Press Enter
  7. Type mv project/notes.txt project/plan.txt
  8. Press Enter

Watch project appear, then notes.txt inside it. On the rename step, the old name is marked removed and the destination created. These are name changes, not two copies. The static transcript and final tree show the same outcome: project contains only plan.txt.

You want a directory for the first day’s drafts. Guess first: does this create both drafts and day1?

Terminal window
mkdir project/drafts/day1
Run it, then compare with your guess
Captured output
mkdir: cannot create directory ‘project/drafts/day1’: No such file or directory

mkdir names the full path, but its parent project/drafts does not exist. Without -p, it does not create missing parents.

Terminal window
mkdir -p project/drafts/day1
mkdir -p project/drafts/day1
ls -R project/drafts
Captured output
project/drafts:
day1
project/drafts/day1:

The first line creates the missing directories. The second succeeds silently too: -p accepts an existing directory. ls -R, from B04.3, lists the directory and its subdirectories. Nothing follows the day1: heading because it is empty.

Terminal window
touch project/checklist.txt
ls -l project/checklist.txt
Captured output
-rw-r--r-- 1 learner learner 0 Nov 11 08:23 project/checklist.txt

The size before the date is 0: there is no content. When its path does not exist, touch creates an empty file. It does not open an editor or ask you to type text.

Guess first: does touch also empty a file that already contains text? Read the note’s modification time, run touch, then check again:

Terminal window
ls -l --full-time project/plan.txt
touch project/plan.txt
ls -l --full-time project/plan.txt
cat project/plan.txt
Run it, then compare with your guess
Captured output
-rw-r--r-- 1 learner learner 40 2026-11-11 08:23:12.944446467 +0000 project/plan.txt
Captured output
-rw-r--r-- 1 learner learner 40 2026-11-11 08:23:18.533475384 +0000 project/plan.txt
Captured output
Buy printer paper
Renew lab VM snapshot

The time changed; the size and text did not. For an existing file, touch updates its access and modification timestamps, the recorded times for reading and changing it. It does not empty the contents. ls -l shows modification time; --full-time includes seconds and finer detail so you can see this short interval.

Your timestamps and time zone will differ. The Files panel does not track timestamps, so touching an existing file produces no change mark there. The terminal metadata above shows the change.

An existing directory and an unused filename mean different things:

Terminal window
cp ../Documents/notes.txt project/reference.txt
cp project/plan.txt project/drafts/
ls project/drafts
Captured output
day1 plan.txt

The first destination is an unused filename, so the copy is called reference.txt. The second is an existing directory, so the copy keeps plan.txt. A trailing / makes your directory intent clear; it does not create the directory.

Move the reference into the drafts:

Terminal window
mv project/reference.txt project/drafts/reference.txt
ls project
ls project/drafts
Captured output
checklist.txt drafts plan.txt
Captured output
day1 plan.txt reference.txt

Copying left the source in place. Moving removed its old name. mv can change the directory, the filename, or both. Unlike cp, mv moves a whole directory without -r, because it changes where the name points instead of copying the contents.

To copy the small drafts directory, first see what plain cp does:

Terminal window
cp project/drafts archive
Captured output
cp: -r not specified; omitting directory 'project/drafts'

The message says the source is a directory. Add -r, for a recursive copy: copy the directory, its contents, and directories inside it.

Terminal window
cp -r project/drafts archive
ls -R archive
Captured output
archive:
day1 plan.txt reference.txt
archive/day1:

Because archive did not exist, it becomes the copy of drafts. If archive already existed as a directory, the copy would go inside it as archive/drafts. Check first rather than assuming the same command always makes the same layout.

Both cp and mv can overwrite an existing destination file. You can lose the version previously available at that path. Learn the safeguard first. -i means interactive: ask before overwriting.

Terminal window
cp -i ../Documents/report.txt project/plan.txt
Captured output
cp: overwrite 'project/plan.txt'?

The command is waiting for your answer, not another shell command. Press Ctrl+C to cancel, as in B03.4. Then check:

Terminal window
cat project/plan.txt
Captured output
Buy printer paper
Renew lab VM snapshot

The notes remain. mv -i provides the same kind of safeguard when moving or renaming onto an existing file.

Terminal window
cp ../Documents/report.txt project/plan.txt
cat project/plan.txt
Captured output
Quarterly lab report

The report replaced the working note. The command succeeded silently, so silence alone cannot tell you whether a destination was safe.

Creating an existing directory without -p.

Terminal window
mkdir project
Captured output
mkdir: cannot create directory ‘project’: File exists

File exists names a collision. Here it is a directory; you do not need to create it again. -p accepts that directory, but cannot turn an existing regular file into a directory.

A misspelled source.

Terminal window
cp ../Documents/missing.txt project/
Captured output
cp: cannot stat '../Documents/missing.txt': No such file or directory

cannot stat means cp could not inspect that source. Read the ending: no file has that path. Check the spelling with ls or Tab. Changing the destination does not fix a missing source.

Goal: Prepare a handoff with a backup taken before a rename.

Inside ~/lab/b06, create handoff/source. Copy the original Documents/report.txt into it and add an empty ready.txt. Copy the whole source directory to the unused name handoff/backup. Then rename only the source’s report to summary.txt.

Expected: The backup keeps report.txt; the source has summary.txt. Both contain the report text, and each directory has an empty ready.txt.

Hint

Create missing parents with -p. Copy the directory before renaming its report. From b06, the original report is ../Documents/report.txt.

Solution
Terminal window
mkdir -p handoff/source
cp ../Documents/report.txt handoff/source/
touch handoff/source/ready.txt
cp -r handoff/source handoff/backup
mv handoff/source/report.txt handoff/source/summary.txt
ls -R handoff
Captured output
handoff:
backup source
handoff/backup:
ready.txt report.txt
handoff/source:
ready.txt summary.txt
Terminal window
cat handoff/backup/report.txt
cat handoff/source/summary.txt
Captured output
Quarterly lab report
Captured output
Quarterly lab report

Each cat prints the same line. Renaming the source did not rename the independent backup.

Use this after finishing, or after stopping partway through. First return to ~/lab/b06 and inspect only this lesson’s three directories:

Terminal window
cd ~/lab/b06
ls -AR project archive handoff

-A includes hidden entries; -R lists subdirectories. After the solution, the captured listing is:

Captured output
archive:
day1 plan.txt reference.txt
archive/day1:
handoff:
backup source
handoff/backup:
ready.txt report.txt
handoff/source:
ready.txt summary.txt
project:
checklist.txt drafts plan.txt
project/drafts:
day1 plan.txt reference.txt
project/drafts/day1:

If you stopped earlier, you will see the files left at that point instead.

Terminal window
rm -r ~/lab/b06/project ~/lab/b06/archive ~/lab/b06/handoff

When all three directories exist, it succeeds silently. If a directory is already absent, rm reports it and still removes the others. With project absent and the other two present, the captured message is:

Captured output
rm: cannot remove '/home/learner/lab/b06/project': No such file or directory

Restart from Give the project a place. This reset leaves the B06 parent directory in place and preserves the other lessons’ work.

  • mkdir creates directories; -p also creates missing parents and accepts existing directories.
  • touch creates an empty file when none exists. On an existing file, it updates timestamps and preserves content.
  • cp keeps the source; mv removes its old name. An existing destination directory receives the source’s name.
  • cp -r copies a directory and its contents. Check whether the destination already exists.
  • cp -i and mv -i ask before overwriting. Cancel if the named destination is one you need to keep.

Next, Removing files deliberately (B06.2) removes disposable files and directories while preserving your earlier work.