Bytebook Compiler

Developer Tools

The Linux Command Line

Work like a developer in the terminal: the file system tree, navigating and managing files, viewing and searching text, permissions, pipes and redirection, processes and compiling code.

4 min read4-question quiz

Servers, cloud machines, Raspberry Pis and most developer tools run Linux, and they are driven from the command line (also called the terminal or shell). It is faster than clicking once you know a few dozen commands — and in internships you will be expected to use it.

The program that reads your commands is the shell (usually bash or zsh). On Windows, install WSL (Windows Subsystem for Linux) to get a real Linux shell; macOS already has a Unix shell.

The file system

Linux organises everything in one tree starting at the root, /:

projectsnotes.txtashahomeetcbinusrtmp/
Part of a Linux directory tree. Asha's home folder is /home/asha, also written ~.
Path Meaning
/ the root of the whole tree
~ your home folder, e.g. /home/asha
. the current folder
.. the parent folder
/home/asha/notes.txt an absolute path (starts at /)
projects/lab1.c a relative path (starts at the current folder)
Terminal
pwd                     # print working directory: where am I?
ls                      # list files
ls -la                  # long format, including hidden files (names starting with .)
cd projects             # change into a folder
cd ..                   # go up one level
cd ~                    # go home (plain `cd` does the same)
cd -                    # go back to the previous folder

Managing files and folders

Terminal
mkdir lab1                      # make a folder
mkdir -p cse101/week2/day3      # make nested folders at once
touch main.c                    # create an empty file
cp main.c backup.c              # copy
cp -r lab1 lab1-backup          # copy a folder recursively
mv backup.c old/                # move into a folder
mv draft.txt final.txt          # rename
rm old.txt                      # delete a file (no recycle bin!)
rm -r lab1-backup               # delete a folder and everything inside

Viewing and searching text

Terminal
cat notes.txt                   # print a whole file
less big.log                    # scroll through a long file (q to quit)
head -n 5 data.csv              # first 5 lines
tail -n 20 server.log           # last 20 lines
tail -f server.log              # follow a growing file live
wc -l *.c                       # count lines in every .c file
grep "main" *.c                 # find lines containing "main"
grep -rn "TODO" src/            # search recursively, with line numbers
find . -name "*.py"             # find files by name

Pipes and redirection

Linux commands are small tools that you combine:

Symbol Meaning Example
> write output to a file (overwrite) ls > files.txt
>> append output to a file echo "done" >> log.txt
< read input from a file ./a.out < input.txt
| pipe one command's output into the next cat log.txt | grep ERROR | wc -l
Terminal
history | grep gcc | tail -n 3          # my last 3 gcc commands
ls -l | sort -k5 -n | tail -n 1          # the largest file in this folder
./marks < test1.txt > result1.txt       # test a program with saved input

Permissions

ls -l shows a permission string like -rwxr-xr--: the file type, then read/write/execute for the owner, the group and others.

Terminal
chmod +x script.sh              # make a script executable
chmod 644 notes.txt             # rw- r-- r-- (6 = 4+2, 4 = read)
sudo apt install gcc            # run one command as the administrator (root)
Digit Permissions
7 rwx (4 + 2 + 1)
6 rw-
5 r-x
4 r--

Processes

Terminal
ps aux | grep python            # list running processes
top                             # live view of CPU and memory use (q to quit)
kill 4321                       # stop process 4321
./server &                      # run in the background
Ctrl+C                          # stop the program running in the terminal

Compiling and running from the terminal

Terminal
gcc -Wall -o hello hello.c && ./hello          # C: compile, then run only if it compiled
g++ -std=c++17 -o app main.cpp && ./app         # C++
python3 script.py                               # Python
./hello < input.txt                             # feed input from a file

Command cheat sheet

Task Command
Where am I / what's here pwd, ls -la
Move around cd folder, cd .., cd ~
Create mkdir, touch
Copy / move / delete cp, mv, rm (-r for folders)
Read files cat, less, head, tail
Search grep, find
Help man ls, ls --help
Install software (Ubuntu) sudo apt install name
Download curl -O url, wget url

Test yourself

4 questions · pick an answer to check it instantly

  1. 1

    Which command shows the folder you are currently in?

  2. 2

    What does cd .. do?

  3. 3

    What does ls | wc -l print?

  4. 4

    Which permission number gives the owner read, write and execute, and everyone else only read?