dcd9fedd-5509-4f32-8754-e48.../docs/02_navigation_file_ops.md

5.9 KiB
Raw Blame History

Step 2 — Navigation & File Operations (Ubuntu 24)

Type along exactly as shown. This step is written for absolute beginners and assumes no prior Linux experience.
Estimated time: ~1015 minutes


What youll learn

  • What a path is (absolute vs. relative)
  • How to move around with cd
  • How to create, inspect, copy, move/rename, and delete files and folders
  • How to work safely (confirmation prompts), and handle names with spaces
  • How to verify your actions with ls -l

This expands the original brief step (pwd → mkdir → cd → create file → ls -l) into a guided mini-lesson with checks, expected outputs, and troubleshooting.


Create a clean practice area so you dont accidentally change other files:

mkdir -p ~/playground && cd ~/playground
  • ~ is your home directory (e.g., /home/yourname).
  • mkdir -p creates all missing parent folders safely.
  • cd changes your current working directory.

If something goes wrong, you can reset this folder later:

cd ~ && rm -rf ~/playground && mkdir -p ~/playground && cd ~/playground

1) Where am I? (Print Working Directory)

pwd

Expected output: the full path of your current directory (e.g., /home/yourname/playground).
This is called an absolute path (it starts with /).


2) Create folders (directories)

Create a single folder:

mkdir projects

Create a nested folder tree in one go:

mkdir -p projects/alpha

Verify:

ls -la
  • -l = long list (permissions, owner, size, date)
  • -a = include hidden items (names starting with a dot)

Hidden files often store settings and are called dotfiles (e.g., .bashrc).


3) Move around with cd

Change into the new folder:

cd projects
pwd

Go down another level:

cd alpha
pwd

Go up one level:

cd ..
pwd

Jump home quickly:

cd ~
pwd

Return to your last directory:

cd -

Think of cd as “change directory” — just like opening folders in a file explorer.


4) Create files (3 common ways)

Make sure youre inside ~/playground (use pwd to check), then:

A) Create with content (overwrite if exists):

echo "Hello Linux" > hello.txt

B) Append more content (keeps existing lines):

echo "Second line" >> hello.txt

C) Create an empty file:

touch empty.txt

D) Create and edit with a text editor (nano):

nano notes.txt
# Type: My first notes line.
# Save: Ctrl+O, Enter.  Exit: Ctrl+X

Check what you made:

ls -l
cat hello.txt

> writes/overwrites; >> appends. touch creates an empty file or updates the timestamp if it already exists.


5) Inspect details with ls

List files with extra info:

ls -l

Youll see lines like:

-rw-r--r-- 1 yourname yourname   12 Sep  8 10:00 hello.txt
  • -rw-r--r--permissions
  • first yournameowner
  • second yournamegroup
  • 12 → file size in bytes
  • hello.txt → file name

Try human-readable sizes:

ls -lh

Show hidden files too:

ls -la

6) Copy, move/rename, and delete files

Copy a file:

cp hello.txt hello.bak
ls -l hello*

Move/rename a file (same command):

mv hello.bak projects/alpha/
mv empty.txt empty-renamed.txt
ls -l projects/alpha
ls -l empty-renamed.txt

Delete a file (interactive/safe):

rm -i empty-renamed.txt

-i asks “remove regular file…?” before deleting — excellent for beginners.

Delete a folder tree (careful!):

rm -r projects/alpha
mkdir -p projects/alpha  # recreate it for the next step

rm -r removes directories recursively (everything inside). Use with caution.


7) Names with spaces

Two safe methods:

Quotes:

mkdir "my notes"
echo "a line" > "my notes/file with space.txt"
ls -l "my notes"

Escaping spaces with backslashes:

echo "another line" > my\ notes/another\ file.txt
ls -l my\ notes

If you forget quotes/backslashes, the shell will think youre typing multiple arguments and show an error like “No such file or directory”.


8) Wildcards (globbing)

List all .txt files:

ls -l *.txt

List everything that starts with he:

ls -l he*

If a wildcard matches nothing, the shell may pass the literal pattern to the command (e.g., ls: cannot access '*.txt': No such file or directory). Thats normal.


9) Verify what you did (mini-checklist)

  • Where are you?
    pwd
    
  • Do you see the files you created?
    ls -la
    
  • Do you see the content you wrote?
    cat hello.txt
    
  • Is there a copy in projects/alpha/?
    ls -l projects/alpha
    

10) Cleanup (optional)

If you want to start fresh:

cd ~ && rm -rf ~/playground && mkdir -p ~/playground && cd ~/playground

Troubleshooting

Q: Permission denied when creating files?
A: Ensure youre working inside your home (e.g., ~/playground) where you have write permission.

Q: No such file or directory when using spaces?
A: Use quotes "like this" or escape spaces: my\ notes/that\ file.txt.

Q: I ran rm -r in the wrong place!
A: This is why we practice in ~/playground. In real life, double-check paths before you press Enter.


Quick Quiz (1 minute)

  • How do you print the current directory path?
  • Whats the difference between > and >>?
  • Which command renames a file?
  • Show a long listing including hidden files—what options do you use?
  • Two ways to handle file names with spaces?

Answers: pwd; > overwrites, >> appends; mv; ls -la; quotes or backslashes.


Next Step

Continue to Step 3 — Searching & Text Viewing to learn grep, find, less, and more!