The filesystem is where your work lives. Everything you write, compile, and run exists as a file somewhere in the tree. You need to be able to create, read, copy, move, and delete files without touching a mouse.
Creating
touch hello.ctouch creates an empty file if it does not exist. If the file
already exists, it updates the modification timestamp. You will use it
to create placeholder files and to update timestamps when a Makefile
needs persuading.
Don't worry about what Make[1][2] and Makefiles are yet — we'll cover
them in detail later in the curriculum. For now, just know that touch is
a handy tool for creating empty files and adjusting timestamps.
mkdir projects
mkdir -p projects/f01/src # -p creates parent directories as neededmkdir creates directories. The -p flag is essential — without it,
mkdir projects/f01/src fails if projects/f01 does not exist yet.
Reading
cat hello.ccat prints the contents of a file to stdout. It is short for
concatenate — its original purpose was to join files together — but
you will mostly use it to inspect short files quickly.
For longer files, cat scrolls past everything before you can read
it. Use less instead:
less /etc/passwdless lets you page through the file. j/k or arrow keys to
scroll. /pattern to search. q to quit.
head -20 /var/log/syslog # first 20 lines
tail -20 /var/log/syslog # last 20 lines
tail -f /var/log/syslog # follow: print new lines as they appearhead and tail are your tools for inspecting the beginning and end
of files. tail -f is indispensable for watching log files in real
time.
Copying and moving
cp hello.c hello_backup.c # copy a file
cp -r projects/ projects_backup/ # copy a directory (-r: recursive)
mv hello.c src/hello.c # move (or rename) a file
mv projects/ old_projects/ # rename a directorymv moves a file or directory to a new location and always removes the
original — there is no copy left behind, whether the destination is on
the same filesystem or a different one.
You can use mv to rename files and directories in place:
mv old_name new_name is a rename. mv file.c src/file.c is both a move
and a rename. The command does not distinguish; it just changes the path.
Deleting
rm hello_backup.c # delete a file
rm -r old_projects/ # delete a directory and everything in itThere is no trash. There is no undo. When rm runs, the file is gone.
I ran rm -r on the wrong directory once, early in a project. An hour
of work, gone in 40 milliseconds. The lesson: read the command before
you press Enter. Every time. Without exception.
It did not feel like a lesson at the time. It felt like losing an hour of work because I was moving fast and did not read the path. The directory I deleted was not the one I intended. The commands looked right in my head before I pressed Enter; they were not. That is the only way this happens. The lesson is not "be careful" — it is "read the command." Every time. Before Enter. The habit takes a week to build and saves you from the mistake that costs a day.
Reading a file you just created
echo "int main(void) { return 0; }" > hello.c
cat hello.cThe > operator redirects stdout into a file. You will learn exactly
what that means in the pipes and redirects page. For now: it is how
you write content into a file from the command line.