The Unix Shell
To a casual user, the computer is a collection of icons. To a power user or a developer, the computer is a Shell. The shell is a special program that takes keyboard commands and passes them to the operating system to execute. On most Unix-like systems (Linux, macOS, BSD), the default shell is Bash or Zsh.
Navigating the File System
Imagine your files are a tree. The root of the tree is /.
pwd: Print Working Directory. Shows you exactly where you are.ls: List files in the current folder.ls -l: Long format (shows sizes and permissions).ls -a: Show hidden files (those starting with a.).
cd: Change Directory.cd Documents: Go into a folder.cd ..: Go “up” one level.cd ~: Go to your “Home” folder.
mkdir: Make Directory.
File Manipulation
touch filename.txt: Create a new empty file.cp source destination: Copy a file.cp -r folder1 folder2: Copy an entire folder recursively.
mv old_name new_name: Move or rename a file.rm filename: Remove (delete) a file.- WARNING: There is no “Trash Can” in the shell. Once you
rma file, it is gone forever. rm -rf folder: Forcefully and recursively delete a folder. Use with extreme caution.
- WARNING: There is no “Trash Can” in the shell. Once you
Reading and Searching Files
cat file: Catenate and print the whole file to the screen.less file: Open a file in a viewer that lets you scroll up and down (pressqto quit).head -n 10 file: Show the first 10 lines.tail -n 10 file: Show the last 10 lines.grep "search_term" file: Search for text within a file.grep -r "error" /var/log: Search for the word “error” in every log file.
The Power of Redirection and Pipes
This is where the Unix philosophy shines.
Redirection
ls > files.txt: Write the output oflsinto a file namedfiles.txt(overwrites).ls >> files.txt: Append the output to the end of the file.command 2> errors.txt: Save only the error messages to a file.
Pipes |
The pipe takes the output of the command on the left and feeds it as the input to the command on the right.
ls -l | grep ".jpg"
Translation: List all files in long format, then search that list for anything ending in .jpg.
Permissions: Who owns what?
Unix is multi-user. Every file has an owner and a group.
If you type ls -l, you will see something like -rwxr-xr--.
- r: read
- w: write
- x: execute
The letters are grouped in threes: Owner, Group, and Others.
chmod 755 script.sh: Make a script executable by everyone.chown user:group file: Change who owns the file.sudo command: Substitute User Do. Run a command with the powers of the “Root” (Administrator) user.
Process Management from the Shell
top: Real-time view of running processes (like Task Manager).ps aux: Snapshot of every running process.kill PID: Stop a process by its ID.killall name: Stop every process with a certain name (e.g.,killall chrome).
Summary Table of Essentials
| Command | Purpose | Example |
|---|---|---|
cd | Change directory | cd /var/log |
ls | List files | ls -lh |
cat | View file content | cat config.json |
grep | Search text | grep "TODO" main.c |
find | Find files | find . -name "*.pdf" |
ssh | Secure Shell (Remote access) | ssh user@server.com |
man | Manual (Help) | man grep |
The best way to learn the shell is to use it. Try to perform your daily tasks—organizing photos, searching for documents—using only these commands for an hour. You will quickly realize why professional developers find the GUI so limiting.
In the next module, we’ll look at the Windows alternative: PowerShell, and see how it differs from the Unix approach.