TerminalCommands Basic
TerminalCommands Basic
UNIX was developed in 1969 by Dennis Ritchie and Kevin Thompson at Bell Laboratories.
UNIX was designed to be easily portable to different hardware platforms. One factor that
contributes to the portability of UNIX is that most of the UNIX operating system is written in the
high-level programming language C, which was specifically developed for the purpose of
implementing UNIX. A UNIX operating system consists of a kernel and a set of common utility
programs. The kernel is the core of the operating system. It manages the computer hardware,
provides essential facilities, such as the control of program execution, memory management, a
file system, and mechanisms for exchanging data with attached devices, programs, or over a
network. The utility programs provide user-level commands, such as those needed to create and
edit files.
Since its inception, many different versions of UNIX operating systems have emerged.
Most computer companies have developed their own version of UNIX. Examples include AIX
(IBM), HP-UX (Hewlett Packard), SunOS and Solaris (Sun Microsystems), Ultrix (DEC), and
Xenix (Microsoft Systems). By the early 1990s, when PCs had become powerful enough to run
UNIX-like operating systems, UNIX versions for PCs, such as FreeBSD, NetBSD, OpenBSD,
and Linux began to emerge. The BSD systems are based upon the very significant version of
UNIX developed at the University of California at Berkeley. Linux was created by Finnish
computer science student Linus Torvalds and has become quite popular in a very short period of
time.
The source code for most of UNIX versions for PCs is distributed freely. The Linux operating
system, which is sometimes packaged with proprietary software, is distributed by different
vendors. Popular Linux distributions include Red Hat (Fedora), Caldera, and Suse Linux.
For the most part, the different distributions of Linux are quite similar. However, there are
differences in the configuration files, (those containing system startup parameters), and the tools
used to modify these files. Although configuration differences among versions of Linux, many
of the tools described in this lab (such as the file system characteristics and commands) apply to
other UNIX systems.
Like other modern operating systems, Linux organizes files in a hierarchical tree of
directories. Figure 1 shows a snapshot of a hierarchy of directories in Linux. The directory at the
top of the tree is called the root directory.
Figure 1. The Linux directory hierarchy.
Each file and directory in a Linux file system can be uniquely identified by a pathname.
Pathnames can be absolute or relative. Absolute pathnames start at the root directory. The
absolute pathname of the root directory is a slash (/) character. In the file hierarchy in Figure 4,
the absolute pathname of directory home in the root directory is /home, that of directory user1 in
/home is /home/user1, and the absolute pathname of file data.txt in /home/user1 is
/home/user1/data.txt.
Pathnames that do not begin with a slash are relative pathnames and are interpreted relative to
a current (working) directory. For example, if the current directory is /home, then user1/data.txt
refers to the absolute pathname /home/user1/data.txt.
When using relative pathnames, a single dot (.) denotes the current directory and two dots (..)
denote the parent directory, which is the directory immediately above the current directory. With
the parent directory, it is feasible to identify each file with relative pathnames.
If the current directory is /home/user1, the relative pathname .. refers to directory /home and the
pathname ../user2/data.txt refers to the file home/user2/data.txt (see figure 1).
Each Linux account has a home directory. For regular accounts (accounts other than root) the
home directories are located in /home. So, /home/user1 is the home directory for the user1
account. The home directory of the root account is /root. The home directory is used as the
current directory prompt whenever a new terminal window is created. If you log in as root, then
the current directory is /root.
A more complete list of the top level directories in a Linux file system is shown in Figure 2.
Most of these directories and files can only be modified by root. Linux configuration files are
located in directories /etc, /usr/etc, /var and their subdirectories. Whenever you modify the
configuration of a Linux system, you will work on files in these directories.
Each file and each directory has an owner. A regular user only owns the home directory and
all files that are created by the user. Root is the owner of all other files on the system.
Exercises
Type pwd
What does pwd stand for? Hint: type help pwd help at the command prompt.
What does the output represent with regard to the Linux file system?
Type cd ..
Type pwd
What does the output represent with regard to the previous directory location?
Type cd /
Type pwd
What does the output represent with regard to the Linux file system, i.e. what part of the file
system does this command take you to?
Type cd
Type pwd
What does the output represent with regard to the part of the directory system that the cd
command takes you to?
Getting Help in Linux
Help files are available from the Linux help system. This system is composed of what are called
MAN (manual) pages. Simply type man followed by the name of the command you want to
know more about. Explanations will pause automatically after a page of information has been
displayed on your monitor. Press the spacebar to advance to the next page. When you wish to
terminate a MAN page type q for quit.
Exercises
Use the touch command to create the files needed for the remaining exercises. Is it obvious what
the touch command is designed to do? What does the MAN page for touch say about the purpose
of this command?
Type touch Ab
Type touch A1
In Linux, each file has a set of access permissions. The permissions are read (“r”), write
(“w”), and execute (“x”), and give, respectively, permission to read the contents of a file,
modify the file, or execute the file as a program. Permissions are set by the owner of a file. Linux
specifies access permissions separately for the owner of the file, a user group which is associated
the file, and the set of all users. So, the owner of a file can set the permissions so that all users
can read the files, but only the owner can modify the file. Root can ignore all access permissions
and can even change the ownership of files.
Exercises
Type ls -l file1
Type ls –l file1
What are the new file1 permissions for users and groups?
Are these permissions reasonable for file1? Answer the question in terms of who can modify
these files.
The command-line user interface of the Linux operating system is called a shell. A shell is
a program that interprets and executes Linux commands typed in a terminal window. Whenever
you create a new terminal window, a shell is started. The shell displays a prompt to indicate
where to type commands. The prompt can be modified to provide useful information for the user.
For example, the prompt [root@PC1 root]# displays the username, followed by the computer
name and the current directory. The prompt “PC1%” is the shell prompt for PC1.
When you type a command at the prompt, and press the enter key, the shell interprets the
command, and if it is a valid Linux command, the command is executed. A shell can be closed
by typing exit at the command prompt. If the shell is running in a window, the window also
closes.
Linux offers a variety of shell programs with names such as sh, csh, ksh, tcsh, or bash. The shells
provide different features for different purposes. The details will not be discussed here. The
bash shell is one of the more popular shells and is the one used in these exercises.
Exercises
Type csh
Type bash
Commands in Linux use the following general format: commandname option(s) argument(s),
although the options and arguments may be omitted. For example, in the command ls –l data.txt,
ls is the command, l is an option , and data.txt is the argument. Options are generally preceded
by a – (dash). Multiple options can be specified for a single command.
Linux provides on-line help for commands through the man pages. The man pages offer detailed
information on a command, are quite technical and assume that you are familiar with the
command and need only to lookup certain details. Desktop environments, such as Gnome, may
provide additional documentation. The man cmd displays the on-line manual pages. For
example, the command man ls displays the manual pages of the command ls.
Exercises
Type man ls
Directory Commands
Since files in Linux are organized as a tree of directories, you will need to become familiar
with navigating the directory tree. The following are essential directory commands:
pwd
cd Changes the current directory to the relative or absolute pathname of the directory dirpath. If
no directory is given, the command changes the current directory to the home directory.
mkdir dirname
rmdir dirname
Deletes the directory dirname from the current directory. A directory must be empty before it can
be deleted, otherwise an error message is displayed.
Exercises
Changing Directories
Type cd /usr/bin
Type pwd
Type cd
Making a Directory
Type ls –l
Look for the line listing information for directory xyz. Which character in the listing indicates
that xyz is a directory?
Removing a Directory
Type cd files
Type cd subfiles
Type cd ..
Type ls –l
Type cd ..
File commands
ls dirname
Lists information about files and directories in the current directory. If the command has
a directory name as argument (ls dirname), then the command lists the files in that directory. The
ls command has several options. The most important is ls –l, which includes extensive
information on each file, including, the access permissions, owner, file size, and the time when
the file was last modified.
Exercises
Type ls
Type ls –l ..
Type ls /
Wildcards
The wildcard character * matches any sequence of zero or more characters, and ? matches any
single character. Wildcard characters are useful to describe multiple files in a concise manner.
Exercises
Type ls A*.txt
Type ls A?
Moving Files
Renames or moves a file. The file or directory fname is renamed as newfile. If the destination
file (newfile) exists, then the content of the file is overwritten, and the old content of newfile is
lost. If the first argument is a file name and the second argument is a directory name (dirname),
the file is moved to the specified directory.
Exercises
Type ls –l data.txt
Type mv * /root (spacing in critical for this command. There is a space after the m and also
after the asterisk)
Explain the error messages that are printed as a result of the last two commands above.
Which account could perform the above steps without getting the error message?
Which command would allow you to access the above mentioned account?
Exercises
Type cd tmp
Type ls -l
Deleting Files
rm fname
Note: Linux may not issue a warning when a file is overwritten or when a file is removed.
When you use the option –i, Linux asks for confirmation before overwriting or deleting files. It is
strongly recommend that you use the –i option for copying, moving, and removing files (cp –i
instead of cp, mv – i instead of mv, and rm –i instead of rm). Many shells are configured to
always use the -i option. It is important to remember that Linux does not have an undo command
that reverses the effects of a previously issued command.
Exercises
Type rm –i newFile
Type y
Type ls –l newFile
Type ls –l oldfile
What additional feature does the rm –i command offer over the rm command ?
Viewing Files
The more and cat commands allow you to see the content of textfiles.
more fname
Displays the contents of file fname, one page at a time. The display can be scrolled with the ‘‘
and ‘↓‘ keys, and the ‘Page Up‘ and ‘Page Down‘ keys. Keyboard controls are space bar or ‘f’
for the next page, ‘b’ for the previous page, and ‘q’ to close the display.
cat fname
cat is similar to the more command, but the contents of the file is displayed without stopping at
the end of each page.
Exercises
Type cd /etc
Type cd
File Redirection
The following statement shows the syntax for redirecting command screen output to a file.
The output of cmd is written to file fname. The file is created if it doesn’t already exist, and the
contents are overwritten if the file already exists.
cmd >> fname
The >> operator appends the output of command cmd to the end of existing file fname.
Exercises
Type echo “This is the first line of the file” > mlist
Type clear
Type echo “This is the last line of the file” >> mlist