Basic Unix Commands Everyone Must Know

Karishma Kochar

Karishma Kochar

Senior AWS Corporate Trainer

Unix is a powerful, multiuser operating system known for its stability and flexibility, widely used in servers and workstations. Understanding Unix commands is essential for navigating and managing files and processes efficiently. This guide will cover the fundamentals of Unix, including basic commands, text editors like Vi and Nano, and an introduction to shell scripting, empowering users to harness the full potential of the Unix environment.

Introduction to Unix.

Unix is a powerful, multiuser, multitasking operating system originally developed in the 1960s and 1970s at Bell Labs by Ken Thompson, Dennis Ritchie, and others. It has influenced many modern operating systems and is widely used in server environments, embedded systems, and academia.

Basic Unix Commands Everyone Must Know | NovelVista Learning Solutions

Key Features of Unix

  1. Multiuser Capability: Multiple users can access and use the system simultaneously without interfering with each other.
  2. Multitasking: Unix can execute multiple tasks (processes) at the same time, allowing efficient resource utilization.
  3. Portability: Unix is designed to be portable across different hardware platforms, leading to various versions and derivatives (e.g., Linux, BSD).
  4. Hierarchical File System: Unix uses a hierarchical file system structure, making it easy to organize and manage files.
  5. Command-Line Interface: Unix primarily operates through a command-line interface (CLI), allowing users to execute commands and scripts efficiently.
  6. Powerful Scripting and Programming Capabilities: Users can write scripts using shell languages (like Bash) for automation and task management.
  7. Security and Permissions: Unix provides robust security features, including user authentication and file permissions to protect data.
 Features of Unix

What are Unix Commands?

A Unix command is a specific instruction that a user can input into the Unix command-line interface (CLI) to perform a task or execute a program. These commands allow users to interact with the operating system, manage files, control processes, and perform various system administration tasks. Unix commands are essential tools for interacting with the Unix operating system, which is widely used in servers and workstations. These commands enable users to perform a variety of tasks, including managing files, executing programs, and controlling system processes. By mastering basic Unix commands, users can navigate the system efficiently and automate repetitive tasks, leading to increased productivity. The command-line interface is a powerful aspect of Unix, allowing users to execute commands directly, and providing greater control over their computing environment.

Some Basic Unix Commands

  • touch: It creates a new file or upgrades its timestamp.
  • cat: This command is used to concatenate files and display them on stdout.
  • copy: This command is used to copy files.
  • mv: This command is used to rename files or move files.
  • rm: This command is used to remove directories and files.
  • mkdir: It creates a directory.
  • rmdir: It removes a directory.
  • cd: It changes the directory.
  • pwd: It prints the current working directory.
  • clear: It clears the terminal screen.
  • ls: It lists the contents of the directories.
  • su: It modifies user-id and becomes a super or root user.
  • sudo: It runs the command as other super-users or users.
  • chmod (short for "change mode"): is a Unix command used to change the file system permissions of files and directories. It allows users to define who can read, write, or execute a file.
  • chown (short for "change owner"): is a Unix command used to change the ownership of files and directories. This command allows users to specify a new owner and, optionally, a new group for the specified file or directory.
  • Wget: is a command-line utility in Unix and Unix-like systems used to download files from the web. It supports HTTP, HTTPS, and FTP protocols and can handle both single files and recursive downloads of entire directories.
  • curl (short for "Client URL"): is a command-line tool used in Unix and Unix-like systems to transfer data to or from a server using various protocols, including HTTP, HTTPS, FTP, and more. It's widely used for testing APIs, downloading files, and interacting with web services.
  • zip: is a command-line utility in Unix and Unix-like systems used to compress files and directories into a single ZIP archive. This tool is helpful for reducing file sizes, bundling multiple files together, and making file transfers more efficient.
  • unzip: is a command-line utility in Unix and Unix-like systems used to extract files from ZIP archives. It allows you to unpack compressed files and directories, making the contents accessible for use.
  • The apt command: is a package management tool used in Debian-based Linux distributions, such as Ubuntu. It allows users to install, update, and remove software packages from the command line.
  • The apt update command: is used in Debian-based Linux distributions, such as Ubuntu, to refresh the package index. This command retrieves information about the latest available packages and their versions from the repositories configured on your system.

Vi Editor with Commands

The vi editor is a powerful text editor available on Unix and Unix-like systems. It's widely used for editing configuration files, scripts, and other text files. The vi editor has two primary modes: Normal mode (for navigation and commands) and Insert mode (for text entry).

vi editor

Starting vi

To open a file in vi, use:

vi filename

Modes in vi

  • Normal Mode: The default mode for navigating and issuing commands.
  • Insert Mode: Used for entering text. Enter Insert mode from Normal mode by pressing i (insert before the cursor), I (insert at the beginning of the line), a (append after the cursor), or A (append at the end of the line).
  • Command-Line Mode: Used for saving, exiting, and other commands. Enter this mode from Normal mode by pressing :.

Basic Navigation in Normal Mode

  • Arrow Keys: Move the cursor up, down, left, or right.
  • h: Move left.
  • j: Move down.
  • k: Move up.
  • l: Move right.
  • 0: Move to the beginning of the line.
  • $: Move to the end of the line.
  • G: Move to the end of the file.
  • nG: Move to line number n.

Basic Editing Commands

  • i: Switch to Insert mode.
  • a: Switch to Insert mode and append after the cursor.
  • o: Open a new line below the current line and enter Insert mode.
  • O: Open a new line above the current line and enter Insert mode.
  • Esc: Exit Insert mode and return to Normal mode.

Saving and Exiting

In Command-Line mode (after pressing :):

  • : Save the file.
  • : Quit the editor.
  • or ZZ: Save and quit.
  • !: Quit without saving changes.

Deleting and Undoing

  • x: Delete the character under the cursor.
  • dw: Delete the word from the cursor.
  • dd: Delete the current line.
  • u: Undo the last action.
  • Ctrl + r: Redo the last undone action.

Searching and Replacing

  • /search-term: Search for search-term in the file.
  • ?search-term: Search backward for search-term.
  • n: Go to the next search result.
  • N: Go to the previous search result.
  • :%s/old/new/g: Replace all occurrences of old with new in the file.

Nano Text Editor

nano text editor

The nano text editor is a simple, user-friendly text editor available on Unix and Unix-like systems. It's often preferred by beginners due to its straightforward interface and ease of use compared to more complex editors like vi.

Basic Commands

Starting nano

To open a file in nano, use:

nano filename

If the file does not exist, nano will create a new file with that name.

Basic Navigation

  • Arrow Keys: Move the cursor up, down, left, or right.
  • Page Up/Page Down: Scroll up or down by a page.
  • Ctl + A: Move to the beginning of the line.
  • Ctl + E: Move to the end of the line.

Editing Commands

  • Type: Just start typing to insert text.
  • Ctl + K: Cut the current line (also known as "delete").
  • Ctl + U: Paste the cut line.
  • Ctl + ^: Start marking text (select text).

Saving and Exiting

  • Ctl + O: Save the file. You'll be prompted to confirm the filename.
  • Ctl + X: Exit nano. If you have unsaved changes, it will prompt you to save.

Searching and Replacing

  • Ctl + W: Open the search prompt. Enter the text you want to find and press Enter.
  • Ctl + \: Open the replace prompt. Enter the text to find and the text to replace it with.

Additional Features

  • Ctl + G: Open the help menu to see a list of commands and shortcuts.
  • Ctl + J: Justify the current paragraph.
  • Ctl + C: Display the current cursor position (line and column number).
  • Ctl + T: Invoke the spell checker (if installed).

Shell Scripts in Unix

shell scripts

Shell scripts in Unix are text files that contain a series of commands executed by the shell. They are used to automate tasks, manage system operations, and simplify complex sequences of commands. Shell scripts can significantly enhance productivity by allowing users to run multiple commands with a single script.

Basic Structure of a Shell Script

#!/bin/bash
echo "Hello, World!"

Running the Shell Script

./myscript.sh

Understanding Unix commands is crucial for anyone working in technology, as they form the foundation for more advanced operations and scripting. Unix is known for its robustness and flexibility, making it a preferred choice for developers, system administrators, and power users. With the right knowledge, individuals can leverage these commands to troubleshoot issues, manage system resources, and streamline their workflows. As users become more familiar with Unix commands, they will find that their efficiency and effectiveness in utilizing the system increase significantly.