Files, Directories & Paths
mkdir, cd, cp, mv, rm — building and organizing your workspace from the command line
Quick check: What does
cd ..do? If you know, skip to File Operations.Practice these topics: File Navigation · File Operations · Wildcards
After this lesson, you will be able to:
- Create directories with
mkdirandmkdir -p - Navigate with
cdusing..,~, and- - Copy, move, rename, and delete files and directories
- Use wildcards (
*,?,[...]) to match multiple files - View file contents with
cat,head,tail
Building Your Workspace
In CSCD 210, VS Code managed your files. Now you build the structure yourself.
cd ~ # go home
mkdir cscd240 # create course directory
mkdir -p cscd240/lab1/src # create nested dirs in one step
mkdir -p creates all intermediate directories. Without -p, the command fails if a parent does not exist.
Navigation: cd
cd cscd240 # move into cscd240
cd lab1/src # relative path (from where you are)
cd /home/student # absolute path (from root)
cd .. # up one level
cd ../.. # up two levels
cd ~ # home directory
cd - # toggle to previous directory
Key Insight:
cd -toggles between your last two directories. This is the single most useful navigation shortcut when you are working in two places.
Creating Files
touch hello.c Makefile # creates empty files (or updates timestamps)
You will fill these files with code starting in Week 4.
Viewing Files
cat notes.txt # print entire file
head -5 notes.txt # first 5 lines
tail -3 notes.txt # last 3 lines
wc -l notes.txt # count lines
cat is fine for short files. For long files, use less (scroll with arrows, quit with q).
Copying, Moving, and Deleting
cp hello.c backup.c # copy file
cp -r lab1 lab1-backup # copy directory (requires -r)
mv backup.c old_hello.c # rename
mv old_hello.c ../ # move to parent directory
rm old_hello.c # delete file (PERMANENT)
rm -r lab1-backup # delete directory and contents
Common Pitfall:
rmis permanent. There is no trash can, no undo. Alwayslsbefore yourm. Userm -ifor interactive confirmation until you trust your typing.
Wildcards
The shell expands wildcards before the command runs:
| Pattern | Matches |
|---|---|
*.c |
all .c files |
lab?.c |
lab1.c, lab2.c (exactly one character) |
lab[123].c |
lab1.c, lab2.c, lab3.c |
*.[ch] |
all .c and .h files |
ls *.c # list all C source files
rm *.o # remove all object files
lab1.c, lab2.c, lab10.c, lab2.h. What does ls lab?.c match?? wildcard matches exactly one character. lab?.c matches lab1.c and lab2.c. It does not match lab10.c (two characters after "lab") or lab2.h (wrong extension).
What Comes Next
You can create, navigate, copy, move, and delete. Next week starts with man pages and getting help when you are stuck.