T.02 · Terminal Fundamentals

Files & Directories

The handful of commands every lab in this course will ask you to use.

This page is a working reference for the commands you'll type most often. If you can move around the filesystem, list directory contents, read a file, search for text, and connect commands together with pipes, you can do 90% of what any terminal task asks of you. The rest is just specific tools layered on this base.

Where am I and what's here?

pwd — print working directory

Tells you the absolute path of where you currently are in the filesystem.

$ pwd /home/alice/projects/cyberlab

ls — list directory contents

List what's in the current directory (or any directory you name).

$ ls # just names $ ls -l # long format: permissions, owner, size, date $ ls -la # also show hidden files (the ones starting with .) $ ls -lh # human-readable sizes (1.2K instead of 1234) $ ls -lt # sort by modification time, newest first $ ls /var/log # list a specific directory without cd'ing there

The flags combine: ls -lhart = long format, human sizes, all files, reverse time order. The single most-typed flag combination in the world is probably ls -la.

cd — change directory

$ cd /etc # absolute path $ cd projects/cyberlab # relative to current directory $ cd .. # go up one directory $ cd ~ # go to your home directory $ cd # also goes to home (no argument = home) $ cd - # go back to the directory you were just in

Reading files

cat — print whole file to screen

$ cat /etc/hostname workstation $ cat file1.txt file2.txt # concatenate — that's where the name comes from

Use for short files. For long files use less instead — cat on a 10,000-line log file scrolls past too fast.

less — page through a file

$ less /var/log/syslog

Opens the file in a pager. Inside less: arrows or j/k to scroll line by line; space or Page Down to page; g to jump to top; G to jump to end; /pattern to search forward; ?pattern backward; n next match; q to quit.

head and tail — first/last lines

$ head file.log # first 10 lines $ head -n 50 file.log # first 50 lines $ tail file.log # last 10 lines $ tail -f file.log # follow: keep printing new lines as they're appended

tail -f is the standard way to watch a log file in real time.

Making changes — copy, move, delete

cp, mv, rm

$ cp file.txt backup.txt # copy $ cp -r myproject backup-of-project # copy a directory recursively $ mv old.txt new.txt # rename or move $ rm file.txt # delete (no recycle bin!) $ rm -r olddir # delete directory and contents
There is no undo. rm deletes immediately and permanently. There is no recycle bin. Especially scary: rm -rf / would erase the entire system. Build the habit of typing the destination first and double-checking before pressing Enter.

mkdir, touch, rmdir

$ mkdir newdir # create a directory $ mkdir -p a/b/c/d # create all parents as needed $ touch file.txt # create empty file (or update timestamp if it exists) $ rmdir emptydir # remove EMPTY directory only

Finding things: find and grep

find — find files by name, type, age, size

$ find . -name "*.log" # files matching *.log in current dir + subdirs $ find /etc -type f -name "*.conf" # type f = file (d = directory) $ find . -mtime -7 # modified in the last 7 days $ find . -size +100M # larger than 100 MB $ find . -name "*.tmp" -delete # find + delete in one call (CAREFUL)

grep — find text inside files

$ grep "error" app.log # find lines containing "error" $ grep -i "error" app.log # case-insensitive $ grep -n "error" app.log # also show line numbers $ grep -r "TODO" . # recursive: search all files under current dir $ grep -v "DEBUG" app.log # INVERT: lines NOT containing DEBUG $ grep -c "401" access.log # count matching lines

Pipes and redirects — connecting commands

The Unix philosophy: small tools that do one thing well, connected together. Pipes (|) and redirects (>, >>, <, 2>) are the glue.

Pipe — |

Sends the output of the left command as the input to the right command.

$ cat access.log | grep "404" | wc -l # cat: print the file # grep: keep only lines containing "404" # wc -l: count lines # Result: how many 404 responses were in the log. $ ps aux | grep "nginx" # show only nginx processes $ ls -la | less # long ls output paged through less

Redirect output to file — > and >>

$ ls -la > listing.txt # OVERWRITE listing.txt with ls output $ date >> timestamps.txt # APPEND date to file (preserves existing content) $ grep "error" *.log > errors.txt # save matching lines to a file

Single > replaces. Double >> appends. Use append when you're building up a log over time; use overwrite when you want a fresh file.

Redirect input from file — <

$ sort < names.txt # feed names.txt to sort as input

Most commands accept filenames directly, so < is less common in practice. Still useful when a program only reads from stdin.

Redirect error stream — 2> and 2>&1

Programs have two output streams: stdout (1) for normal output, stderr (2) for errors. By default both print to your terminal.

$ find / -name "*.conf" 2> /dev/null # discard error messages (Permission denied spam) $ command > out.log 2> err.log # separate files for normal + error output $ command > all.log 2>&1 # merge stderr into stdout, both to all.log
The /dev/null trick. /dev/null is the Unix "discard" pseudo-file. Anything you redirect to it disappears silently. Use it to hide noisy error output you don't care about.

Permissions — the quick read

When you run ls -l, the leftmost column looks like -rw-r--r--. That's a 10-character permission string:

For the full treatment of users, groups, and permissions — including chmod and the setgid bit — see the Linux Administration reference. Permissions are covered in much greater depth there.

The point

These commands are the muscle memory of a Linux user. You don't need to memorize all of them today — you'll learn the flags you need as you need them. What matters is recognizing the pattern: small tools, connected with pipes, with redirects to capture output. Once that pattern is automatic, the terminal stops being intimidating and becomes a place you get work done.

References

Formatted in APA 7. Alphabetized by first author's last name.

  1. GNU Project. (n.d.). GNU coreutils manual. Free Software Foundation. https://www.gnu.org/software/coreutils/manual/
  2. Kerrisk, M. (n.d.). find(1): Linux manual page. Linux man-pages project. https://man7.org/linux/man-pages/man1/find.1.html
  3. Robbins, A. (2005). Effective awk programming (3rd ed.). O'Reilly Media.