NOS 110 – OPERATING SYSTEM CONCEPTS
Intro to Basic Linux Commands
Documentation
REFERENCES
Ubuntu provides access to various tutorials (FREE). Browse the list or search by topic at this website.
[URL: https://tutorials.ubuntu.com/]
[URL: https://tutorials.ubuntu.com/tutorial/command-line-for-beginners#0]
The Linux command line for beginners tutorial is really worth a look. If nothing else, it provides some
extra practice with command line commands covered in the Basic Linux Commands Lab.
NOS 110 – OPERATING SYSTEM CONCEPTS
Introduction to Linux Command-Line Interface (CLI)
This is a documentation file meant for reference as you go through the actual lab.
Today, most people are familiar with Graphical User Interfaces (GUIs). However, it was not until 1983
with Apple’s release of Lisa, that the first GUI was commercially available. And it was not until 1990,
with the release of Windows 3.0, that GUIs became the standard user interface for personal computers.
Prior to this, all computers used a command-line interface (CLI).
While there are many GUIs available for the Linux system, Linux is inherently designed as a command-
line interface. Power Users, including System Administrators at large corporations, rely heavily on the
CLI of Linux or UNIX. While GUIs are more user-friendly, once mastered CLIs are, arguably, more
powerful.
This document will introduce you to some of the basic Linux commands, after which you will open the
Lab document and perform the lab. I suggest you use this document as a resource when you are
performing the labs. There are many online resources that you can use also. Keep in mind, however,
that NOS-110 is a “survey course.” We are only spending two weeks on Linux, many of the resources on
line are for people that want a more in-depth understanding of Linux, such as you will receive if you take
the Linux course here at Wake Tech.
All commands in Linux are entered through the Terminal application. As part of a previous lab you
should have learned how to open the Terminal application and the Terminal application icon should be
located on your Dock (Favorites) for quick access. You should open and refer to that application as you
read though this documentation. It’s possible to wind up in different environments within the Terminal
application. When you see the dollars sign ($) this is the command prompt, which indicates you are in
the native environment.
There are dozens of commands in the Linux system, however, there are thousands of options. We use
options to modify how a command behaves. Options are usually one character (although not always)
and preceded by a dash (minus sign). There must be at least one space between the command and any
option you might choose to use. You can have more than one space.
Linux command reporting is, generally, “no news is good news.” Because of that, Linux will not usually
tell you when a command was successful. In several of the book’s examples, the authors suggest you
check the results of your command using various techniques. I frequently do that, myself, and I indicate
how to do it in some of the examples below.
RECORDING YOUR LAB WORK
After reading this document, the lab exercise that follows will have you use several of these commands.
However, if you want to have something to follow along with while reading this documentation, open a
Terminal window and issue these two commands:
touch practice This will create an empty file called practice.
echo “This is my practice file” > practice This will re-direct the output of the echo command
to the practice file. (We will discuss echo and
redirection below.)
NOS 110 – OPERATING SYSTEM CONCEPTS
Navigating the Linux File System
Do not uses spaces in your Linux file names. While you may be able to get them to work, more often
than not, it will cause problems. Use underscores in place of spaces. For instance, instead of naming a
file letter to boss.txt name it letter_to_boss.txt.
As with Windows, Linux keeps track of its files via a file system. Whereas, Windows typically uses the
FAT or NTFS file system, Linux typically uses the ext3, ext4, or NTFS-3G (for Windows compatibility) file
systems.
When you open a Terminal application, by default you are positioned in what is called the Home
directory. This is the directory assigned by the System Administrator to each user when that user’s ID
gets created.
Whatever directory you are positioned in is your current directory or working directory. These terms
mean the same thing. When you first login, your home directory and your working directory are the
same. For all intents and purposes, your home directory never changes, but your current directory does
change.
The Linux file system is hierarchical, as are most current file systems. The highest level of the file system
is called root and it is designated by the slash (/), lower levels in the file system, called directories are
also separated by the slash (/). So, for example, you might have a file in the following location:
/home/jdoe/Desktop/classes/NOS110/assignments/lab1
This would indicate that the file lab1 is in the assignments directory, which is a sub-directory of NOS110,
which is a sub-directory of classes, which is a sub-directory of Desktop, which is a sub-directory of jdoe,
which is a sub-directory of home, which is a sub-directory of root. The full specification of the file is known
as the path.
Changing your Working Directory
Linux users are frequently moving from directory to directory. Because we are dealing with a command-
line-interface, it is easy to “get lost.” You can use the pwd command (print working directory) to
determine what your current directory is.
pwd displays current directory (path)
/home/jdoe (output of pwd command)
In Windows, we navigate through the file system using Windows Explorer. Using Terminal in Linux, we
navigate with the cd (change directory) command. In Linux, unless you specify the full path of the file,
known as the absolute path, all commands in Linux are relative to the current directory, known as the
relative path. So, going back to the example at the beginning of this section:
/home/jdoe/Desktop/classes/NOS110/assignments/lab1
NOS 110 – OPERATING SYSTEM CONCEPTS
Example 1.1
Assume your current directory is: /home/jdoe and you wanted to position yourself in the Desktop
directory, you could enter either of the following commands:
cd Desktop (this is a relative path designation)
cd /home/jdoe/Desktop (this is an absolute path designation)
Both commands would accomplish the same thing, positioning you in the Desktop directory.
Example 1.2
Similarly, assume your current directory is: /home/jdoe and you wanted to position yourself in the
Assignments directory, you could enter either of the following commands:
cd Desktop/classes/NOS110/assignments (relative path)
cd /home/jdoe//Desktop/classes/NOS110/assignments (absolute path)
Both commands would accomplish the same thing, positioning you in the assignments directory. Notice
in both examples the absolute path starts with the root designator /. Absolute paths always start with
the root designator.
Example 2.1
There is a special symbol in Linux that represents the parent directory. Again, assume the previous:
/home/jdoe/Desktop/classes/NOS110/assignments/lab1. And, assume your current directory is
Desktop and you want to position yourself in the jdoe directory. You could issue either of the following
commands.
cd ../ (relative path, where ../ represents the parent directory)
cd /home/jdoe (absolute path)
Example 2.2
Similarly, if you were positioned in Desktop and you want to move your position to the home directory
you could issue either of these commands
cd ../../ (relative path)
cd /home (absolute path)
NOS 110 – OPERATING SYSTEM CONCEPTS
Listing the contents of a Directory
In Windows, if you want to know what files and folders reside in a particular directory (remember
the words folder and directory mean the same thing) you would open Windows Explorer (named
File Explorer in Windows 8) and browse through your directory structure.
In Linux, the ls (depending on your font this could like the word “is” however it is actually the
letter “l” (lower case L) followed by the letter “s”) command will give you a list of files that are in
the current directory. If you follow the command with various options, you will change how the
command works.
ls lists all non-hidden files in current directory
ls -a lists all hidden and non-hidden files in current directory
ls -l lists additional information about non-hidden files in current directory
ls -a -l lists additional information about all hidden and non-hidden files in
current directory
ls -al You can usually, but not always, combine options using just one dash.
Listing the Contents of a Text File
The cat utility displays the contents of a text file. On a large file, txt will scroll off screen.
The more utility displays the contents of a text file and pauses after each screen.
o At each pause you can either press enter for one more line of data, or the spacebar for
another full screen of data.
General format: cat filename more filename
cat practice will display the contents of the practice file, if there are more lines in
the file than will fit on one screen, the first lines will scroll off.
more practice will display the contents of the practice file, if there are more lines in
the file than will fit on one screen (there aren’t in this case), the
screen will pause when it is full.
Clearing the Terminal Screen
A Terminal screen can become cluttered. The clear command will clear the screen for you.
clear clear terminal screen
Alias command
If you are used to working on Windows, you might be used to typing cls to clear your screen. You
can create an alias so you can use the same command on Linux.
alias cls=”clear” clear terminal screen
NOS 110 – OPERATING SYSTEM CONCEPTS
Copying one file to another
The cp (copy) command will allow you to copy one file to another.
If the destination file exists cp will overwrite it without warning.
Before modifying a file it’s a good idea to create a backup copy.
One technique is to include the date in the new file name.
General format: cp source-file destination-file
cp practice practice.101714 copy practice file to new filename with date
ls verify that both files exist
Deleting a File
The rm (remove) command will delete a file from a directory.
This command will not ask you if you are sure. If the file exists, it will delete it.
General format: rm file-name
rm practice.101714 remove new file
ls show new file gone
cat practice_101714 also show new file gone
Changing the name of a file
The mv (move) utility will allow you to rename a file without making a copy.
mv can also be used to move the file from one directory to another, or one drive to another. At
this point we will just consider the renaming capability of the command.
General format: mv old-filename new-filename
ls show current contents of directory
mv practice practice_exercise rename file
ls show file is renamed
Who is Using the System
Several Utilities can help you find out who is currently Logged In to the system.
o who: one of the older utilities, gives a short list of user names, the terminal they are
associated with (beyond the scope of this class) and the time they logged in.
who
ken :0 2014-10-17 19:15 (output of who command)
ken pts/0 2014-10-17 1952 (output of who command)
o System administrators sometimes have more than one Userid and are often logged on
to more than one system at a time. As such, it’s easy to get “lost,” forget what ID is
NOS 110 – OPERATING SYSTEM CONCEPTS
logged on to which system. The who am i will display the name of the user currently
logged in to the session.
who am i
ken (output of who am i command)
Displaying Date and Time
The date command will display the date and time
date
Fri Dec 15 19:33:35 EST 2000 (output of date command)
Displaying Calendar
The cal command will display the current month
You can specify a year to display a whole year’s worth of months
You can specify a month and year to display just a specific month
cal displays current month
cal 2000 displays calendar for year 2000
cal 3 2000 displays calendar for March 2000
You might have noticed that there is no dash (minus sign) before the characters following the
command cal, as there were in the ls example earlier. That’s because these are parameters not
options. Parameters are beyond the scope of this class.
Displaying Text on Terminal
A command which may not be appreciated when running from a command prompt is the echo
command. This command will display text on the terminal. While this may at first seem like a
superficial, unnecessary command, it plays an important part when you want to display data to the user
from a script. (A script is similar to a program.)
The echo command will display text on the terminal.
General format: echo text
echo “The echo cmd will display the text you type onto the terminal”
The echo cmd will display the text you type onto the terminal (output of echo cmd)
NOS 110 – OPERATING SYSTEM CONCEPTS
Brief introduction to Piping and Redirection
No discussion of a command-line interface, no matter how cursory the discussion, would be complete
without at least mentioning the ability to pipe the output of one command to the input of another
command and to redirect the output of one command to a file. These are incredibly powerful
features which cannot be fully appreciated without a more in-depth discussion than this course
allows.
Notice that while piping and redirection sound similar they are not interchangeable. With a pipe, you
take the output of one command and send it to another command. With redirection, you take the
output of one command and sent it to a file.
You can use pipes to reroute output from a particular command to another command.
Often used to pipe output of commands to the more utility.
If you did not modify the settings of your terminal window, when you issued the command: cal
2000 in a previous example, you may have noticed that the output of this command did not fit
on one screen. It scrolled off. In early releases of UNIX, the scrolled lines would be lost to you
because you could not scroll the Terminal window. Today, most releases have the ability to
scroll the window. However, there is typically a limit, defined by the buffer size of the window,
as to how many lines are retained before they are lost. With piping you can take the output of
the cal command and pipe it (send it) to another command, for instance the more command.
Recall from an earlier example that the more command will pause the screen when it becomes
full. (The pipe symbol is the vertical bar (|) – typically the character over the backslash key on
most keyboards.)
cal 2000 | more When the screen pauses, press:
enter = to move forward 1 line
space = to move forward 1 screen
You can use redirection to reroute output from a particular command to another command. If
we go back to the example at the beginning of this document:
echo “This is my practice file” > practice
The output of the echo command (which normally displays on the monitor) is redirected (the
greater than symbol is one of the redirection symbols) to the practice file. When you issued the
touch command, you created a null file. When you issued the echo command with redirection
you initialized the practice file with one line of data.