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, userm -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 *.txtdeletes all.txtfiles.rm te[a-z]*removes all files starting withtefollowed 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 > filelistsaves the list of files intofilelist.cat file1.c file2.c > file3.cmergesfile1.candfile2.cintofile3.c.cat file4.c >> file3.cappendsfile4.ctofile3.c.
Redirecting Input
pr -3 < filelistprints the contents offilelistin three-column format.grep main < source.csearches for occurrences of ‘main’ insource.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!