Basic UNIX Commands and Beginner Tutorials

Basic UNIX Commands and Concepts Tutorial for Beginners


If you are new to UNIX, our Basic UNIX Commands and Concepts Tutorial for Beginners will provide an introduction to the UNIX commands you need to know if you want to start working with UNIX immediately. Syntax and output examples are provided to help you grasp basic command usage and expected results more quickly.

Before getting started with actual commands, it would be good for you to have a basic understanding of a few key UNIX concepts which are related to the commands and the UNIX operating system environment in general.

Although you may be tempted to skip over these fundamental concepts and jump right to learning some UNIX commands, please take the time to read through this background information first because it will be useful to you later on even if you don't understand the significance and importance of it at this time.

Let's get started...


The UNIX Shell

In a nutshell (pun intended), the UNIX shell is your interface into what is called the UNIX operating system kernel. The kernel interfaces directly with the system's hardware, and manages the UNIX system's shared resources ensuring that each task is allocated the system resources it needs to finish in a timely manner.

The shell is a command line interpreter that translates human-readable commands such as ls, the command used to list a UNIX directory's contents, into a format that the computer understands.

It is important to recognize that the shell is a program, just like a spreadsheet is a program, and therefore different shells can be run on UNIX systems. Even though different shells can be used on a single server (and by the same user!!!), most UNIX command specific operations are the same.

Where there may be a difference between shells is with what are called built-in shell commands. (Built-in commands will be discussed in a subsequent section of this tutorial.) The most common shells are The Korn Shell, The Bourne Again Shell (BASH), The C Shell and The Bourne Shell.

Again, the key takeaway here is that the shell is the operating system component which allows you to effectively communicate (via UNIX commands) with the kernel which is the inner-most or brain and heart of the UNIX operating system.




The UNIX Command Prompt
Okay, the name basically says it all. The UNIX command prompt is the prompt where you type in the UNIX command. How's that for keeping in simple and memorable? =)

As simple as it seems, there are a few things you should know about the UNIX command prompt.

(1) In it's most basic incarnation, the command prompt is typically a "$" (dollar sign) or a "#" (pound sign). This value is stored in a shell variable named "PS1" and can be changed by updating the value:
$ echo $PS1
$
PS1='Hi, enter your command here> '
Hi, enter your command here>
Hi, enter your command here>
Hi, enter your command here> echo $PS1
Hi, enter your command here>

Details: What was done here, to show you the command prompt was redefined, was to hit the "Enter" key on my keyboard a few times without actually typing in a command and then displayed the PS1 shell variable again to show you its new value.

(2) The UNIX command prompt is frequently referred to as the CLI, which stands for command line interface.

(3) Multiple commands can be run on a single command line. If you want to run multiple commands on a single command line, meaning you only have to press the "Enter" key once, you can separate each command with a semi-colon (";"). For example:
$ echo PS1; pwd
PS1
/home/lfl
$

Details: The first command executed displays the value stored in the PS1 shell variable and the second command ("pwd") displays the current working directory.
(4) A single command can be continued across multiple screen lines using the backslash ("\") character. Command continuation is helpful when the command you are typing exceeds the width of your screen. It is also frequently used to make shell scripts more readable.
$ echo \
> PS1
PS1
$

BONUS:
The secondary (command continuation line) prompt is changed by updating the value stored in the PS2 shell environment variable.


What is a UNIX Command?
A UNIX command can take the form of a compiled program, a script or a built-in shell command. A compiled program can be written in any of the numerous low-level programming languages such as C or C++, a script may be a simple shell script or a more advanced Python program, and a built-in shell command is a command which is incorporated into (is part of) a given shell.


UNIX Command Options and Arguments...what's the Difference?
UNIX commands typically have various options and arguments that can be used with the command. A good way to understand the difference is that a command option will influence the behavior of a command whereas a command argument is the object the command operates on.

Let's again use ls, the command used to list a UNIX directory's contents, to clarify this point.

Running ls with no options will only produce a list of the files located in the current working directory:
$ ls
backup1.py backup2.py backup3.py db1.c db2.c db3.c file1 file2 file3 restore1.py restore2.py restore3.py
$
Adding the ls command's -l option causes ls to display the long listing format, which includes file permissions, ownership and file timestamp information:
$ ls -l
total 0
-rw-r--r-- 1 lfl users 0 May 6 15:31 backup1.py
-rw-r--r-- 1 lfl users 0 May 6 15:31 backup2.py
-rw-r--r-- 1 lfl users 0 May 6 15:31 backup3.py
-rw-r--r-- 1 lfl users 0 May 6 15:29 db1.c
-rw-r--r-- 1 lfl users 0 May 6 15:29 db2.c
-rw-r--r-- 1 lfl users 0 May 6 15:29 db3.c
-rw-r--r-- 1 lfl users 0 May 6 15:16 file1
-rw-r--r-- 1 lfl users 0 May 6 15:17 file2
-rw-r--r-- 1 lfl users 0 May 6 15:17 file3
-rw-r--r-- 1 lfl users 0 May 6 15:31 restore1.py
-rw-r--r-- 1 lfl users 0 May 6 15:31 restore2.py
-rw-r--r-- 1 lfl users 0 May 6 15:31 restore3.py
$
If you have a directory containing a large number of files, which is typically the case with UNIX, you can pass in all or a portion of a file's name as a command argument to the ls command to view only the files you are interested in:
$ ls -l fi*
-rw-r--r-- 1 lfl users 0 May 6 15:16 file1
-rw-r--r-- 1 lfl users 0 May 6 15:17 file2
-rw-r--r-- 1 lfl users 0 May 6 15:17 file3
$





Basic UNIX Commands with Examples
In addition to the previously covered ls command, the following command list is a good place for UNIX beginners to start when they want to learn UNIX commands. Although this is a very small subset of the UNIX commands available on a system, you will find yourself using this group frequently so it's good to become comfortable with the basics on how to use them and expected command output or results.

passwd - changes your password. Unless you are using the root account, you likely will not be concerned about using options or an argument with passwd.
$ passwd
Changing password for user lfl.
Changing password for lfl
(current) UNIX password:
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
$
pwd - displays the name of the current, also known as working, directory. Again, no command options of interest for pwd.
$ pwd
/tmp/lfl
$
cd - changes the current directory, and we finally get to use a command argument even though it's possible to run cd without an argument.

Running cd without an argument will take you to your home directory. This is the directory you start in when you first log in on a UNIX system.
$ pwd
/tmp/lfl
$ cd
$ pwd
/home/lfl
$
Now let's provide the absolute path to a directory as a command argument for where we want to go.
$ pwd
/home/lfl
$ cd /tmp
$ pwd
/tmp
$
We started in our home directory and passed /tmp as an argument to the cd command to move to the /tmp system directory. Now we'll return to our home directory.
$ cd
$ pwd
/home/lfl
$
mkdir - makes a new directory, and we'll provide the name of the new directory as the command argument. A common option used with mkdir is -p, which will create parent directories as needed within the path to the new directory.

After creating the new directory, we'll use the cd command to change to the new directory (mydir1) and then use pwd to display the current directory.
$ pwd
/home/lfl
$ mkdir mydir1
$ cd mydir1
$ pwd
/home/lfl/mydir1
$ ls -l
total 0
$
The ls command, with the -l option, was run to show that there are no files in the new directory.

cp - used to copy files and directories. The most frequently used options are -p to preserve permissions, ownership and timestamps and -R to copy directories recursively.
$ pwd
/home/lfl/mydir1
$ ls -l
total 0
$ ls -l /tmp/lfl/fi*
-rw-r--r-- 1 lfl users 111 May 6 16:00 /tmp/lfl/file1
-rw-r--r-- 1 lfl users 0 May 6 15:17 /tmp/lfl/file2
-rw-r--r-- 1 lfl users 0 May 6 15:17 /tmp/lfl/file3
$ cp -p /tmp/lfl/file1 /home/lfl/mydir1
$ ls -l
total 2
-rw-r--r-- 1 lfl users 111 May 6 16:00 file1
In this example, /tmp/lfl/file1 is the source file and /home/lfl/mydir1 is the destination directory.

cat - displays the contents of a file.
$ cat file1
this is LINE 1 in a file named file1
this is LINE 2 in a file named file1
this is LINE 3 in a file named file1
$
grep - used to print lines that match a pattern. Popular options include -i to ignore case and -v to select non-matching lines.

In this first example, the arguments are 2 and file1. 2 is the pattern we are searching for and file1 is where we want to look for it.
$ grep 2 file1
this is LINE 2 in a file named file1
$
When -i is not used, no results are returned when using grep to search for "line" because it is uppercase in the file (LINE).
$ grep line file1
Using the -i option will list all 3 lines in the file since they each contain "LINE" and case is ignored.
$ grep -i line file1
this is LINE 1 in a file named file1
this is LINE 2 in a file named file1
this is LINE 3 in a file named file1
$
Now we'll use the -v option to search for each line that does not contain a 2 in it.
$ grep -v 2 file1
this is LINE 1 in a file named file1
this is LINE 3 in a file named file1
$
less - allows you to page through a single screenful of output at a time.

The file file2 contains 99 total lines in it.
$ less file2
this is line 1
this is line 2
this is line 3

<lines 4 through 49 removed from this example for brevity>

this is line 50
this is line 51
this is line 52
this is line 53

The space bar is pressed to continue the output.

<lines 54 through 95 removed from this example for brevity>

this is line 96
this is line 97
this is line 98
this is line 99
(END)

"q" for quite is pressed to return to the command prompt.

exit - causes the shell to terminate, and logs you out of the UNIX system.




Do you need to learn UNIX, including how to read and write shell scripts...on a REAL SERVER? If you are ready to move past the basics, either of these online courses is a good place to start...

UNIX and Linux Operating System Fundamentals should be taken if you are new to the UNIX and Linux operating system environments or need a refresher on key concepts. This course also contains a very good "Introduction to UNIX Shell Scripting" module.

UNIX Shell Scripting is a good option if you are already comfortable with UNIX or Linux and just need to sharpen your knowledge about shell scripting and the UNIX shell in general.

Both courses include access to an Internet Lab system for completing the course's hands-on exercises, which are used to re-enforce the key concepts presented in the course. Any questions you may have while taking the course are answered by an experienced UNIX technologist.