The Unix shell is a command-line environment for interacting with your operating system, typically macOS or Linux.
It allows you to launch programs, manage files, and much more, all through a terminal interface.
echo "Hello, world!"
Instead of clicking around with a mouse, the shell lets you tell your computer what to do by typing words and commands.
This document touches on the basics of:
- Navigating directories
- Working with files
- Viewing file content
- And useful tips and tricks
The Unix shell lets you create, view, copy, move, and delete files quickly using commands.
Instead of using a graphical interface which you may be used to, you can type commands to handle files right from the terminal.
touch newfile.txt
This creates an empty file called newfile.txt
if it doesn't already exist.
cp file1.txt file2.txt
This copies file1.txt
to a new file called file2.txt
.
mv file1.txt Documents/
This moves file1.txt
into the Documents directory. It can also rename files if you provide a new name.
rm filename
- delete a filecat filename
- print file contents to the terminalnano filename
- open a file for editing in a terminal text editorcp -r folder1 folder2
- copy entire foldersmv oldname.txt newname.txt
- rename a file
Using these commands, you can quickly manage many files without leaving the command line.
The Unix shell provides many commands to look inside files without opening them in a full editor.
This is useful for quickly checking logs, configuration files, or code.
cat filename.txt
This prints the entire contents of a file directly in the terminal.
less filename.txt
This opens a file in a scrollable viewer so you can move up and down easily.
head filename.txt
Displays just the first few lines of a file, useful for a quick peek.
tail filename.txt
Shows the last few lines of a file, helpful for checking logs.
tail -f filename
- watch new lines appear in a growing file like logsless +F filename
- view a file and follow new changes like a live log viewerhead -n 20 filename
- show the first 20 lines of a filecat file1 file2
- display multiple files at onceless -N filename
- view a file with line numbers
These commands make it quick and easy to read file contents directly from the command line.
Besides basic commands, the Unix shell has many small features and shortcuts that can make your work much faster and easier.
One of the most helpful tools is tab completion. When typing a file or folder name, you can press the Tab
key to automatically complete it or show suggestions.
cd Doc
Pressing Tab
after typing Doc
might complete it to Documents/
if that folder exists.
Here are some other useful tips for working in the shell:
- Use the up and down arrow keys to scroll through your recent commands instead of typing them again.
- Type
history
to see a list of past commands. - Press
Ctrl + C
to stop a running command. - Use
clear
to clear your terminal screen. - Use
..
to move up one directory level, e.g.cd ..
- Use
man command
to read the manual page and learn how a command works.
Practicing these small shortcuts helps make using the shell feel much faster and more comfortable.