Welcome to our beginner-friendly Unix training page. Whether you're a developer, system admin, or just curious about Unix-based systems, this guide will walk you through essential concepts and commands.
What is Unix?
Unix is a powerful, multiuser, multitasking operating system used widely in servers, desktops, and mobile devices. Many modern operating systems like Linux and macOS are Unix-based.
Basic Unix Commands
- ls – List directory contents
- cd – Change directory
- pwd – Show current directory
- mkdir – Create new directory
- rm – Remove files or directories
- mv – Rename files or directories
Basic vi Editor Tutorial
The vi editor is a powerful text editor available on most Unix systems. Here's a quick guide to help you get started:
- Open a file:
vi filename.sh - Enter insert mode: Press
ito start typing - Save and exit:
- Press
Escto leave insert mode - Type
:wq(write and quit), then pressEnter
- Press
- Exit without saving: Press
Esc, then type:q! - Basic navigation:
h- move leftl- move rightj- move downk- move up
- Delete a line:
dd - Undo last action:
u - Search text:
/wordthen pressnto repeat
Tip: If you're new, practice in a test file before editing critical scripts.
Unix Shell Scripting
Shell scripts are text files containing Unix commands. They help automate tasks. Start a script with #!/bin/bash and write commands line by line.
Shell Script Tutorial
Let's write a simple script that greets the user and shows the current date.
#!/bin/bash
echo "Welcome to the Unix Shell Script Tutorial"
echo "Today is: $(date)"
echo "Your current directory is: $(pwd)"
Steps to run the script:
- Open a terminal and create a file:
vi myscript.sh - Press
ito enter insert mode and paste the script - Save and exit: Press
Esc, then type:wqand press Enter - Make it executable:
chmod +x myscript.sh - Run it:
./myscript.sh
This is just the beginning. You can use variables, conditionals, loops, and functions in shell scripts to build more powerful tools.
Using if Statements in Bash
The if statement allows your shell script to make decisions based on conditions. Here's the basic syntax:
if [ condition ]
then
# commands to execute if condition is true
fi
Example 1: Check if a Number is Positive
#!/bin/bash
echo "Enter a number:"
read num
if [ $num -gt 0 ]
then
echo "The number is positive."
fi
Example 2: Check if a File Exists
#!/bin/bash
echo "Enter a filename:"
read file
if [ -f "$file" ]
then
echo "File '$file' exists."
else
echo "File '$file' does not exist."
fi
Common Conditions
-f filename– True if file exists and is a regular file-d dirname– True if directory exists$a -eq $b– Equal numbers$a -ne $b– Not equal$a -gt $b– Greater than$a -lt $b– Less than-z "$var"– True if variable is empty
Pro Tips
- Always leave spaces around brackets:
[ $var -gt 10 ] - Use
eliffor multiple conditions - Use
[[ ]]for advanced string comparisons
Practice Exercise
Create a script that checks whether a user is root:
#!/bin/bash
if [ "$USER" == "root" ]
then
echo "You are root!"
else
echo "You are not root."
fi
Using while Loops in Bash
The while loop is used to execute a block of code repeatedly as long as a given condition is true.
Basic Syntax
while [ condition ]
do
# commands to execute
done
Example 1: Countdown from 5
#!/bin/bash
count=5
while [ $count -gt 0 ]
do
echo "Countdown: $count"
count=$((count - 1))
done
echo "Blast off!"
Notes
- Use
$(( ))for arithmetic operations continueskips the rest of the loop and starts next iterationwhile truecreates an infinite loop (until manually broken)
Reading User Input in Bash
In Bash, the read command is used to take input from the user during script execution.
Basic Syntax
read variable_name
The user's input is stored in variable_name.
Example 1: Greeting the User
#!/bin/bash
echo "What is your name?"
read name
echo "Hello, $name! Welcome to Unix Training."
Example 2: Multiple Inputs
#!/bin/bash
echo "Enter your first and last name:"
read first last
echo "Hello, $first $last!"
Example 3: Prompt on the Same Line
#!/bin/bash
read -p "Enter your age: " age
echo "You are $age years old."
Example 4: Silent Input (e.g. passwords)
#!/bin/bash
read -sp "Enter your password: " password
echo
echo "Password received (but not shown for security)."
Tips
-plets you prompt inline-smakes input silent (useful for passwords)- Always validate user input in real scripts for safety
Practice Task
Create a script that asks for a favorite programming language and responds accordingly:
#!/bin/bash
read -p "What is your favorite programming language? " lang
if [ "$lang" == "bash" ]
then
echo "Nice! You're already using it!"
else
echo "$lang is cool too!"
fi
SSH Tutorial: Connecting to a Remote Unix Server
SSH (Secure Shell) is a protocol that allows you to securely connect to a remote Unix or Linux system over a network. It's commonly used by system administrators, developers, and IT professionals.
What You Need
- The IP address or hostname of the remote server
- A valid username on that server
- SSH installed (default on Unix/Linux/macOS)
Connect to a Remote Server
Use this command in your terminal:
ssh username@remote_host
Example:
ssh john@example.com
If it’s your first time connecting, you’ll be asked to confirm the server’s authenticity. Then enter your password when prompted.
Common SSH Options
-p– Specify a custom port (default is 22):ssh -p 2222 user@host-i– Use an identity file (SSH key):ssh -i ~/.ssh/id_rsa user@host-v– Verbose output (for troubleshooting):ssh -v user@host
Useful Tips
- Use
scporrsyncto transfer files over SSH - You can create an SSH config file for easier connections
- Use key-based authentication for better security (no passwords)
Exit the SSH Session
exit
This will close the remote session and return you to your local terminal.