Professional Documents
Culture Documents
Chapter 2:
Basic Commands and Getting Help
- 1 -
Linux Platform – I Chapter: 2
Title Page
1. Switching between Consoles and the Graphical Environment 5
4. Getting Help 18
- 2 -
Linux Platform – I Chapter: 2
Learning Objectives
Upon completion this chapter, the student should be able to :
1. Switch between virtual consoles and the graphical environment ;
2. Create new user accounts and change their passwords ;
3. Execute basic commands such as: pwd, date, cal;... ,
4. Get help when needed.
Keywords
GUI, CLI, virtual console, terminal, useradd, passwd, ls, pwd, file, whoami, who am
I, w, who, id, su, sudo, cat, tac, more, less, date, cal, tree, help, man, whatis,
makewhatis, apropos.
- 3 -
Linux Platform – I Chapter: 2
Introduction
After installing CentOS correctly, you must reboot it and login to the system. You
can login with either a Graphical User Interface (GUI) or a Command Line Interface
(CLI) which is a text-based prompt. Although GUI will have installed by default
and easy to control by novice users, you should learn how to use CLI to be a good
Linux user. Note that the GUI and its tools are out of the scope of our study.
- 4 -
Linux Platform – I Chapter: 2
- 5 -
Linux Platform – I Chapter: 2
If you use the graphical environment, you should know that CTRL+ALT+F1 will return
you to it, and the screen will look like the following:
When logging into Linux using a GUI, you will have to start a shell manually. Some
GUIs provide a menu option to start a program called a terminal, xterm, Konsole, or
something similar.
- 6 -
Linux Platform – I Chapter: 2
To verify that the user is created, just type the following command line:
The newUser is created and its UID (User Identifier) is 1002. Now, you should enter
a password for this new user:
- 7 -
Linux Platform – I Chapter: 2
- 8 -
Linux Platform – I Chapter: 2
ls command
The ls command lists the names of all (or selected) files in a directory, and tells you
about the attributes of those files. The syntax of this command is:
Some common options for the ls command are shown in the following table:
Option Description
-a, --all Include files that start with ‘.’ (hidden files)
List information about the directory (or file) itself, not the directory's
-d, --directory
contents.
List the files in a directory, with a different flag for each type of file:
-F
directory (/), executable (*), soft link (@)
-n, --numeric-
Use numeric UIDs and GIDs, rather than usernames and groupnames
uid-gid
- 9 -
Linux Platform – I Chapter: 2
Report (or sort by) time specified by WORD instead of mtime. WORD
--time=WORD
may be one of "atime", "access", "ctime", or "status".
[student@StudentHost ~]$ ls –a
File1.txt .bash_profile file1.txt .bash_logout
. ..bash_history .bashrc services stuff .ssh File3.txt classes
[student@StudentHost ~]$ ls –l
Total 98
-rw-r--r-- 1 student student 19558 Sep 17 00:04 services
drwxr-xr-x 2 student student 4096 Sep 19 15:17 stuff
Output is truncated…
It is very important to understand every detail of the output given by the ls –l command,
for that reason, we describe the output contents in the following table:
- 10 -
Linux Platform – I Chapter: 2
Detail Description
The total size of your directory in blocks (1024 bytes) on the hard
disk, which is 98 in our example. Each file and directory is listed with
Total 98
a lot of information. (keep in mind that this does not include
subdirectories)
The file type and permissions of the file (in this example, the first
drwxr-xr-x
character ‘d’ means the file is a directory)
The file's link count, or the total number of entries (filenames) that
2 refer to this file. (keep in mind that it is always greater than 1 for
directories)
Student (first
The file's owner
word)
Student (second
The file's group owner
word)
Sep 19 15:17 The file's mtime, or last time the file was modified
pwd command
This command allows you to know “where are you in the directory tree”; pwd stands
for “Print Working Directory”:
- 11 -
Linux Platform – I Chapter: 2
file command
This command determines the file type, and displays results to terminal window. Its
syntax is:
The contents of any given file might be ASCII (plain text, HTML, shell script, program
source code, etc.) or binary (compiled executable, compressed archive, audio, etc.).
Here are some examples:
and here is another example, showing information about several other file types :
whoami command
This command tells you your username.
- 12 -
Linux Platform – I Chapter: 2
who command
This command gives you information about who is logged on the system.
who am i command
This command displays only the line pointing to your current session.
w command
This command shows you who is logged on and what they are doing.
[student@StudentHost ~]$ w
01:54:47 up 1 day, 9:09,2 users, load average: 0.03, 0.01. 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
student tty1 - sun23 6.00s 5.35s 0.00s w
root tt - sun23 8:32m 0.40s 0.40s -bash
id command
This command gives you your user id, primary group id, and a list of the groups that
you belong to (on RHEL/CentOS you will also get SELinux context information with
this command). To find out information about your identity, use the id command as
follows:
[student@StudentHost ~]$ id
uid=1000(student)gid=1000(student) groups=1000 (student)
Su command
This command allows a user to run a shell as another user. You can also use su
command to become root, when you know the root password.
[student@StudentHost ~]$ su C1IOS
Password:
[C1IOS@RaddadHost raddad]$
- 13 -
Linux Platform – I Chapter: 2
su - $username command
The su command maintains, by default, the same shell environment. To become
another user and get the target user's environment, issue the su - command
followed by the target username.
sudo command
This command allows a user to start a program with the credentials of another user.
Before this works, the system administrator has to set up the /etc/sudoers file.
This can be useful to delegate administrative tasks to another user without giving the
root password.
The example below shows the usage of sudo. User student received the right to
run useradd with the credentials of root. This allows student to create new users on
the system without becoming root and without knowing the root password.
cat command
This command concatenates files to standard output. Its syntax is as follows:
It can be used to show the contents of one or more files on the terminal window.
The most common options for this command is –n which numbers all output lines
and –b which numbers nonempty output lines (overrides –n). The following
command lines illustrate how to use cat command:
- 14 -
Linux Platform – I Chapter: 2
tac command
This command concatenates and prints files in reverse. The following example
shows you the purpose of tac (cat backwards).
- 15 -
Linux Platform – I Chapter: 2
date command
This command displays the date, time, time zone and more.
A date string can be customized to display the format of your choice (use the next
section to check the man page for more options).
cal command
If this command used alone (without options or arguments), it will display the current
month, with the current day highlighted. However, you can select any month in the
past or the future to be shown and any day to be highlighted. In the next command,
we choose May 24, 1977:
tree command
With no arguments, the command tree lists the files in the current directory. When
directory arguments are given, it lists all the files or directories found in the given
directories each in turn. Upon completion of listing all files and directories found, tree
command returns the total number of files and directories listed. There are options to
change the characters used in the output, and to use color output. You can type
man tree; and press Enter key to show all available options for this command.
- 16 -
Linux Platform – I Chapter: 2
- 17 -
Linux Platform – I Chapter: 2
4. Getting Help
People new to the CLI think that every command and each little argument must be
committed to memory. Linux provides so many commands and so many possible
options for each command; you can't expect to remember all of them. There are
megabytes of documentation that can answer all of your questions, if you just know
how to access it quickly.
help command
The help command displays information about shell built-in commands. For example,
the command line shows not only options, but also time formats you can use with the
date command:
This command displays a help screen describing the ls command and its usage. To
learn about how the man page system itself works, type man man.
The man pages provide succinct summaries of what a command or file does. They
are formatted from special source files, and are displayed using the less pager to
display information. They are grouped into "sections". The common sections of the
Linux manual are listed in the following table:
- 18 -
Linux Platform – I Chapter: 2
This table is very useful in order to find quickly the exact man page you are looking
for. Sometimes a single keyword has entries in multiple sections. For example,
“passwd” has entries under both section 1 and section 5. In most cases, man returns
the entry in the lowest-numbered section; but you can force the issue by preceding
the keyword by the section number. The user command passwd is used to change
your login password. If you type:
you will get the man page for this command, since it is found in section 1. However,
there is also a file called passwd that stores the account information on your system.
The format for this file is explained in a man page in section 5. To get to it, you need
to type:
then, the man command will look in section 5 rather than starting at section 1 .
To navigate man pages, you can use arrows, PgUp, PgDn. To look for a word, you
should use forward slash (/) followed by the term you are looking for and press Enter.
To look for “word” for example, you should type /word and to go to the next/previous
match, you should use n/N, respectively. To quit the man pages, you should type q
character.
- 19 -
Linux Platform – I Chapter: 2
As any other command, you can use options with man command; the following table
contains its most common options:
Option Description
Search the given command as a regular expression in all the manuals and it
-k
returns the manual pages with the section number in which it is found
-w Return the location in which the manual page of a given command is present
No
Display the whole man pages of the command
option
Man pages are often very detailed and might tell you far more than you wanted to
know. If you just want a quick idea of what a command does, use the whatis
command.
Note: Should you be convinced that a man page exists, but you can't access it, then
try running makewhatis on CentOS/Redhat. For each page, the makewhatis program
writes a line in the whatis database.
whatis command
The whathis command provides very brief descriptions of the component name that
follows it. It searches the short descriptions in the whatis database for each keyword
provided to it as an argument (i.e., input data). This database contains just the title,
section number and description from the NAME section of each page in the man
manual that is built into most Linux systems. The whatis database is a plain text (i.e.,
human-readable characters) file that is generated automatically by the makewhatis
program .
- 20 -
Linux Platform – I Chapter: 2
The output of whatis is limited by the fact that it provides only a single line for each
keyword found in the database; thus it supplies incomplete information about even
moderately complex commands. Here are two examples of this command:
When you use the man command with its -f option, it produces the same output as
whatis, for example:
is equivalent to:
apropos command
This command produces the same output as the man command with its -k option. For
example, if you want to know about renaming files, try this command line:
- 21 -
Linux Platform – I Chapter: 2
By reading through the result, you will find multiple commands that could be used to
rename your files, you can choose the command you want.
- 22 -
Linux Platform – I Chapter: 2
Questions
1. How to display only your currently logged-on user name.
2. List all logged-on users.
3. List the contents of the root directory.
4. List a long listing of the root directory.
5. List of all logged-on users including the command they are running.
6. Display your user name and your unique user identification (userid).
7. Stay at your home directory, and list the contents of /etc.
8. Stay where you are, and list the contents of /bin and /sbin.
9. Stay where you are, and list the contents of ~.
10. List all the files (including hidden files) in your home directory.
11. List the files in /boot in a human readable format.
12. Use less command to display /etc/services.
13. Use ls command to find the biggest file in /etc.
14. Use cat command to display the contents of .bashrc followed by the contents
of /etc/passwd.
15. Use more command to display /var/log/secure*.
16. Use su to switch to another user account (unless you are root, you will need
the password of the other account), and get back to the previous account.
17. Now use su - to switch to another user and notice the difference.
18. Note that su - gets you into the home directory of Yara.
19. When using your regular user account, try to create a new user account. What
has been happened?
20. Now try the same, but with sudo before your command.
References
1. Red Hat Linux Essentials RH033-RHEL5-en-2-20070306
2. Paul Cobbaut, “Linux Fundamentals”, https://linux-
training.be/funhtml/index.html. Updated on 2015-05-24
- 23 -