1. Core Terminology
- Command Line
- A text-based interface used to interact with a computer by entering commands.
- Shell
- A program that interprets commands and executes them through the operating system.
- Terminal
- An application that provides access to a command-line shell.
- CLI
- Short for Command-Line Interface, a way of interacting with software by typing commands.
- Command
- An instruction entered into the command line to perform a specific action.
- Argument
- A value provided to a command that specifies what the command should operate on.
- Option / Flag
-
An argument that modifies how a command behaves, usually beginning with
-or--. - Prompt
- The text displayed by a shell indicating that it is ready to accept a command.
- Path
- A location that identifies a file or directory in a file system.
- Absolute Path
- A complete path that starts from the root directory.
- Relative Path
- A path interpreted relative to the current working directory.
- Directory
- A container used to organize files and other directories.
- Working Directory
- The directory in which the current shell or command is operating.
- Environment Variable
- A named value provided by the operating system or shell that can provide configuration or other information to programs.
- Executable
- A file or program that the operating system can run.
- Script
- A file containing a sequence of commands that can be executed automatically.
- Process
- A running instance of a program managed by the operating system.
- Exit Code
- A numerical value returned by a program to indicate whether it completed successfully or encountered an error.
- Standard Input (stdin)
- The default input stream from which a program receives data.
- Standard Output (stdout)
- The default output stream used by a program to display normal results.
- Standard Error (stderr)
- The default output stream used by a program to display errors and diagnostic information.
- Pipe
- An operator that sends the output of one command into the input of another command.
- Redirection
- An operation that changes where a command receives input from or sends output to.
- Permission
- A rule that determines what actions a user or program is allowed to perform on a file or system resource.
- Root
- The highly privileged superuser account on Unix-like systems.
- Command History
- A record of previously entered commands that can be viewed and reused.
- Autocomplete
- A feature that automatically completes commands, file names, or other text as you type.
2. Core Commands
Note: Command names, variables, and file paths are generally case-sensitive on Unix-like systems.
pwdPrint Working Directory
Shows the absolute path of your current working directory.
Examples
pwd
Example output:
/home/yourname/Projects
lsList
Lists files and directories in the current directory.
Examples
ls
ls -a
ls -l
ls -la
Common Options
-a- Includes hidden files and directories.
-l- Uses a detailed listing format.
-h-
Displays file sizes in a human-readable format when used with
-l.
Useful Example
ls -lah
Shows hidden files using a detailed, human-readable listing.
cdChange Directory
Changes the current working directory.
Examples
cd ~/Projects
cd ..
cd /home/yourname
cd /
cd ~
Special Paths
~- Your home directory.
.- The current directory.
..- The parent directory.
/- The root directory.
Useful Example
cd ~/Projects
pwd
mkdirMake Directory
Creates one or more directories.
Examples
mkdir my-folder
mkdir project docs
Useful Option
mkdir -p project/src/components
The -p option creates parent directories when they do not already exist.
touchCreate or Update a File
Creates an empty file if it does not exist. If the file already exists, it updates its modification timestamp.
touch README.md
touch index.html style.css script.js
catConcatenate / Display
Displays the contents of one or more files in the terminal.
cat README.md
cat file1.txt file2.txt
It is useful for quickly viewing small files.
lessView Files Page by Page
Opens a file in a scrollable terminal viewer. It is useful for reading large files.
less README.md
less large-file.log
Press q to exit.
echoPrint Text
Prints text or the value of a variable to standard output.
echo "Hello, world"
echo "$HOME"
Redirection
echo "Hello" > myfile.txt
echo "More text" >> myfile.txt
>- Writes output to a file, replacing its existing contents.
>>- Appends output to the end of a file.
cpCopy
Copies files or directories from one location to another.
cp file.txt backup.txt
cp file.txt ~/Documents/
Copy a Directory
cp -r project project-backup
The -r option copies a directory and its contents recursively.
mvMove / Rename
Moves a file or directory. It can also be used to rename them.
mv file.txt Documents/
mv old-name.txt new-name.txt
rmRemove
Removes files or directories.
rm file.txt
Remove a Directory
rm -r old-folder
Be careful with rm: removed files are generally not moved to a recycle bin.
clearClear the Terminal
Clears the visible contents of the terminal screen.
clear
manManual
Opens the manual page for a command.
man ls
man mkdir
man cp
Manual pages are one of the primary ways to learn how Unix commands work.
whichLocate an Executable
Shows the path of the executable that the shell would run for a command.
which python
which git
which bash
whoamiCurrent User
Displays the username of the current user.
whoami
dateDisplay Date and Time
Displays the current system date and time.
date
historyCommand History
Displays commands that were previously entered in the shell.
history
You can usually press the ↑ and ↓ arrow keys to navigate through previous commands.
grepSearch Text
Searches for matching text within files or command output.
grep "error" app.log
grep -i "hello" file.txt
Common Option
-i- Ignores differences between uppercase and lowercase letters.
findFind Files and Directories
Searches for files and directories based on conditions such as name, type, or modification time.
find . -name "*.html"
find . -type f
find . -type d
sortSort Text
Sorts lines of text alphabetically or according to other rules.
sort names.txt
wcCount
Counts lines, words, and bytes in files or input.
wc file.txt
wc -l file.txt
The -l option counts lines.
headShow the Beginning
Displays the beginning of a file. By default, it shows the first 10 lines.
head file.txt
head -n 20 file.txt
tailShow the End
Displays the end of a file. It is particularly useful for inspecting log files.
tail app.log
tail -n 20 app.log
Follow a Log
tail -f app.log
The -f option continues displaying new lines as they are added to the file.
psProcesses
Displays information about running processes.
ps
ps aux
killSend a Signal to a Process
Sends a signal to a process, commonly to request that it stop.
kill 1234
The number is the process ID (PID).
chmodChange Permissions
Changes the permissions associated with a file or directory.
chmod +x script.sh
This example adds execute permission to the script.
envEnvironment
Displays environment variables or runs a command with modified environment variables.
env
echo "$HOME"
echo "$PATH"
exportSet Environment Variables
Creates or modifies an environment variable for the current shell and processes started from it.
export NAME="yourname"
echo "$NAME"
commandRun a Command
Can be used to check or execute commands using shell built-in functionality.
command -v git
command -v python
3. Pipes and Redirection
The command line becomes significantly more powerful when commands can be combined and their input or output redirected.
Output Redirection: >
Sends standard output to a file and replaces the file's existing contents.
echo "Hello" > output.txt
Append Output: >>
Sends standard output to a file and appends to its existing contents.
echo "Another line" >> output.txt
Pipe: |
Sends the output of one command directly into another command.
ls -la | grep ".html"
Here, ls -la produces output and grep searches that output.
Combine Commands
cat names.txt | sort | uniq
The output flows from one command into the next.
4. Useful Shortcuts
- Ctrl + C
- Interrupts the currently running command.
- Ctrl + L
- Clears the visible terminal screen in many shells.
- Ctrl + D
- Sends EOF (end-of-file) to the shell or current program.
- ↑ / ↓
- Navigate through command history.
- Tab
- Autocompletes commands, paths, and file names when supported by the shell.
5. Real Workflow Example
A simple workflow for creating a new project directory and README file:
# Check where you are
pwd
# Navigate to your Projects directory
cd ~/Projects
# Create a new project directory
mkdir my-app
# Enter the project directory
cd my-app
# Create a README file
echo "# My App" > README.md
# Read the README file
cat README.md
# List the project files
ls -la
After these commands, the project contains a
README.md file inside the my-app
directory.