Search Knowledge

© 2026 LIBREUNI PROJECT

Operating Systems Internals / System Interfaces & Commands

Unix Shell: The Command Line Interface

The Unix Shell

To a casual user, a computer is a collection of graphical icons. To a systems programmer or administrator, the computer is managed through a Shell. The shell is an interactive command-line interpreter that reads user commands from the keyboard and passes them to the operating system kernel for execution. On most Unix-like systems (Linux, macOS, BSD), the default shell is Bash (Bourne Again Shell) or Zsh (Z Shell).

In Unix-like systems, the file system is organized as a single tree structure. The root of this tree is designated by a single forward slash /.

  • pwd (Print Working Directory): Displays the absolute path of the current active folder.
  • ls (List): Displays the contents of the current directory.
    • ls -l: Renders a detailed list format displaying file sizes, ownership, and permission metadata.
    • ls -a: Lists all files, including hidden system files (which begin with a dot, e.g., .bashrc).
  • cd (Change Directory): Shifts the shell’s active folder context.
    • cd Documents: Navigates into a sub-folder.
    • cd ..: Moves the context up one level to the parent directory.
    • cd ~: Returns directly to the user’s home directory.
  • mkdir (Make Directory): Creates a new subdirectory.

The following shell session demonstrates navigating directories and inspecting folder contents:

# Example: Navigating and listing directory structures
$ pwd
/home/student
$ mkdir sandbox
$ cd sandbox
$ pwd
/home/student/sandbox
$ ls -la
total 8
drwxr-xr-x  2 student student 4096 May 21 12:00 .
drwxr-xr-x 15 student student 4096 May 21 12:00 ..

File Manipulation

Command-line utilities allow swift, scriptable creation, copying, moving, and deletion of files:

  • touch filename.txt: Creates a new empty file, or updates the access and modification timestamps of an existing file.
  • cp source destination (Copy): Duplicates files.
    • cp -r folder1 folder2: Recursively duplicates directories and their inner contents.
  • mv old_name new_name (Move): Renames or relocates files and folders.
  • rm filename (Remove): Deletes files.
    • WARNING: The command line does not have a recycle bin. Once deleted, file retrieval is difficult.
    • rm -rf folder: Forcefully and recursively deletes an entire directory tree.

The following sequence shows standard file manipulation tasks:

# Example: Creating, copying, and renaming files in the shell
$ touch report.log
$ cp report.log backup_report.log
$ mv report.log archive_report.log
$ ls -l
total 0
-rw-r--r-- 1 student student 0 May 21 12:00 archive_report.log
-rw-r--r-- 1 student student 0 May 21 12:00 backup_report.log
$ rm backup_report.log

Reading and Searching Files

System administrators read and parse file contents directly via the terminal:

  • cat file (Catenate): Reads and prints the entire contents of a file to standard output.
  • less file: Opens a scrollable interface to inspect large files (press q to exit).
  • head -n 10 file: Displays the initial 10 lines of a file.
  • tail -n 10 file: Displays the terminal 10 lines of a file, useful for inspecting appended system logs.
  • grep "term" file: Searches for a matching regular expression pattern within a file.
    • grep -r "error" /var/log: Recursively searches for the string “error” inside all log files under /var/log.

The following session shows how to read file contents and search for error patterns:

# Example: View the tail of a syslog file and search for error tags
$ tail -n 3 /var/log/syslog
May 21 12:00:01 server systemd[1]: Started Periodic Command Scheduler.
May 21 12:05:00 server systemd[1]: Starting Database Backup Service...
May 21 12:05:02 server backup[1024]: ERROR: Connection timed out.
$ grep "ERROR" /var/log/syslog
May 21 12:05:02 server backup[1024]: ERROR: Connection timed out.

The Power of Redirection and Pipes

One of the defining innovations of Unix is the ability to redirect process standard input/output channels and string multiple independent tools together.

Redirection

By default, stdout and stderr are printed directly to the terminal, and stdin is read from the keyboard. The shell can redirect these streams using operators:

  • command > file: Redirects standard output (stdout) to a file, overwriting the file.
  • command >> file: Appends standard output to the end of a file.
  • command 2> file: Redirects standard error (stderr) to a file.

Pipes

The pipe operator | connects the stdout of the command on its left directly to the stdin of the command on its right, allowing developers to chain operations.

# Example: Redirect file listings to a text file, and search using a pipe
$ ls -la /usr/bin > binary_list.txt
$ grep "zip" binary_list.txt | wc -l
4

This sequence records a directory listing to binary_list.txt, searches it for lines containing “zip”, and counts the matches.

Permissions: Who owns what?

In Unix multi-user security environments, every file has an owner user and an associated group. Running ls -l reveals a permission string such as -rwxr-xr--.

  • r (Read): Permission to read file contents or list a directory.
  • w (Write): Permission to modify a file or add/delete files in a directory.
  • x (Execute): Permission to run a file as a program or traverse a directory.

The permission flags are divided into three groups of three: Owner, Group, and Others:

  • chmod 755 script.sh: Updates permissions, making the file readable/executable by everyone, but writable only by the owner.
  • chown user:group file: Modifies the owner user and group of a file.
  • sudo command (Substitute User Do): Executes a command with elevated superuser (root) privileges.
# Example: Checking permissions and making a file executable
$ ls -l script.sh
-rw-r--r-- 1 student student 124 May 21 12:00 script.sh
$ chmod +x script.sh
$ ls -l script.sh
-rwxr-xr-x 1 student student 124 May 21 12:00 script.sh

Process Management from the Shell

The shell provides tools to monitor and manage running programs (processes) within the system:

  • top or htop: Interactive real-time process list displaying CPU, memory, and task performance.
  • ps aux: Displays a detailed static snapshot of all active processes.
  • kill PID: Sends a signal (by default SIGTERM) to terminate a process by its Process ID.
  • killall name: Stops all active processes matching a specific binary name.
# Example: Listing running Python instances and stopping a process
$ ps aux | grep "python"
student  23456  0.5  1.2  45678  12345 pts/0    S    12:00   0:05 python worker.py
$ kill 23456

Exercise: Redirections and Shell Chaining

Evaluate your knowledge of shell operations and stream control in the exercises below:

Which shell operator is used to redirect only the standard error (stderr) stream to a file?

Appending Command Output

# Example: Append the current date and time to the log file
date  system.log

References & Further Reading

For additional guides and historical background on shell development, consult the following sources:

  • Shotts, W. (2019). The Linux Command Line: A Complete Introduction (2nd ed.). No Starch Press. (Comprehensive textbook covering navigation, scripting, and system commands).
  • Kerrisk, M. (2010). The Linux Programming Interface. No Starch Press. (Covering Chapter 2: Basic Concepts and Chapter 5: File I/O stream mechanics).
  • Bash Reference Manual. Free Software Foundation.
  • Zsh Reference Manual. SourceForge project.