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!

Unknown's avatar

Author: Abhilash

Hi, Iโ€™m Abhilash! A seasoned web developer with 15 years of experience specializing in Ruby and Ruby on Rails. Since 2010, Iโ€™ve built scalable, robust web applications and worked with frameworks like Angular, Sinatra, Laravel, Node.js, Vue and React. Passionate about clean, maintainable code and continuous learning, I share insights, tutorials, and experiences here. Letโ€™s explore the ever-evolving world of web development together!

Leave a comment