Programming in Unix Environment: Using the Shell

Command Line Structure

In a Unix shell, commands are typically executed by pressing Enter. However, there are different ways to structure and control the execution of commands:

  • Command Terminators:
    • A command usually ends with a newline but can also be terminated with a semicolon (;).
    • Parentheses () can be used to group commands and execute them in a subshell.
    • The ampersand (&) allows a command to run in the background, letting the user continue with other tasks.
  • Redirection and Piping:
    • The pipe (|) allows passing the output of one command as input to another.
    • The tee command captures output from a pipeline and writes it both to a file and to the standard output.

Examples:

# Grouping commands using parentheses and piping output to wc (word count)
$ (date ; who) | wc

# Capturing output in a file and continuing the pipeline
$ (who ; date) | tee output.txt | wc

# Running a long-running command in the background
$ long-running-command &

# Sleeping for 5 seconds before executing date
$ sleep 5 ; date

# Running a command in the background while executing another immediately
$ (sleep 5 ; date) & who

In the last example, who executes immediately, while (sleep 5 ; date) & waits for 5 seconds before printing the current date in the background.

Metacharacters in the Shell

Metacharacters have special meanings in Unix shells. To use them literally, enclose them in single quotes (').

Example:

$ echo '**'  # Prints ** instead of interpreting * as a wildcard

Other common metacharacters include:

  • * (Wildcard for multiple characters)
  • ? (Wildcard for a single character)
  • {} (Brace expansion)
  • [] (Character class matching)
  • $ (Variable substitution)
  • > and < (Redirection operators)
  • | (Pipe operator)

Escaping Metacharacters

If you need to use a metacharacter without its special meaning, escape it using a backslash (\) or enclose it in single quotes:

$ echo \$HOME    # Prints the string "$HOME" instead of expanding it to the home directory
$ echo 'Hello > World'  # Prints "Hello > World" instead of treating '>' as a redirection operator

Additional Notes

  • The nohup command allows a process to continue running after the user logs out.
  • Job control commands like fg, bg, and jobs help manage background processes.
  • Using &> redirects both standard output and standard error to a file.

Example of nohup:

$ nohup long-running-command &  # Keeps running even if the session is closed

UNIX File System: An Overview

A file in UNIX is essentially a collection of data, whether it be text, binary, or structured information. The UNIX file system provides various commands for examining and managing files. This post covers some fundamental commands used to inspect and navigate files efficiently.

Viewing File Content with od

The od (octal dump) command displays a file’s contents in different formats, which is useful for inspecting non-text files. Some commonly used options include:

  • -c: Interprets bytes as characters.
  • -b: Prints bytes as octal numbers.
  • No option: Dumps the file in 16-bit words (default).

Example:

$ od -c example.txt   # Show file content as characters
$ od -b example.txt   # Show file content in octal format
$ od example.txt      # Default output (16-bit words)

Identifying File Types with file

The file command determines a file’s type by inspecting its contents rather than relying on extensions.

Example:

$ file example.txt
example.txt: ASCII text
$ file script.sh
script.sh: Bourne-Again shell script, UTF-8 Unicode text
$ file binary_file
binary_file: ELF 64-bit LSB executable, x86-64

Checking Disk Usage with du

The du (disk usage) command reports the disk space used by files and directories.

  • du: Shows disk usage of directories.
  • du -a: Includes files along with directories.
  • du -h: Displays sizes in a human-readable format.
  • du -a | grep filename: Filters output for a specific file.

Example:

$ du             # Show disk usage of directories
$ du -a          # Show disk usage of all files and directories
$ du -h          # Display sizes in human-readable format (e.g., KB, MB, GB)
$ du -a | grep example.txt  # Search for a specific file's usage

Understanding UNIX Directories

A UNIX directory consists of 16-byte chunks:

  • The first two bytes point to the administrative information.
  • The last 14 bytes contain the file name, padded with ASCII null characters (NUL).

Understanding this structure helps in low-level file system debugging and development.

Finding Files with find

The find command searches for files based on criteria like name, type, size, and modification time.

Basic usage:

$ find . -name "example.txt"    # Find a file named 'example.txt' in the current directory
$ find /home -type f -size +10M  # Find files larger than 10MB in /home
$ find /var/log -mtime -7         # Find files modified in the last 7 days in /var/log

Additional Useful Commands

  • ls -l: Lists files with detailed information.
  • stat filename: Displays detailed file metadata.
  • df -h: Shows available disk space in a human-readable format.

With these commands, managing and analyzing files in UNIX becomes efficient and insightful. Mastering them will help streamline system operations and troubleshooting.

Programming in a UNIX Environment: Essential Commands

Understanding Basic UNIX Commands

UNIX provides a powerful command-line environment where users interact with the system via a shell. Here, we explore some fundamental commands and concepts that every UNIX user should know.

User Identification Commands

who am i

This command displays the current user’s name along with the terminal (TTY), date, and time of login:

$ who am i
john_doe    tty1    Mar 19 10:15

whoami

This command prints only the username of the currently logged-in user:

$ whoami
john_doe

Directory Navigation

In UNIX systems:

  • The parent directory is denoted by ..
  • The current directory is denoted by .

Removing a Directory

  • rmdir <directory_name> removes an empty directory. To remove non-empty directories, use rm -r <directory_name>.

About the Shell

The shell is an ordinary program that interprets commands and executes them. It processes wildcards (like * and ?) before passing arguments to commands.

Using Wildcards in Commands

Wildcards allow flexible pattern matching in filenames.

cat Command and Wildcards

  • cat c* prints the contents of all files starting with ‘c’.
  • The * wildcard represents any string of characters.

echo Command

The echo command prints arguments to the terminal:

$ echo Hello, UNIX!
Hello, UNIX!

Wildcards with echo:

$ echo ch*
changelog checklists chapter1.txt

$ echo *
file1.txt file2.txt script.sh test.c

rm (Remove Files)

  • rm * deletes all files in the current directory (use with caution!).
  • rm *.txt deletes all .txt files.
  • rm te[a-z]* removes all files starting with te followed by a lowercase letter.

Advanced Wildcard Usage

The [ ] brackets specify a range of characters:

$ pr r[1234]*

Prints all files starting with ‘r’ followed by 1, 2, 3, or 4.

The ? wildcard matches exactly one character:

$ ls ?.txt

Lists files that have a single-character name followed by .txt.

Input and Output Redirection

UNIX allows redirecting command outputs using > and <.

Redirecting Output

  • ls > filelist saves the list of files into filelist.
  • cat file1.c file2.c > file3.c merges file1.c and file2.c into file3.c.
  • cat file4.c >> file3.c appends file4.c to file3.c.

Redirecting Input

  • pr -3 < filelist prints the contents of filelist in three-column format.
  • grep main < source.c searches for occurrences of ‘main’ in source.c.

Conclusion

Understanding these essential UNIX commands enhances productivity and efficiency when working in a UNIX-based environment. Wildcards, redirection, and basic command utilities provide a powerful toolkit for managing files, directories, and data. Master these, and you’ll navigate UNIX with ease!

Programming in the UNIX Environment: Essential Commands (ed, cat, ls, pr)

The UNIX environment provides a vast number of commands to help users manage files, edit text, and organize output efficiently. In this post, we will explore four fundamental commands: ed, cat, ls, and pr.

The ed Command: Line Editor

The ed command is a simple, line-based text editor that allows you to create and modify files.

Example Usage:

$ ed file1
no such file or directory

If the file does not exist, UNIX will display an error message. You can then create and edit it using the following steps:

  1. Type a to enter append mode.
  2. Enter the text you want to add.
  3. Type . (a single period) on a new line to indicate that input is finished.
  4. Save the file using w file1.
  5. Exit ed using q.
$ ed file1
a
Enter the text
.
w file1
q

The cat Command: Viewing File Contents

The cat command is used to display the content of a file or concatenate multiple files.

Example Usage:

$ cat filename

To view multiple files together:

$ cat file1 file2

The pr Command: Formatting File Output

The pr command paginates the output of a file, making it easier to read.

Example Usage:

$ pr filename

To display multiple files side by side in parallel columns:

$ pr -m file1 file2

To format output into multiple columns:

$ pr -3 filename

The ls Command: Listing Files and Directories

The ls command is used to list files in the current directory.

Common Options:

  • ls – Lists all files in the directory.
  • ls DIRNAME – Lists files within a specified directory.
  • ls * – Lists all files, including those in subdirectories.
  • ls -t – Lists files sorted by modification time (newest first).
  • ls -l – Displays detailed information about each file.
  • ls -lt – Combines -l and -t to list files with details, sorted by most recent first.
  • ls -u – Shows files sorted by last access time.
  • ls -ult – Lists files by last accessed time with details.

Example Usage:

$ ls
$ ls -l
$ ls -lt
$ ls -u
$ ls -ult

Additional Notes

  • The ls command can be combined with other UNIX utilities like grep to filter specific results.
  • Modern alternatives to ed include vi, nano, and vim for a more interactive text editing experience.
  • The pr command can be useful when preparing text for printing.

These commands provide a foundation for working in the UNIX environment. Mastering them will help improve efficiency when managing files and navigating the system.