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.